【问题标题】:Babel 6 presets specified in .babelrc not working.babelrc 中指定的 Babel 6 预设不起作用
【发布时间】:2015-11-21 21:25:19
【问题描述】:

正如标题所暗示的,基本上根据文档,对于新的 Babel 6,我们现在应该传入插件/预设,因为默认情况下它不会对我们的代码做任何事情。

所以我在我的项目目录中创建了一个 .babelrc 文件,其中包含以下内容(就像在文档中一样)

{
  "presets": ["es2015"]
}

但是这行不通。 由于我使用的是 webpack 和 babel-loader,我遇到了一个不同的答案,建议在 webpack 配置中放置类似的内容:

{
     test: /\.js$/, exclude: /node_modules/, loader: "babel", query: {
         presets: ["es2015"]
     }
}

这行得通。 所以我的问题是,这是否是新 Babel 中的错误,还是我遗漏了一些明显错误的东西?我以前用过 Babel 5 和 Webpack,可以在 .babelrc 中指定 babel config 没问题...

提前致谢

编辑:该问题仅在 babel 加载程序之前运行 eslint 加载程序时发生。但是刚刚更新到最新的 babel-loader 6.2.0,一切都恢复正常了。

    module: {
        preLoaders: [
            { test: /\.js$/, exclude: /node_modules/, loader: "eslint"}
        ],
        loaders: [
            { test: /\.js$/, exclude: /node_modules/, loader: "babel"},
            { test: /\.css$/, exclude: /node_modules/, loader: "style!css!postcss"}

【问题讨论】:

    标签: javascript webpack babeljs


    【解决方案1】:

    babel-loader 似乎有问题。它应该在版本6.1.0 中修复。

    你可以看到release/v6.1.0摘要:

     * release/v6.1.0:
     Update CHANGELOG.md and package.json
     Set source file name relative to options.sourceRoot
     Allow babelrc to be specified for cache purposes
     Add BABEL_ENV || NODE_ENV to default cacheIdentifier
    

    所以更新 babel-loader 就足够了。

    【讨论】:

    • 谢谢,我刚刚更新到 6.1.0,但是问题仍然存在,出现相同的“意外令牌”错误。 (将预设放回 webpack 配置中仍然可以按预期工作)
    • @luanped 除了babel,您还在使用其他加载器吗?你能发布你的webpack.config.js吗?
    • 嗨@dreyescat,在问题中添加了配置文件。感谢您的帮助
    • @luanped 我怀疑的:)。我还没弄清楚为什么......但我不知道为什么在混合中引入eslint 会导致这个错误。只需删除eslint-loader,您就会看到它与.babelrc 一起使用。我并不是建议将此作为解决方案,而只是指出问题可能来自哪里。
    • 非常感谢@dreyescat,确实可以让它工作。那么你知道这是 Babel 还是 Webpack 的错误吗?再次感谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 2017-02-15
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2022-07-23
    相关资源
    最近更新 更多