In webpack 1 docs is statement that in webpack 2 will use System.import()
for dynamic require:
Luckily, there is a JavaScript API “loader” specification being written to handle the dynamic use case:
System.load
(orSystem.import
). This API will be the native equivalent to the aboverequire
variations.
And during that time all around the web was examples of using this System.import()
.
Before releasing webpack 2, authors decide to change System.import()
to import()
:
add
import()
as Code Splitting construct. It should be used instead ofSystem.import
when possible.System.import
will be deprecated in webpack 2 release (removed in webpack 3) as it's behavior is incorrect according to the spec.
This import()
is based on tc39/proposal-dynamic-import specification, and you can read more why they made this change here.
Can someone explain difference between System.import()
and import()
?
Despite different name, usage looks the same:
import(modulePath)
.then(module => module.default())
.catch(/* ... */);
System.import(modulePath)
.then(module => module.default())
.catch(/* ... */);
But in weback 2 doc is: "System.import()
behavior is incorrect according to the spec" - so it suggest that there is difference between System.import()
and import()
.
A system import map is used by the system to find the partner relationship for a document (flat file definition), to determine which import map is used to translate the data. The system import map builds the key that the translator uses to find the partner relationship.
A system is a collection of elements or components that are organized for a common purpose. The word sometimes describes the organization or plan itself (and is similar in meaning to method, as in "I have my own little system") and sometimes describes the parts in the system (as in "computer system").
Require is Non-lexical, it stays where they have put the file. Import is lexical, it gets sorted to the top of the file. It can be called at any time and place in the program. It can't be called conditionally, it always run in the beginning of the file.
The import * as name syntax imports all exported content of a javascript file.
The important part of your first quote is
specification being written
When Webpack 1 implemented System.import
, the spec was still evolving. If fact it still is. Webpack 1 implemented System.import
because that was what was being tossed around as the potential API at the time.
Webpack 2 implements import()
because that is a new proposal to standardize on a syntactic approach rather library-based one.
Here's what you're looking for: tc39 Proposal for Import
An actual function
Drafts of the Loader ideas collection have at various times had actual functions (not just function-like syntactic forms) named System.import() or System.loader.import() or similar, which accomplish the same use cases.
The biggest problem here, as previously noted by the spec's editors, is how to interpret the specifier argument to these functions. Since these are just functions, which are the same across the entire Realm and do not vary per script or module, the function must interpret its argument the same no matter from where it is called. (Unless something truly weird like stack inspection is implemented.) So likely this runs into similar problems as the document base URL issue for the importModule function above, where relative module specifiers become a bug farm and mismatch any nearby import declarations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With