我最近更新到 babel 7 和 webpack 4,并在运行 gulp 构建任务时收到此错误:
gulp build
[00:26:04] Requiring external module @babel/register
[91m[BABEL] Note: The code generator has deoptimised the styling of /node_modules/lodash/lodash.js as it exceeds the max of 500KB.
[0m[91m/node_modules/@babel/core/lib/parser/index.js:95
throw err;
^
SyntaxError: /node_modules/dev-ip/lib/dev-ip.js: 'return' outside of function (41:8)
39 | var out = getIp();
40 | if (!out.length) {
> 41 | return console.log(messages.error);
| ^
42 | }
43 | console.log(getIp("cli"));
44 | }
at Parser.raise (/node_modules/@babel/parser/src/parser/location.js:41:63)
at Parser.parseReturnStatement (/node_modules/@babel/parser/src/parser/statement.js:577:12)
at Parser.parseStatementContent (/node_modules/@babel/parser/src/parser/statement.js:199:21)
at Parser.parseStatement (/node_modules/@babel/parser/src/parser/statement.js:146:17)
at Parser.parseBlockOrModuleBlockBody (/node_modules/@babel/parser/src/parser/statement.js:865:25)
at Parser.parseBlockBody (/node_modules/@babel/parser/src/parser/statement.js:841:10)
at Parser.parseBlock (/node_modules/@babel/parser/src/parser/statement.js:818:10)
at Parser.parseStatementContent (/node_modules/@babel/parser/src/parser/statement.js:223:21)
at Parser.parseStatement (/node_modules/@babel/parser/src/parser/statement.js:146:17)
at Parser.parseIfStatement (/node_modules/@babel/parser/src/parser/statement.js:570:28)
[0m[91merror Command failed with exit code 1.
这是由浏览器同步 dev-ip 依赖项中的函数外部返回引起的。
有没有办法配置我的 .babelrc 文件以忽略此问题?
我尝试过以下方法:
- 仅安装生产依赖项,但由于浏览器同步已导入到我的 gulp 文件中,因此仍在编译
- 使用 yarn 设置工作区,但问题与 #1 类似
- 在我的 gulp 文件中动态导入浏览器同步,我猜这尚不支持?
- 告诉 babel 忽略或排除编译 node_modules 文件夹,但这似乎没有做任何事情?
显然 babel-parser 有一个选项 allowReturnOutsideFunction: true
,但我不知道如何在我的 .babelrc 文件中设置它。
关于如何解决这个问题有什么想法吗?
最佳答案
使用 Babel7,您可以提供所有 parser options (其中包括 allowReturnOutsideFunction
)通过 parserOpts
,例如:
// .babelrc.js
module.exports = {
parserOpts: { allowReturnOutsideFunction: true }
};
关于javascript - 使用 babel 7 忽略函数外部的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996550/