Babel Troubleshooting
文章目录

Babel 编译 Typescript: Optional Chaining 报错

1
2
3
4
5
6
7
8
9
10
11
./node_modules/bisheng-formatter-core/dist/formatters/index.js 17:31
Module parse failed: Unexpected token (17:31)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| const biShengFormat = (content, config) => {
> const ellipsisCount = config?.ellipsisCount || 3;
| const useSimpleQuotation = config?.useSimpleQuotation;
| const mainFeature = config?.mainFeature || {


> Build error occurred

几个需要注意的点:

  1. 对于 TS, tsc 本身就可以编译
  2. 如果需要使用 babel 编译 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
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
],
'@babel/preset-typescript'
],
plugins: [
['module-resolver', {
alias: {
'@src': './src',
}
}],
/* 例如: const x = data?.detail?.x
Optional Chaining 是 TS 后期添加的功能
早期的 babel 编译可能需要添加这个插件
否则在 import 的时候会报出 Syntax Error: SyntaxError: 的错误
*/
["@babel/plugin-proposal-optional-chaining"]
],
ignore: [
'**/*.spec.ts'
]
}