Babel TroubleshootingBabel, NodeJS

Babel 编译 Typescript: Optional Chaining 报错

./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, 就需要进行设置, 然后
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'
  ]
}
Powered by Remix
|
Designed by szhshp
|
Copyright © szhshp 2022