Skip to content

Commit

Permalink
refactor: Use ESBuild in prod if a user doesn't require Babel
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 16, 2024
1 parent 19ca2f4 commit 67f68e4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/],
Expand All @@ -151,6 +152,10 @@ function preactPlugin({
},
},
},
esbuild: {
jsx: "automatic",
jsxImportSource: jsxImportSource ?? "preact",
},
optimizeDeps: {
include: ["preact/jsx-runtime", "preact/jsx-dev-runtime"],
},
Expand All @@ -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,
Expand Down

0 comments on commit 67f68e4

Please sign in to comment.