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.

Change System.import() to import()

See original GitHub issue

I thought it would be worth mentioning this point for Webpack 2 consideration. The loader spec strictly specifies System.loader.import as the dynamic import entry point, instead of System.import as it used to be.

The other problem with using System.import is that strictly System.import shouldn’t have any parent knowledge without a second argument. That is you’d really want System.import('x', parentName) to be the contextual require. Assuming System.import is already parent-scoped is going to not be true on the upgrade path at all. In SystemJS, we’ve defined a locally scoped __moduleName variable to allow for the above, but again it ends up being a custom solution.

In reality modules will have a contextual import function syntax available that will automatically scope the import to the current module parent so that this won’t usually be necessary making for a nicer patter in ES modules. Perhaps it could be worth enquiring about that status of that syntax as well.

There might be the scope for a little more freedom to experiment here too. For example trying out one spec suggestion syntax like:

export function dynamicLoad() {
  return import.default('x') // default export (import.namespace being the normal namespace import)
}

which was discussed in https://github.com/whatwg/loader/issues/36#issuecomment-89049563. If the spec does implement it, the upgrade path works. If the spec doesn’t implement it, then it’s just a breaking change to adjust, so it’s a no different scenario than using System.import.

One could even then experiment with another possible syntax like:

export function async dynamicLoad() {
  return await import 'x';
}

And treating that import 'x' as the dynamic import. Would (import 'x').then(...) make sense with that grammar?

At some level it does come down to betting on something somewhat custom, and aiming to minimise the risk and inform users about the situation.

The approach SystemJS is taking here is not trying to polyfill System.loader due to potential conflicts when native implementations eventually land. Instead the idea is to stick with the “old” API of System.import and then double this up with a smooth upgrade path to a best practice non-conflict SystemJS.import, only switching into System.loader.import when we’re sure there won’t be a native conflict.

At least that’s the best approach I’ve been able to come up with so far. If you’ve already considered this not to worry, but just to check the System.import decision should at least be made knowingly to avoid an upgrade path issue like SystemJS now has (although it would be great for SystemJS to be able to share the same convention actually, so I probably should have just shut up here!).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:10
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
jhnnscommented, Sep 12, 2016

Thanks @guybedford for letting us know. This is the kind of collaboration I’m looking for. Most developers experience the upcoming standards through tools like webpack, SystemJS and babel. We should try to provide a friction-less upgrade path as much as possible 😀.

First of all, a promised-based solution for creating split points is very important. require.ensure (which was designed after an extension to the original CommonJS) has no way to report failures. Furthermore, it feels weird that you don’t get back the actual modules, but still need to require it. That’s why we need a new API for this.

But shipping an outdated spec knowingly feels also very wrong to me. This can be pretty confusing for developers. And since webpack@2 is still in beta, it’s ok to change that until we remove the beta label.

I really like this proposal and it seems to be very up-to-date – which can be good and bad at the same time. Good, because it incorporates already some of the insights. Bad, because it has no broad support yet. If I understood it correctly it basically looks like this:

import("something").then(namespace => {
    namespace.someNamedExport;
    namespace.default;
});

Calling import() would be a syntax extension similiar to new.target and super() were a familiar notation is used for stuff you can’t do with ordinary properties and functions. In this case, the context of the importing module is passed to import() under the hood without the developer needing to specify it. @guybedford Can you confirm my assumption?

Unfortunately, import() is no valid JavaScript for current parsers. That’s why webpack will throw an error when encountering it. I’m not sure if we can extend acorn’s parser to support import()?


This leaves us with two options:

  • Stick to System.import().
    • Pro: Same syntax as SystemJS (is that correct?). People can easily switch between tools.
    • Contra: Outdated.
    • Contra: Still has the notion of being something official. This can be very confusing for other developers who are not familiar with the spec.
  • Change the API of require.ensure(). Since webpack is the only popular implementation of require.ensure(), this is already conceived as webpack-specific syntax.
    • Pro: Can be easily integrated into webpack
    • Pro: Almost like the old implementation, but it returns a promise.
    • Pro: Is easily recognized as not an official thing
    • Contra: Looks uglier 😃

Or does someone have a different solution?

2reactions
guybedfordcommented, Sep 13, 2016

As soon as the next TC39 meeting confirms the above import syntax which I believe is on the agenda, then it can be integrated into parsers and supported. It looks like this is the way things will be going now, and we can likely expect confirmation at the end of this month.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between System.import() and import()?
import() . Before releasing webpack 2, authors decide to change System. import() to import() : add import() as Code Splitting construct.
Read more >
5. The import system — Python 3.11.1 documentation
Changed in version 3.10: Use of find_module() by the import system now raises ImportWarning . 5.4. Loading¶. If and when a module spec...
Read more >
Learn Exercise: Using System.import() – Webpack 2 Deep Dive
In this exercise, you will use the ES6 System.import API to implement code splitting. ... The "Exercise: Using System.import()" Lesson is part of...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings which are exported by another module. The imported bindings are ...
Read more >
How to Dynamically Import JavaScript with Import Maps
But by default most build systems will bundle all the code into one ... Import maps won't completely replace build tools that perform...
Read more >

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