From 356a76addadc8a530d70d0e2b5a1a801921ae203 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 4 Mar 2022 12:14:53 +0100 Subject: [PATCH] Add support for custom babel configuration --- README.md | 18 ++++++++++++++++++ src/index.ts | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f6fe4a7..03278b0 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,24 @@ export default defineConfig({ |---|---|---|---| | `devtoolsInProd` | `boolean` | `false` | Inject devtools bridge in production bundle instead of only in development mode | +### Babel configuration + +The `babel` option lets you add plugins, presets, and [other configuration](https://babeljs.io/docs/en/options) to the Babel transformation performed on each JSX/TSX file. + +```js +preact({ + babel: { + presets: [...], + // Your plugins run before any built-in transform (eg: Fast Refresh) + plugins: [...], + // Use .babelrc files + babelrc: true, + // Use babel.config.js files + configFile: true, + } +}) +``` + ## License MIT, see [the license file](./LICENSE). diff --git a/src/index.ts b/src/index.ts index 5a9cbb8..ee773cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import type { Plugin, ResolvedConfig } from "vite"; import type { FilterPattern } from "@rollup/pluginutils"; -import type { ParserPlugin } from "@babel/parser"; +import type { ParserPlugin, ParserOptions } from "@babel/parser"; +import type { TransformOptions } from "@babel/core"; import resolve from "resolve"; import prefresh from "@prefresh/vite"; @@ -8,6 +9,16 @@ import { preactDevtoolsPlugin } from "./devtools.js"; import { createFilter, parseId } from "./utils.js"; import { transformAsync } from "@babel/core"; +export type BabelOptions = Omit< + TransformOptions, + | "ast" + | "filename" + | "root" + | "sourceFileName" + | "sourceMaps" + | "inputSourceMap" +>; + export interface PreactPluginOptions { /** * Inject devtools bridge in production bundle instead of only in development mode. @@ -23,6 +34,20 @@ export interface PreactPluginOptions { * RegExp or glob to match files to NOT be transformed */ exclude?: FilterPattern; + + /** + * Babel configuration applied in both dev and prod. + */ + babel?: BabelOptions; +} + +export interface PreactBabelOptions extends BabelOptions { + plugins: Extract; + presets: Extract; + overrides: Extract; + parserOpts: ParserOptions & { + plugins: Extract; + }; } // Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts @@ -30,9 +55,22 @@ export default function preactPlugin({ devtoolsInProd, include, exclude, + babel, }: PreactPluginOptions = {}): Plugin[] { let config: ResolvedConfig; + let babelOptions = { + babelrc: false, + configFile: false, + ...babel, + } as PreactBabelOptions; + + babelOptions.plugins ||= []; + babelOptions.presets ||= []; + babelOptions.overrides ||= []; + babelOptions.parserOpts ||= {} as any; + babelOptions.parserOpts.plugins ||= []; + const shouldTransform = createFilter( include || [/\.[tj]sx?$/], exclude || [/node_modules/], @@ -89,20 +127,22 @@ export default function preactPlugin({ ].filter(Boolean) as ParserPlugin[]; const result = await transformAsync(code, { - babelrc: false, - configFile: false, + ...babelOptions, ast: true, root: config.root, filename: id, parserOpts: { + ...babelOptions.parserOpts, sourceType: "module", allowAwaitOutsideFunction: true, plugins: parserPlugins, }, generatorOpts: { + ...babelOptions.generatorOpts, decoratorsBeforeExport: true, }, plugins: [ + ...babelOptions.plugins, [ config.isProduction ? "@babel/plugin-transform-react-jsx"