From d2832421b5ec0a8bb78eefa6af4af38c30c46821 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sat, 16 Mar 2024 05:43:14 -0500 Subject: [PATCH] refactor: Remove `devtoolsInProd` option, default to dev/prod --- README.md | 3 +-- src/devtools.ts | 9 +++++---- src/index.ts | 22 ++++------------------ 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5400e15..21a5d16 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,7 @@ export default defineConfig({ | Option | Type | Default | Description | |---|---|---|---| -| `devtoolsInProd` | `boolean` | `false` | Inject devtools bridge in production bundle instead of only in development mode | -| `devToolsEnabled` | `boolean` | `true` | Inject devtools bridge | +| `devToolsEnabled` | `boolean` | `!isProduction` | Inject devtools bridge | | `prefreshEnabled` | `boolean` | `true` | Inject [Prefresh](https://github.com/preactjs/prefresh) for HMR | | `reactAliasesEnabled` | `boolean` | `true` | Aliases `react`, `react-dom` to `preact/compat` | | `babel` | `object` | | See [Babel configuration](#babel-configuration) | diff --git a/src/devtools.ts b/src/devtools.ts index 19239cc..ee16ca0 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -7,12 +7,12 @@ import type { RollupFilter } from "./utils.js"; import { parseId } from "./utils.js"; export interface PreactDevtoolsPluginOptions { - injectInProd?: boolean; + devToolsEnabled?: boolean; shouldTransform: RollupFilter; } export function preactDevtoolsPlugin({ - injectInProd = false, + devToolsEnabled, shouldTransform, }: PreactDevtoolsPluginOptions): Plugin { const log = debug("vite:preact-devtools"); @@ -37,6 +37,7 @@ export function preactDevtoolsPlugin({ configResolved(resolvedConfig) { config = resolvedConfig; + devToolsEnabled ??= !config.isProduction; }, resolveId(url, importer = "") { @@ -58,8 +59,8 @@ export function preactDevtoolsPlugin({ transform(code, url) { const { id } = parseId(url); - if (entry === id && (!config.isProduction || injectInProd)) { - const source = injectInProd ? "preact/devtools" : "preact/debug"; + if (entry === id) { + const source = devToolsEnabled ? "preact/devtools" : "preact/debug"; code = `import "${source}";\n${code}`; log(`[inject] ${kl.cyan(source)} -> ${kl.dim(id)}`); diff --git a/src/index.ts b/src/index.ts index 029cbdb..0e63b65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,15 +20,9 @@ export type BabelOptions = Omit< >; export interface PreactPluginOptions { - /** - * Inject devtools bridge in production bundle instead of only in development mode. - * @default false - */ - devtoolsInProd?: boolean; - /** * Whether to use Preact devtools - * @default true + * @default !isProduction */ devToolsEnabled?: boolean; @@ -97,7 +91,6 @@ 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, reactAliasesEnabled, @@ -131,7 +124,6 @@ function preactPlugin({ exclude || [/node_modules/], ); - devToolsEnabled = devToolsEnabled ?? true; prefreshEnabled = prefreshEnabled ?? true; reactAliasesEnabled = reactAliasesEnabled ?? true; prerender = prerender ?? { enabled: false }; @@ -158,6 +150,7 @@ function preactPlugin({ }, configResolved(resolvedConfig) { config = resolvedConfig; + devToolsEnabled ??= !config.isProduction; }, async transform(code, url) { // Ignore query parameters, as in Vue SFC virtual modules. @@ -200,7 +193,7 @@ function preactPlugin({ importSource: jsxImportSource ?? "preact", }, ], - ...(devToolsEnabled && !config.isProduction ? ["babel-plugin-transform-hook-names"] : []), + ...(devToolsEnabled ? ["babel-plugin-transform-hook-names"] : []), ], sourceMaps: true, inputSourceMap: false as any, @@ -235,14 +228,7 @@ function preactPlugin({ ] : []), jsxPlugin, - ...(devToolsEnabled - ? [ - preactDevtoolsPlugin({ - injectInProd: devtoolsInProd, - shouldTransform, - }), - ] - : []), + preactDevtoolsPlugin({ shouldTransform }), ...(prefreshEnabled ? [prefresh({ include, exclude, parserPlugins: baseParserOptions })] : []),