Eslint-plugin-import: Support for dynamic imports

Created on 22 Dec 2016  路  8Comments  路  Source: benmosher/eslint-plugin-import

https://github.com/tc39/proposal-dynamic-import (stage 3)

import(`./foo/${bar}`).then(module => {
    // ...
})

This is currently recognized as invalid syntax: Parsing error: Unexpected token import.

Also probably need to add a rule similar to no-dynamic-require as well.

accepted help wanted rule proposal

Most helpful comment

Thanks. Using babel-eslint resolved my issue. I incorrectly assumed that using the webpack resolver would apply the babel config it contains.

Leaving open as a request to support a new no-dynamic-import rule.

All 8 comments

The babel-eslint parser is required for it to be parsed properly (syntax parsing is not the job of an eslint plugin).

I agree that no-dynamic-import is a good rule to have.

Thanks. Using babel-eslint resolved my issue. I incorrectly assumed that using the webpack resolver would apply the babel config it contains.

Leaving open as a request to support a new no-dynamic-import rule.

just making a note: would also like to add to the moduleVisitor so that static paths imported dynamically (think code splitting) can be linted by no-unresolved, no-absolute-path, etc.

no-dynamic-import is a confusing name since import() is called dynamic import.

Also, why make a new rule for this when we can nest it as an option under no-dynamic-require?

I think it's pretty easy to ban import() with no-restricted-syntax, so we probably don't need a rule for that.

@ljharb Do you mean banning with an expression in it?

const foopath = './foo';
const foo = await import(foopath);

sure, you could do that too.

1093 added import(...) to the moduleVisitor 馃憤馃徎

Was this page helpful?
0 / 5 - 0 ratings