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

Add support for custom babel configuration #44

Merged
merged 1 commit into from
Mar 29, 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ export default defineConfig({
|---|---|---|---|
| `devtoolsInProd` | `boolean` | `false` | Inject devtools bridge in production bundle instead of only in development mode |

### Babel configuration

The `babel` option lets you add plugins, presets, and [other configuration](https://babeljs.io/docs/en/options) to the Babel transformation performed on each JSX/TSX file.

```js
preact({
babel: {
presets: [...],
// Your plugins run before any built-in transform (eg: Fast Refresh)
plugins: [...],
// Use .babelrc files
babelrc: true,
// Use babel.config.js files
configFile: true,
}
})
```

## License

MIT, see [the license file](./LICENSE).
46 changes: 43 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import type { Plugin, ResolvedConfig } from "vite";
import type { FilterPattern } from "@rollup/pluginutils";
import type { ParserPlugin } from "@babel/parser";
import type { ParserPlugin, ParserOptions } from "@babel/parser";
import type { TransformOptions } from "@babel/core";

import resolve from "resolve";
import prefresh from "@prefresh/vite";
import { preactDevtoolsPlugin } from "./devtools.js";
import { createFilter, parseId } from "./utils.js";
import { transformAsync } from "@babel/core";

export type BabelOptions = Omit<
TransformOptions,
| "ast"
| "filename"
| "root"
| "sourceFileName"
| "sourceMaps"
| "inputSourceMap"
>;

export interface PreactPluginOptions {
/**
* Inject devtools bridge in production bundle instead of only in development mode.
Expand All @@ -23,16 +34,43 @@ export interface PreactPluginOptions {
* RegExp or glob to match files to NOT be transformed
*/
exclude?: FilterPattern;

/**
* Babel configuration applied in both dev and prod.
*/
babel?: BabelOptions;
}

export interface PreactBabelOptions extends BabelOptions {
plugins: Extract<BabelOptions["plugins"], any[]>;
presets: Extract<BabelOptions["presets"], any[]>;
overrides: Extract<BabelOptions["overrides"], any[]>;
parserOpts: ParserOptions & {
plugins: Extract<ParserOptions["plugins"], any[]>;
};
}

// Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
export default function preactPlugin({
devtoolsInProd,
include,
exclude,
babel,
}: PreactPluginOptions = {}): Plugin[] {
let config: ResolvedConfig;

let babelOptions = {
babelrc: false,
configFile: false,
...babel,
} as PreactBabelOptions;

babelOptions.plugins ||= [];
babelOptions.presets ||= [];
babelOptions.overrides ||= [];
babelOptions.parserOpts ||= {} as any;
babelOptions.parserOpts.plugins ||= [];

const shouldTransform = createFilter(
include || [/\.[tj]sx?$/],
exclude || [/node_modules/],
Expand Down Expand Up @@ -89,20 +127,22 @@ export default function preactPlugin({
].filter(Boolean) as ParserPlugin[];

const result = await transformAsync(code, {
babelrc: false,
configFile: false,
...babelOptions,
ast: true,
root: config.root,
filename: id,
parserOpts: {
...babelOptions.parserOpts,
sourceType: "module",
allowAwaitOutsideFunction: true,
plugins: parserPlugins,
},
generatorOpts: {
...babelOptions.generatorOpts,
decoratorsBeforeExport: true,
},
plugins: [
...babelOptions.plugins,
[
config.isProduction
? "@babel/plugin-transform-react-jsx"
Expand Down