babel-loader:模块构建失败:语法错误:在严格模式下删除局部变量

2024-02-09

我在用着babel-loader in webpack使用自定义 babel-plugin 将一些第三方代码转换为可以顺利通过 Webpack 捆绑器的格式。然而,当我的代码通过 babel 的解析器(babylon)运行来构建 AST 时,我收到以下错误:

Module build failed: SyntaxError: Deleting local variable in strict mode

我在巴比伦找到了触发此消息的行:https://github.com/babel/babylon/blob/master/src/parser/expression.js#L236 https://github.com/babel/babylon/blob/master/src/parser/expression.js#L236

看看该代码,似乎我应该能够通过设置来禁用巴比伦中的严格模式解析this.state.strict to false。问题是不知道怎么设置this.state.strict from babel-loader。我希望其他人对此有更多了解。

到目前为止,我已经尝试过以下一些操作:

  1. strict: false and strictMode: false in query

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            strict: false,
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    
  2. strict: false and strictMode: false带插件

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [
                [__dirname + '/babel-plugins/custom-plugin', {strict: false}]
            ]
        }
    }
    
  3. Set state.opts.strict为假Program within custom-plugin.js(但这不应该起作用,因为babylon解析代码并在传递AST进行遍历之前失败)

    module.exports = function (params) {
        return {
            visitor: {
                Program: function (path, state) {
                    state.opts.strict = false;
                }
            }
        };
    };
    
  4. Use blacklist in webpack.config.js and .babelrc(在 babel v6 中被删除,所以这无论如何都不应该工作)

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    

我可以想到一些解决这个问题的办法,但是这个标志应该可以通过表面访问babel-loader or .babelrc以某种形式。


只需更改您的预设即可。这可能会有所帮助。

presets: [
'es2015'
]

to be

presets: [
['es2015', {modules: false}]
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

babel-loader:模块构建失败:语法错误:在严格模式下删除局部变量 的相关文章

随机推荐