From 0f165c97d8e02eb95ac369a21f4ab623896ef0c9 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sat, 16 Mar 2024 07:17:56 -0500 Subject: [PATCH 1/4] refactor: Use ESBuild in prod if a user doesn't require Babel --- src/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 7517dca..c976a99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,6 +126,7 @@ function preactPlugin({ babelOptions.parserOpts ||= {} as any; babelOptions.parserOpts.plugins ||= []; + let useBabel: boolean; const shouldTransform = createFilter( include || [/\.[tj]sx?$/], exclude || [/node_modules/], @@ -151,6 +152,10 @@ function preactPlugin({ }, }, }, + esbuild: { + jsx: "automatic", + jsxImportSource: jsxImportSource ?? "preact", + }, optimizeDeps: { include: ["preact/jsx-runtime", "preact/jsx-dev-runtime"], }, @@ -160,12 +165,20 @@ function preactPlugin({ config = resolvedConfig; devToolsEnabled = devToolsEnabled ?? (!config.isProduction || devtoolsInProd); + + useBabel = + !config.isProduction || + devToolsEnabled || + !!babelOptions.plugins.length || + !!babelOptions.presets.length || + !!babelOptions.overrides.length || + !!babelOptions.parserOpts.plugins.length; }, async transform(code, url) { // Ignore query parameters, as in Vue SFC virtual modules. const { id } = parseId(url); - if (!shouldTransform(id)) return; + if (!useBabel || !shouldTransform(id)) return; const parserPlugins = [ ...baseParserOptions, From a5ff486641c73df994f7608cfea5455ca10a3391 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sat, 16 Mar 2024 07:21:56 -0500 Subject: [PATCH 2/4] refactor: Should be able to get away with a simpler Babel check --- src/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index c976a99..284ebdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -167,12 +167,7 @@ function preactPlugin({ devToolsEnabled ?? (!config.isProduction || devtoolsInProd); useBabel = - !config.isProduction || - devToolsEnabled || - !!babelOptions.plugins.length || - !!babelOptions.presets.length || - !!babelOptions.overrides.length || - !!babelOptions.parserOpts.plugins.length; + !config.isProduction || devToolsEnabled || typeof babel !== "undefined"; }, async transform(code, url) { // Ignore query parameters, as in Vue SFC virtual modules. From 48b5c76be283f65810cd08ff47cc406abae735e7 Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Sun, 17 Mar 2024 19:21:52 -0500 Subject: [PATCH 3/4] chore: Formatting --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 284ebdb..556896f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,7 +165,6 @@ function preactPlugin({ config = resolvedConfig; devToolsEnabled = devToolsEnabled ?? (!config.isProduction || devtoolsInProd); - useBabel = !config.isProduction || devToolsEnabled || typeof babel !== "undefined"; }, From 0fddc4f029f66486a3df0ac452377eed8b2c221c Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:21:01 -0500 Subject: [PATCH 4/4] chore: Add comment to ESBuild config --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 556896f..b64b198 100644 --- a/src/index.ts +++ b/src/index.ts @@ -152,6 +152,7 @@ function preactPlugin({ }, }, }, + // While this config is unconditional, it'll only be used if Babel is not esbuild: { jsx: "automatic", jsxImportSource: jsxImportSource ?? "preact",