Skip to content
Advertisement

Ignore return outside of function with babel 7

I recently updated to babel 7 and webpack 4 and am receiving this error when running our gulp build task:

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.

This is caused by the return outside of a function in browser-syncs dev-ip dependency.

Is there a way to configure my .babelrc file to ignore this?

I’ve tried the following:

  1. Installing only production dependencies, but because browser sync is imported in my gulp file it’s still being compiled
  2. Setting up workspaces with yarn, but similar issue as #1
  3. Dynamically importing browser sync in my gulp file, I guess this is not yet supported yet?
  4. Telling babel to ignore or exclude compiling the node_modules folder, but this doesn’t seem to do anything?

Apparently babel-parser has an option allowReturnOutsideFunction: true, but I can’t figure out how to set this in my .babelrc file.

Any thoughts on how to get around this?

Advertisement

Answer

Since I could not find a solution to this, I ended up just forking browser-sync and dev-ip.

I give you, browser-stink

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement