Skip to content

Run a transform hook before JSX is compiled but after TypeScript syntax is removed #16630

Closed Answered by aleclarson
aleclarson asked this question in Q&A
Discussion options

You must be logged in to vote

It appears that using vite.transformWithEsbuild within my transform hook is the best approach. Since my transform hook produces a sourcemap of its own, I've had to split my plugin into two plugins (each with its own transform hook) so that Vite combines the sourcemap I produce with the one transformWithEsbuild produces.

// Remove any TypeScript syntax but preserve JSX.
if (id.endsWith('.tsx')) {
  const transformed = await vite.transformWithEsbuild(code, id, {
    jsx: 'preserve',
    loader: 'tsx',
  })
  return {
    code: transformed.code,
    map: transformed.map,
    meta: { isFoo: true },
  }
}
return {
  meta: { isFoo: true },
}

In the second plugin's transform hook, I call this.ge…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by aleclarson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant