Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Devtool flag inconsistencies #113

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 && devToolsEnabled) {
const source = devtoolsInProd ? "preact/devtools" : "preact/debug";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why the source changes based upon whether devtoolsInProd is enabled or not. Is this correct? This seems wrong to me, but it's perfectly possible I'm missing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During production you likely don't want the weight of preact/debug with all the warnings, just the devtools connection bridge. That's why the switch was originally introduced. The preact/devtools bridge is <200b .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh shoot, I had it in my mind for some reason that devtools pulled in debug, not the other way around. Didn't even double check when confused.

My bad, cheers!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, I think this is a bit of a problem as it'd mean enabling it would cause problems with dev, no?

Will correct tomorrow

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be correct now, will always provide 'preact/debug' in dev if devToolsEnabled is set to true/default, while in prod, you'll get 'preact/devtools' only if you've enabled devtoolsInProd.

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