Skip to content

Commit e55fc29

Browse files
Merge pull request #5 from preactjs/array-of-plugins
2 parents 3285369 + d2f1dfd commit e55fc29

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Avoid tabs in code samples
2+
README.md

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ npm install --save-dev @preact/preset-vite
1717
yarn add -D @preact/preset-vite
1818
```
1919

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

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

26-
export default withPreact({
27-
// Your usual vite config
26+
export default defineConfig({
27+
plugins: [preact()]
2828
});
2929
```
3030

3131
## Options
3232

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

3535
```js
36-
export default withPreact(viteConfig, {
37-
// Add your options here
38-
devtoolsInProd: true
36+
export default defineConfig({
37+
plugins: [
38+
preact({ devtoolsInProd: true })
39+
]
3940
});
4041
```
4142

demo/vite.config.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineConfig } from "vite";
2-
import withPreact from "../src/index";
2+
import preact from "../src/index";
33

44
// https://vitejs.dev/config/
5-
export default defineConfig(withPreact({}));
5+
export default defineConfig({
6+
plugins: [preact()],
7+
});

src/index.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
import { mergeConfig, UserConfig } from "vite";
1+
import { Plugin } from "vite";
22
import prefresh from "@prefresh/vite";
33
import { preactDevtoolsPlugin } from "./devtools";
44

55
export interface PreactPluginOptions {
66
devtoolsInProd?: boolean;
77
}
88

9-
export default function withPreact(
10-
config: UserConfig,
11-
{ devtoolsInProd }: PreactPluginOptions = {},
12-
): UserConfig {
13-
const preactConfig: UserConfig = {
14-
esbuild: {
15-
jsxFactory: "h",
16-
jsxFragment: "Fragment",
17-
jsxInject: `import { h, Fragment } from 'preact'`,
18-
},
19-
resolve: {
20-
alias: {
21-
"react-dom/test-utils": "preact/test-utils",
22-
"react-dom": "preact/compat",
23-
react: "preact/compat",
9+
export default function preactPlugin({
10+
devtoolsInProd,
11+
}: PreactPluginOptions = {}): Plugin[] {
12+
return [
13+
{
14+
name: "preact:config",
15+
config() {
16+
return {
17+
esbuild: {
18+
jsxFactory: "h",
19+
jsxFragment: "Fragment",
20+
jsxInject: `import { h, Fragment } from 'preact'`,
21+
},
22+
resolve: {
23+
alias: {
24+
"react-dom/test-utils": "preact/test-utils",
25+
"react-dom": "preact/compat",
26+
react: "preact/compat",
27+
},
28+
},
29+
};
2430
},
2531
},
26-
plugins: [
27-
preactDevtoolsPlugin({ injectInProd: devtoolsInProd }),
28-
prefresh(),
29-
],
30-
};
31-
32-
return mergeConfig(config, preactConfig);
32+
preactDevtoolsPlugin({ injectInProd: devtoolsInProd }),
33+
prefresh(),
34+
];
3335
}

0 commit comments

Comments
 (0)