简体   繁体   English

为什么 Babel 7 不编译 node_modules 文件?

[英]Why is Babel 7 not compiling node_modules files?

I have error in IE11 SCRIPT1002: Syntax error (problem with class syntax).我在 IE11 SCRIPT1002 中有错误:语法错误(类语法问题)。 My simple code with 2 lines:我的简单代码有 2 行:

import { struct } from 'superstruct';
console.log('finished');

I wan't that my babel7 compile class into ES5 code我不想我的 babel7 将类编译成 ES5 代码

I have tried write .babelrc file :我试过写 .babelrc 文件:

 {
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "ie": "11"
        }
      }
    ]
  ]
}

and https://babeljs.io/docs/en/babel-plugin-transform-classes haven't fixed toohttps://babeljs.io/docs/en/babel-plugin-transform-classes也没有修复

Update : I have tried use @babel/plugin-preset-es2015 which convert class in ES5 code but this package is deprecated in babel7更新:我尝试使用 @babel/plugin-preset-es2015 来转换 ES5 代码中的类,但是这个包在 babel7 中已被弃用

Help me please请帮帮我

In order to transform node_modules and child packages in Babel 7 you need to use a babel.config.js file instead of a .babelrc file.为了在 Babel 7 中转换 node_modules 和子包,你需要使用babel.config.js文件而不是.babelrc文件。

See this issue comment and the babel documentation on project-wide configuration .请参阅此问题评论项目范围配置的 babel 文档。 Specifically具体来说

New in Babel 7.x, Babel has as concept of a "root" directory, which defaults to the current working directory. Babel 7.x 中的新功能,Babel 具有“根”目录的概念,默认为当前工作目录。 For project-wide configuration, Babel will automatically search for a "babel.config.js" in this root directory.对于项目范围的配置,Babel 会自动在这个根目录中搜索“babel.config.js”。

... ...

Because project-wide config files are separated from the physical location of the config file, they can be ideal for configuration that must apply broadly, even allowing plugins and presets to easily apply to files in node_modules or in symlinked packages, which were traditionally quite painful to configure in Babel 6.x.由于项目范围的配置文件与配置文件的物理位置分开,因此它们非常适用于必须广泛应用的配置,甚至允许插件和预设轻松应用于 node_modules 或符号链接包中的文件,这在传统上是非常痛苦的在 Babel 6.x 中配置。

The short of it is that .babelrc is used for a local project file transformations (not including node_modules ) while babel.config.js should be considered project-wide and will apply to package dependencies when bundling ( node_modules ).简而言之, .babelrc用于本地项目文件转换(不包括node_modules ),而babel.config.js应该被视为项目范围的,并且在捆绑时将应用于包依赖项( node_modules )。 It's a bit confusing but hopefully that helps!这有点令人困惑,但希望能有所帮助!

Edit编辑

Here's a bit more information on a full project config to build your example file using webpack.这是有关使用 webpack 构建示例文件的完整项目配置的更多信息。 Note that if you use .babelrc instead of babel.config.js here it will not work.请注意,如果您在此处使用.babelrc而不是babel.config.js它将无法工作。 Running webpack-cli produces a script script.out.js that does not use the class keyword.运行webpack-cli会生成一个不使用 class 关键字的脚本script.out.js

script.js 脚本.js
module.exports = {
    entry: './script.js',
    output: {
        path: __dirname,
        filename: 'script.out.js',
    },
    module: {
        rules: [ {
            test: /\.m?js$/,
            use: {
                loader: 'babel-loader'
            }
        } ]
    }
}
babel.config.js babel.config.js
 module.exports = { "presets": [ [ "@babel/preset-env", { "targets": { "ie": "11" } } ] ] };
webpack.config.js webpack.config.js
 module.exports = { entry: './script.js', output: { path: __dirname, filename: 'script.out.js', }, module: { rules: [ { test: /\\.m?js$/, use: { loader: 'babel-loader' } } ] } }
Package Dependencies 包依赖
"@babel/core": "^7.3.4", "@babel/preset-env": "^7.3.4", "babel-loader": "^8.0.5", "superstruct": "^0.6.0", "webpack-cli": "^3.2.3"

The cause for me was, in the webpack config, only checking for js files eg test: /\\.js$/ as I only had js files in the codebase.我的原因是,在 webpack 配置中,只检查 js 文件,例如test: /\\.js$/因为我在代码库中只有 js 文件。

What I didn't realize is some of the node_modules were .mjs files, and were being ignored by babel completely.我没有意识到一些 node_modules 是 .mjs 文件,并且被 babel 完全忽略了。

The fix was to use test: /\\.m?js$/ in the webpack config to include .mjs files.修复方法是在 webpack 配置中使用test: /\\.m?js$/来包含 .mjs 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM