8

我正在使用babel-loaderwebpack定义 babel-plugin 将一些第三方代码转换为可以毫无问题地通过 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.strict解析false。问题是我不知道如何this.state.strictbabel-loader. 我希望其他人对此有更多了解。

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

  1. strict: false并且strictMode: falsequery

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

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

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

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

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

4

1 回答 1

0

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

presets: [
'es2015'
]

成为

presets: [
['es2015', {modules: false}]
]
于 2021-02-21T22:44:46.210 回答