babel: Bug: @babel/transform-modules-commonjs: loose does not work

This and this claims that:

As per the spec, import and export are only allowed to be used at the top level. When in loose mode these are allowed to be used anywhere.

However, not with babel 6 or babel 7 will it work.

Input Code

function test() {
	import 'which'
}

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
    "presets": ["@babel/es2015"],
    "plugins": [
      ["@babel/transform-modules-commonjs",  { "loose": true }],
      "@babel/syntax-object-rest-spread"
    ]
}

Expected Behavior

It should compile.

Current Behavior

SyntaxError: ‘import’ and ‘export’ may only appear at the top level

Your Environment

software version(s)
Babel 7.0.0-beta.33
node 8.6.0
Operating System linux

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (17 by maintainers)

Commits related to this issue

Most upvoted comments

@perinikhil PR is in. Thanks again!

On Sat, Dec 16, 2017 at 10:49 AM, Peri Nikhil notifications@github.com wrote:

@abianco3 https://github.com/abianco3 I’m the one who should apologise as i jumped the gun and raised a PR while you were still working on it… 😅 I’d be happy to see a contributor badge next on your name as penance 😌 so do raise a PR 😇

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/babel/babel/issues/6954#issuecomment-352203124, or mute the thread https://github.com/notifications/unsubscribe-auth/AOLJDFa5EOVY3R6p5Bdy-HO_F2dr-q5eks5tBBDPgaJpZM4QzE6e .

– Software Engineer linkedin https://www.linkedin.com/in/abianco3/ github https://www.github.com/abianco3

From what I understand the line that reads “As per the spec, import and export are only allowed to be used at the top level. When in loose mode these are allowed to be used anywhere.” needs to be simply removed from the docs. I’ve been working my way through the source code to make sure I’m not missing some functionality that should replace this line.