javascript - 使用 babel 7 忽略函数外部的返回

标签 javascript webpack gulp babeljs

我最近更新到 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 文件以忽略此问题?

我尝试过以下方法:

  1. 仅安装生产依赖项,但由于浏览器同步已导入到我的 gulp 文件中,因此仍在编译
  2. 使用 yarn 设置工作区,但问题与 #1 类似
  3. 在我的 gulp 文件中动态导入浏览器同步,我猜这尚不支持?
  4. 告诉 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/

相关文章:

webpack - 使用 webpack 动态请求 vendor 模块

gulp - 如何将参数传递给 gulp-watch 调用的任务

javascript - 从 Vue 应用程序外部调用 Vue 方法 (Vue Webpack CLI)

reactjs - [Webpack][React] 在带有代码拆分的 SSR 上,我如何获得页面所需的 block 列表?

javascript - 如何使用 webpack 在 Angular 应用程序中动态加载 Assets (pdf)?

css - Gulp-sass 不会将 scss 文件编译为 css,而是将所有文件从 scss 文件夹复制到 css 文件夹

javascript - 如何在 javascript 函数中运行一个又一个 gulp 函数?

javascript - AngularJS 服务的问题

JavaScript;用新行分割字符串

javascript - Node.js 无法读取未定义的属性 'on'