diff --git a/src/vite-plugin.ts b/src/vite-plugin.ts new file mode 100644 index 00000000..574c152c --- /dev/null +++ b/src/vite-plugin.ts @@ -0,0 +1,36 @@ +import { BabelFileResult, transformSync } from '@babel/core'; + +type ParserPlugin = 'jsx' | 'typescript'; + +export default () => ({ + name: 'vite:astroturf', + enforce: 'pre', + transform: (src: string, id: string): { code: string } => { + if (id.includes('node_modules')) { + return { + code: src, + }; + } + const parserPlugins: ParserPlugin[] = []; + if (/\.(t|j)sx?$/.test(id)) { + parserPlugins.push('jsx'); + } + if (/\.tsx?$/.test(id)) { + parserPlugins.push('typescript'); + } + const result: BabelFileResult | null = transformSync(src, { + babelrc: false, + configFile: false, + filename: id, + parserOpts: { + plugins: parserPlugins, + }, + plugins: ['astroturf/plugin'], + sourceMaps: true, + sourceFileName: id, + }); + return { + code: result?.code || src, + }; + }, +});