babel踩坑記錄

1、如果子模塊和外部模塊都有.babelrc時,babel處理子模塊時,是使用的子模塊的babelrc處理的。本質上是文件查找babelrc是從當前文件向上查找的,因爲使用到子倉庫的原因很容易把這個問題忽略瞭解決方法:

外部模塊不使用babelrc,而是使用babel.config.js文件代替。如

module.exports = function(api) {
  api.cache(true)

  const presets = [
    [
      '@babel/preset-env',
      {
        modules: false
      }
    ],
    '@babel/preset-react',
    '@babel/preset-typescript'
  ]
  const plugins = ['@babel/plugin-proposal-class-properties']

  return {
    presets,
    plugins
  }
}

2、如果babel使用了@babel/preset-typescript插件,自己寫的babel轉換插件,需要手動指定babel文件位置

    const result = babel.transform(source, {
      plugins: [
        {
          visitor: {
...
          }
        }
      ],
      filename: './babel.config.js'
    })
   ...

不然就會報錯Error: [BABEL] unknown: Configuration contains string/RegExp pattern, but no filename was passed to Babel

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章