This repository was archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompilation.ts
150 lines (134 loc) · 3.92 KB
/
compilation.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
import path from 'path';
import { createRequire } from 'module';
import { transform, type Config as SwcConfig } from '@builder/swc';
import type { UnpluginOptions } from 'unplugin';
import lodash from '@builder/pack/deps/lodash/lodash.js';
import type { Config } from '@ice/types';
const { merge } = lodash;
type JSXSuffix = 'jsx' | 'tsx';
interface Options {
rootDir: string;
mode: 'development' | 'production' | 'none';
isServer?: boolean;
sourceMap?: Config['sourceMap'];
}
const require = createRequire(import.meta.url);
const regeneratorRuntimePath = require.resolve('regenerator-runtime');
const compilationPlugin = (options: Options): UnpluginOptions => {
const { rootDir, sourceMap, mode, isServer } = options;
const dev = mode !== 'production';
return {
name: 'compilation-plugin',
// @ts-expect-error TODO: source map types
async transform(source: string, id: string) {
// TODO specific runtime plugin name
if ((/node_modules/.test(id) && !/[\\/]runtime[\\/]/.test(id))) {
return;
}
const suffix = (['jsx', 'tsx'] as JSXSuffix[]).find(suffix => new RegExp(`\\.${suffix}?$`).test(id));
if (!suffix) {
return;
}
const programmaticOptions = {
filename: id,
sourceMaps: !!sourceMap,
...getSwcTransformOptions({ suffix, rootDir, dev, isServer }),
};
// auto detect development mode
if (mode && programmaticOptions.jsc && programmaticOptions.jsc.transform &&
programmaticOptions.jsc.transform.react &&
!Object.prototype.hasOwnProperty.call(programmaticOptions.jsc.transform.react, 'development')) {
programmaticOptions.jsc.transform.react.development = mode === 'development';
}
const output = await transform(source, programmaticOptions);
const { code, map } = output;
return { code, map };
},
};
};
function getSwcTransformOptions({
suffix,
rootDir,
dev,
isServer,
}: {
suffix: JSXSuffix;
rootDir: string;
dev: boolean;
isServer?: boolean;
}) {
const baseReactTransformConfig = {
refresh: dev && !isServer,
};
const reactTransformConfig = merge(baseReactTransformConfig, hasJsxRuntime(rootDir) ? { runtime: 'automatic' } : {});
const commonOptions: SwcConfig = {
jsc: {
transform: {
react: reactTransformConfig,
legacyDecorator: true,
// @ts-expect-error fix me when @builder/swc fix type error
regenerator: {
importPath: regeneratorRuntimePath,
},
},
externalHelpers: false,
},
module: {
// @ts-expect-error module type only support cjs umd amd, fix me when @builder/swc fix type error
type: 'es6',
noInterop: false,
// webpack will evaluate dynamic import, so there need preserve it
ignoreDynamic: true,
},
env: {
loose: true,
},
};
if (isServer) {
commonOptions.env.targets = 'node >= 12';
}
const jsOptions = merge({
jsc: {
parser: {
jsx: true,
dynamicImport: true,
functionBind: true,
exportDefaultFrom: true,
exportNamespaceFrom: true,
decorators: true,
},
},
}, commonOptions);
const tsOptions = merge({
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
decorators: true,
dynamicImport: true,
},
},
}, commonOptions);
if (suffix === 'jsx') {
return jsOptions;
} else if (suffix === 'tsx') {
return tsOptions;
}
return commonOptions;
}
function hasJsxRuntime(rootDir: string) {
try {
// auto detect of jsx runtime
// eslint-disable-next-line
const tsConfig = require(path.join(rootDir, 'tsconfig.json'));
if (tsConfig?.compilerOptions?.jsx !== 'react-jsx') {
return false;
}
// ensure react/jsx-runtime
require.resolve('react/jsx-runtime');
return true;
} catch (e) {
return false;
}
}
export default compilationPlugin;