Skip to content

Commit 4669ee6

Browse files
authored
修复 Typescript target 导致的问题 (#145)
* fix Typescript target * update samples * update preset config react
1 parent 98758fe commit 4669ee6

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

preset-configs/react.json

+3-23
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
{
2-
"extends": "esnext",
2+
"extends": "default",
33
"transforms": {
4-
"js": {
5-
"transformer": "jsx",
6-
"config": {
7-
"babelOptions": {
8-
"plugins": [
9-
["@babel/plugin-proposal-decorators", { "legacy": true }],
10-
"@babel/plugin-proposal-class-properties"
11-
]
12-
}
13-
}
14-
},
15-
"jsx": {
16-
"transformer": "jsx",
17-
"config": {
18-
"babelOptions": {
19-
"plugins": [
20-
["@babel/plugin-proposal-decorators", { "legacy": true }],
21-
"@babel/plugin-proposal-class-properties"
22-
]
23-
}
24-
}
25-
},
4+
"js": "jsx",
5+
"jsx": "jsx",
266
"svg@js": "svgr",
277
"svg@jsx": "svgr"
288
}

samples

src/webpack/transform.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,22 @@ function addTransform(
246246
transform.transformer === Transform.Tsx
247247
)
248248
const compilerOptions = {
249-
// 这里设置为 ESNext(最新的规范能力),进一步的转换由 babel 处理
250-
target: 'ESNext',
251-
// enable tree-shaking,由 webpack 来做 module 格式的转换
252-
module: 'ESNext',
253-
// module 为 ESNext 时,moduleResolution 默认为 Classic(虽然 TS 文档不是这么说的),这里明确指定为 Node
249+
// 这里设置为 ES2020(最新的规范能力),需要注意的是,这里设置 ESNext 可能是不合适的:
250+
//
251+
// > The special ESNext value refers to the highest version your version of TypeScript supports. This setting should be used with caution,
252+
// > since it doesn’t mean the same thing between different TypeScript versions and can make upgrades less predictable.
253+
// > - https://www.typescriptlang.org/tsconfig#target
254+
//
255+
// 这里 Typescript 处理的结果会交给 babel 处理;我们默认使用 @babel/preset-env,预期会支持最新的规范能力
256+
// 然而我们使用的 Typescript 跟 babel (& @babel/preset-env) 行为之间可能会有 gap:
257+
// 以 babel-plugin-proposal-class-properties 为例,在对应的 proposal 进入 stage 4 后,
258+
// Typescript 会认为以 ESNext 为目标时,对应的语法不再需要转换;
259+
// 而如果 builder 此时依赖了相对更新的 Typescript 版本,以及相对较旧的 babel (& @babel/preset-env) 版本
260+
// 那么这里对 class properties 语法的支持就会有问题(Typescript & babel 都不会对它进行转换)
261+
target: 'ES2020',
262+
// 跟 target 保持一致,而不是设置为 CommonJS;由 webpack 来做 module 格式的转换以 enable tree shaking
263+
module: 'ES2020',
264+
// module 为 ES2020 时,moduleResolution 默认为 Classic(虽然 TS 文档不是这么说的),这里明确指定为 Node
254265
moduleResolution: 'Node'
255266
}
256267
const tsLoaderOptions = {

0 commit comments

Comments
 (0)