-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
51 lines (43 loc) · 1.04 KB
/
plugin.js
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
import { transformSync } from "@babel/core";
import babelGlimmerPreset from "@glimmerx/babel-preset";
const scriptFileRegex = /\.(ts|js)$/;
export default function vitePluginGlimmerX(
plgOptions
) {
let viteConfig;
return {
name: 'vite:glimmerx',
enforce: 'pre',
configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},
transform(rawCode, id) {
let code = rawCode;
if (!scriptFileRegex.test(id)) {
return;
}
return {
code: transformSrcCode(code, id, plgOptions, viteConfig),
map: null,
};
},
};
}
function transformSrcCode(code, fileName, plgOptions, viteConfig) {
let presets = [function (api, opts) {
return babelGlimmerPreset(api, {
...opts, ...{
isDebug: !viteConfig.isProduction
}
})
}];
let result = transformSync(code, {
sourceType: "module",
babelrc: false,
configFile: false,
envName: viteConfig.mode,
filename: fileName,
presets
});
return result.code;
}