question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import()` argument shouldn't be evaluated in the arrow scope

See original GitHub issue

Reproduction

var babelPluginDynamicImportNode = require("babel-plugin-dynamic-import-node");
var babel = require("@babel/core");

var input = `
let i = 0;
import("./file-" + i++ + ".js");
assert(i === 1);

async function fn() {
  await import(await getSource());
}
`

var out = babel.transformSync(input, {
  configFile: false,
  plugins: [babelPluginDynamicImportNode]
});

console.log(out.code);

Output

let i = 0;
Promise.resolve().then(() => _interopRequireWildcard(require(`${"./file-" + i++ + ".js"}`)));
assert(i === 1);

async function fn() {
  await Promise.resolve().then(() => _interopRequireWildcard(require(`${await getSource()}`)));
}

As you can see in the output code, the first assertion fails because i++ is evaluated asynchronously, and the second one is a syntax error because await is inside a non-arrow function.

Proposed output

We can pass the import argument in Promise.resolve, so that it is in the correct scope:

let i = 0;
Promise.resolve(`${"./file-" + i++ + ".js"}`).then(_ => _interopRequireWildcard(require(_)));
assert(i === 1);

async function fn() {
  await Promise.resolve(`${await getSource()}`).then(_ => _interopRequireWildcard(require(_)));
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Nov 24, 2020

The solution is “don’t use this plugin with webpack”: webpack supports dynamic imports (since version 3 I think), so you don’t need to transpile them.

Also this plugin wasn’t meant to be used with webpack in the first place (hence -node in the name): there was https://github.com/airbnb/babel-plugin-dynamic-import-webpack which is the equivalent one but for Webpack.

1reaction
ljharbcommented, Apr 19, 2020

That’s a great catch and seems like a very trivial fix. Want to submit a PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Official information on `arguments` in ES6 Arrow functions?
Arrow functions do not have an own arguments binding in their scope; no arguments object is created when calling them.
Read more >
eval() - JavaScript - MDN Web Docs - Mozilla
The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
Read more >
Linter rules - Dart programming language
Avoid relative imports for files in lib/ . This rule is available as of Dart 2.0.0. Rule sets: core, recommended, flutter. This rule...
Read more >
Understanding Variables, Scope, and Hoisting in JavaScript
This tutorial covers what variables are, how to declare and name them, and also take a closer look at the difference between var,...
Read more >
Airbnb JavaScript Style Guide()
7.5 Never name a parameter arguments . This will take precedence over the arguments object that is given to every function scope. //...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found