我正在使用babel-loader自webpack定义 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.strict从babel-loader. 我希望其他人对此有更多了解。
以下是我迄今为止尝试过的一些事情:
strict: false并且strictMode: false在query{ test: /\.js$/, include: /bower_components/, //only thirdparty loader: 'babel', query: { strict: false, plugins: [__dirname + '/babel-plugins/custom-plugin'] } }strict: false并strictMode: false带有插件{ test: /\.js$/, include: /bower_components/, //only thirdparty loader: 'babel', query: { plugins: [ [__dirname + '/babel-plugins/custom-plugin', {strict: false}] ] } }在内部设置
state.opts.strict为 false (但这不应该工作,因为巴比伦解析代码并在传递 AST 进行遍历之前失败)Programcustom-plugin.jsmodule.exports = function (params) { return { visitor: { Program: function (path, state) { state.opts.strict = false; } } }; };blacklist在webpack.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以某种形式在表面上访问。