-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from config factory function to array of plugins
Releasing this preset to the public sparked a discussions on how presets should be done in vite. Presumably we were the first project to do so. It turns out that plugins can be an array of plugins. This makes the config more composable and we can avoid using a somewhat internal function to merge configs. See this issue for more background: vitejs/vite#2500
- Loading branch information
1 parent
393a522
commit d2f1dfd
Showing
3 changed files
with
39 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} |