Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import type { RollupFilter } from "./utils.js";
import { parseId } from "./utils.js";

export interface PreactDevtoolsPluginOptions {
injectInProd?: boolean;
devtoolsInProd?: boolean;
devToolsEnabled?: boolean;
shouldTransform: RollupFilter;
}

export function preactDevtoolsPlugin({
injectInProd = false,
devtoolsInProd,
devToolsEnabled,
shouldTransform,
}: PreactDevtoolsPluginOptions): Plugin {
const log = debug("vite:preact-devtools");
Expand All @@ -37,6 +39,8 @@ export function preactDevtoolsPlugin({

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

resolveId(url, importer = "") {
Expand All @@ -58,8 +62,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 && (!config.isProduction || devToolsEnabled)) {
const source = config.isProduction ? "preact/devtools" : "preact/debug";
code = `import "${source}";\n${code}`;

log(`[inject] ${kl.cyan(source)} -> ${kl.dim(id)}`);
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function preactPlugin({
exclude || [/node_modules/],
);

devToolsEnabled = devToolsEnabled ?? true;
devtoolsInProd = devtoolsInProd ?? false;
prefreshEnabled = prefreshEnabled ?? true;
reactAliasesEnabled = reactAliasesEnabled ?? true;
prerender = prerender ?? { enabled: false };
Expand All @@ -158,6 +158,8 @@ function preactPlugin({
},
configResolved(resolvedConfig) {
config = resolvedConfig;
devToolsEnabled =
devToolsEnabled ?? (!config.isProduction || devtoolsInProd);
},
async transform(code, url) {
// Ignore query parameters, as in Vue SFC virtual modules.
Expand Down Expand Up @@ -200,7 +202,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 +237,11 @@ function preactPlugin({
]
: []),
jsxPlugin,
...(devToolsEnabled
? [
preactDevtoolsPlugin({
injectInProd: devtoolsInProd,
shouldTransform,
}),
]
: []),
preactDevtoolsPlugin({
devtoolsInProd,
devToolsEnabled,
shouldTransform,
}),
...(prefreshEnabled
? [prefresh({ include, exclude, parserPlugins: baseParserOptions })]
: []),
Expand Down