Skip to content

Commit

Permalink
refactor: Remove devtoolsInProd option, default to dev/prod
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 16, 2024
1 parent 238a9f8 commit d283242
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
9 changes: 5 additions & 4 deletions src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -37,6 +37,7 @@ export function preactDevtoolsPlugin({

configResolved(resolvedConfig) {
config = resolvedConfig;
devToolsEnabled ??= !config.isProduction;
},

resolveId(url, importer = "") {
Expand All @@ -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)}`);
Expand Down
22 changes: 4 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -131,7 +124,6 @@ function preactPlugin({
exclude || [/node_modules/],
);

devToolsEnabled = devToolsEnabled ?? true;
prefreshEnabled = prefreshEnabled ?? true;
reactAliasesEnabled = reactAliasesEnabled ?? true;
prerender = prerender ?? { enabled: false };
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -235,14 +228,7 @@ function preactPlugin({
]
: []),
jsxPlugin,
...(devToolsEnabled
? [
preactDevtoolsPlugin({
injectInProd: devtoolsInProd,
shouldTransform,
}),
]
: []),
preactDevtoolsPlugin({ shouldTransform }),
...(prefreshEnabled
? [prefresh({ include, exclude, parserPlugins: baseParserOptions })]
: []),
Expand Down

0 comments on commit d283242

Please sign in to comment.