forked from ps73/preact-preset-vite
/
index.ts
170 lines (154 loc) · 4.08 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { Plugin, ResolvedConfig } from "vite";
import type { FilterPattern } from "@rollup/pluginutils";
import type { ParserPlugin, ParserOptions } from "@babel/parser";
import type { TransformOptions } from "@babel/core";
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.
* @default false
*/
devtoolsInProd?: boolean;
/**
* RegExp or glob to match files to be transformed
*/
include?: FilterPattern;
/**
* 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/],
);
const jsxPlugin: Plugin = {
name: "vite:preact-jsx",
enforce: "pre",
config() {
return {
optimizeDeps: {
include: ["preact/jsx-runtime"],
},
};
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
async transform(code, url) {
// Ignore query parameters, as in Vue SFC virtual modules.
const { id } = parseId(url);
if (!shouldTransform(id)) return;
const parserPlugins = [
"importMeta",
// This plugin is applied before esbuild transforms the code,
// so we need to enable some stage 3 syntax that is supported in
// TypeScript and some environments already.
"topLevelAwait",
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
!id.endsWith(".ts") && "jsx",
/\.tsx?$/.test(id) && "typescript",
].filter(Boolean) as ParserPlugin[];
const result = await transformAsync(code, {
...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"
// : "@babel/plugin-transform-react-jsx-development",
// {
// runtime: "automatic",
// importSource: "preact",
// },
// ],
...(config.isProduction ? [] : ["babel-plugin-transform-hook-names"]),
],
sourceMaps: true,
inputSourceMap: false as any,
});
// NOTE: Since no config file is being loaded, this path wouldn't occur.
if (!result) return;
return {
code: result.code || code,
map: result.map,
};
},
};
return [
{
name: "preact:config",
config() {
return {
resolve: {
alias: {
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
react: "preact/compat",
},
},
};
},
},
jsxPlugin,
preactDevtoolsPlugin({ injectInProd: devtoolsInProd, shouldTransform }),
prefresh({ include, exclude }),
];
}