Skip to content

Switch from config factory function to array of plugins #5

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

Merged
merged 2 commits into from
Mar 15, 2021
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 .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Avoid tabs in code samples
README.md
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ npm install --save-dev @preact/preset-vite
yarn add -D @preact/preset-vite
```

Enhance your vite config with the Preact preset:
Enhance your vite config with the Preact preset plugin in your `vite.config.ts` or `vite.config.js`:

```js
// vite.config.js or vite.config.ts
import withPreact from "@preact/preset-vite";
import { defineConfig } from "vite";
import preact from "@preact/preset-vite";

export default withPreact({
// Your usual vite config
export default defineConfig({
plugins: [preact()]
});
```

## Options

Options can be passed to our preset by adding a second argument:
Options can be passed to our preset plugin via the first argument:

```js
export default withPreact(viteConfig, {
// Add your options here
devtoolsInProd: true
export default defineConfig({
plugins: [
preact({ devtoolsInProd: true })
]
});
```

Expand Down
6 changes: 4 additions & 2 deletions demo/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defineConfig } from "vite";
import withPreact from "../src/index";
import preact from "../src/index";

// https://vitejs.dev/config/
export default defineConfig(withPreact({}));
export default defineConfig({
plugins: [preact()],
});
48 changes: 25 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { mergeConfig, UserConfig } from "vite";
import { Plugin } from "vite";
import prefresh from "@prefresh/vite";
import { preactDevtoolsPlugin } from "./devtools";

export interface PreactPluginOptions {
devtoolsInProd?: boolean;
}

export default function withPreact(
config: UserConfig,
{ devtoolsInProd }: PreactPluginOptions = {},
): UserConfig {
const preactConfig: UserConfig = {
esbuild: {
jsxFactory: "h",
jsxFragment: "Fragment",
jsxInject: `import { h, Fragment } from 'preact'`,
},
resolve: {
alias: {
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
react: "preact/compat",
export default function preactPlugin({
devtoolsInProd,
}: PreactPluginOptions = {}): Plugin[] {
return [
{
name: "preact:config",
config() {
return {
esbuild: {
jsxFactory: "h",
jsxFragment: "Fragment",
jsxInject: `import { h, Fragment } from 'preact'`,
},
resolve: {
alias: {
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
react: "preact/compat",
},
},
};
},
},
plugins: [
preactDevtoolsPlugin({ injectInProd: devtoolsInProd }),
prefresh(),
],
};

return mergeConfig(config, preactConfig);
preactDevtoolsPlugin({ injectInProd: devtoolsInProd }),
prefresh(),
];
}