Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel踩坑记录 #5

Open
flytam opened this issue Apr 10, 2019 · 0 comments
Open

babel踩坑记录 #5

flytam opened this issue Apr 10, 2019 · 0 comments

Comments

@flytam
Copy link
Owner

flytam commented Apr 10, 2019


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

@flytam flytam added the Go label Nov 16, 2019
@flytam flytam added 工程化 and removed Go labels Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant