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

Kev*_*iol 9 javascript node.js strict-mode webpack babeljs

我使用babel-loaderwebpack一个自定义巴贝尔,插件改造一些第三方代码,这样通过的WebPack的捆绑,但不在麻烦的格式。但是,当我的代码通过 babel 的解析器 (babylon) 运行以构建 AST 时,出现以下错误:

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

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

看着这些代码,好像我应该能够在巴比伦禁用严格模式解析通过设置this.state.strictfalse。问题是我不知道如何this.state.strictbabel-loader. 我希望其他人对此有更多了解。

以下是我迄今为止尝试过的一些事情:

  1. strict: falsestrictMode: falsequery

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            strict: false,
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. strict: falsestrictMode: false带有插件

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [
                [__dirname + '/babel-plugins/custom-plugin', {strict: false}]
            ]
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. state.opts.strictProgram内部设置为 false custom-plugin.js(但这不应该起作用,因为巴比伦解析代码并在传递 AST 进行遍历之前失败)

    module.exports = function (params) {
        return {
            visitor: {
                Program: function (path, state) {
                    state.opts.strict = false;
                }
            }
        };
    };
    
    Run Code Online (Sandbox Code Playgroud)
  4. blacklistwebpack.config.jsand 中使用.babelrc(已在 babel v6 中删除,因此无论如何这都不起作用)

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

小智 0

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

presets: [
'es2015'
]
Run Code Online (Sandbox Code Playgroud)

成为

presets: [
['es2015', {modules: false}]
]
Run Code Online (Sandbox Code Playgroud)

  • 通过解释,您的回答将会得到改善。 (3认同)