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

Provide options to disable devtools and prefresh #65

Merged
merged 4 commits into from
Nov 15, 2022
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand All @@ -75,6 +90,9 @@ function preactPlugin({
exclude || [/node_modules/],
);

devToolsEnabled = devToolsEnabled ?? true;
prefreshEnabled = prefreshEnabled ?? true;

const jsxPlugin: Plugin = {
name: "vite:preact-jsx",
enforce: "pre",
Expand Down Expand Up @@ -164,8 +182,15 @@ function preactPlugin({
},
},
jsxPlugin,
preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform }),
prefresh({ include, exclude }),
...(devToolsEnabled
? [
preactDevtoolsPlugin({
injectInProd: devtoolsInProd,
shouldTransform,
}),
]
: []),
...(prefreshEnabled ? [prefresh({ include, exclude })] : []),
];
}

Expand Down