From 9a87450f87f6128ab73360e47f6bdd6a5963a721 Mon Sep 17 00:00:00 2001 From: Liam Don Date: Mon, 14 Nov 2022 12:58:06 -0800 Subject: [PATCH 1/4] Provide options to disable devtools and prefresh --- src/index.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 06fbc8e..7068ff1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,19 @@ export interface PreactPluginOptions { * @default false */ devtoolsInProd?: boolean; + + /** + * Whether to use Preact devtools + * @default true + */ + devToolsEnabled?: boolean; + + /** + * Whether to use prefresh HMR + * @default true + */ + prefreshEnabled?: boolean; + /** * RegExp or glob to match files to be transformed */ @@ -52,6 +65,8 @@ export interface PreactBabelOptions extends BabelOptions { // Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts function preactPlugin({ devtoolsInProd, + devToolsEnabled, + prefreshEnabled, include, exclude, babel, @@ -75,6 +90,9 @@ function preactPlugin({ exclude || [/node_modules/], ); + devToolsEnabled = devToolsEnabled ?? true; + prefreshEnabled = prefreshEnabled ?? true; + const jsxPlugin: Plugin = { name: "vite:preact-jsx", enforce: "pre", @@ -148,7 +166,7 @@ function preactPlugin({ }; }, }; - return [ + const plugins = [ { name: "preact:config", config() { @@ -164,9 +182,17 @@ function preactPlugin({ }, }, jsxPlugin, - preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform }), - prefresh({ include, exclude }), ]; + + if (devToolsEnabled) { + plugins.push( + preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform }), + ); + } + if (prefreshEnabled) { + plugins.push(prefresh({ include, exclude })); + } + return plugins; } export default preactPlugin; From 66703eefc7e87cab78fd89943d49d8a78556027e Mon Sep 17 00:00:00 2001 From: Liam Don Date: Mon, 14 Nov 2022 13:08:45 -0800 Subject: [PATCH 2/4] Add documentation of new options --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0bc217c..28c0126 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ export default defineConfig({ | Option | Type | Default | Description | |---|---|---|---| +| `devToolsEnabled` | `boolean` | `true` | Inject devtools bridge | +| `prefreshEnabled` | `boolean` | `true` | Inject [Prefresh](https://github.com/preactjs/prefresh) for HMR | | `devtoolsInProd` | `boolean` | `false` | Inject devtools bridge in production bundle instead of only in development mode | ### Babel configuration From 948d6e21dfd8ef2e43bd460245eba078bece4d20 Mon Sep 17 00:00:00 2001 From: Liam Don Date: Mon, 14 Nov 2022 13:12:41 -0800 Subject: [PATCH 3/4] Use spread operator for options to match project style --- src/index.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7068ff1..cbd002a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -166,7 +166,7 @@ function preactPlugin({ }; }, }; - const plugins = [ + return [ { name: "preact:config", config() { @@ -182,17 +182,16 @@ function preactPlugin({ }, }, jsxPlugin, + ...(devToolsEnabled + ? [] + : [ + preactDevtoolsPlugin({ + injectInProd: devtoolsInProd, + shouldTransform, + }), + ]), + ...(prefreshEnabled ? [] : [prefresh({ include, exclude })]), ]; - - if (devToolsEnabled) { - plugins.push( - preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform }), - ); - } - if (prefreshEnabled) { - plugins.push(prefresh({ include, exclude })); - } - return plugins; } export default preactPlugin; From e856824928d2ebbe8db71d1dd9342cdf00fa39de Mon Sep 17 00:00:00 2001 From: Liam Don Date: Mon, 14 Nov 2022 13:17:36 -0800 Subject: [PATCH 4/4] Fix ternary condition --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index cbd002a..bfc8609 100644 --- a/src/index.ts +++ b/src/index.ts @@ -183,14 +183,14 @@ function preactPlugin({ }, jsxPlugin, ...(devToolsEnabled - ? [] - : [ + ? [ preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform, }), - ]), - ...(prefreshEnabled ? [] : [prefresh({ include, exclude })]), + ] + : []), + ...(prefreshEnabled ? [prefresh({ include, exclude })] : []), ]; }