`import()` argument shouldn't be evaluated in the arrow scope
See original GitHub issueReproduction
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:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.That’s a great catch and seems like a very trivial fix. Want to submit a PR?