-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix build for es modules in browser #3087
Conversation
rollup.config.js
Outdated
@@ -12,7 +13,11 @@ const config = { | |||
if (env === 'es' || env === 'cjs') { | |||
config.output = { format: env, indent: false } | |||
config.external = ['symbol-observable'] | |||
config.paths = env === 'es' ? { | |||
'symbol-observable': 'https://unpkg.com/symbol-observable?module' | |||
} : config.paths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.paths
is always undefined here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For commonJS build yes, it is, but it was it before so it is not doing anything new. Anyway, might be clearer to have something like
if (env === 'es') config.paths = { 'symbol-observable': 'https://unpkg.com/symbol-observable?module' }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@YerkoPalma Can we use that form? It'll be easier to understand for most folks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem 👍
@timdorr @TimvdLippe updated according to reviews. Thanks :) |
How does something like Webpack handle these things? The URL and its structure is arbitrary, so it can't know to transform it to a 'symbol-observable' module. Does this work when used in a non-module-aware browser (IE11, for instance)? |
I would say that an ES modules is only supposed to be loaded by browsers which support modules. For IE11 you need to bundle anyway, so you can keep on using the |
Yes, that's correct. But Webpack and others are module-aware and can convert these import/export statements into standard ES5 code. I would like to know what they do when presented with a URL like this, as this will be the file most people will use (it's default for Webpack, Rollup, and Parcel, at least). |
This is a transparent change for bundlers that transpile to es5, because es5 use commonJS, and the unpkg url is used only for es modules. About the url, that's the official npm cdn repo, and the And like @TimvdLippe said, old browsers would use commonJS build, so non of this matter to those Hope this makes sense. |
Unpkg isn't an official product of npm. Michael Jackson built and maintains it. It's solid and reliable, but it's definitely unofficial and 3rd-party to npm. Recent versions of Webpack and other bundlers will be pulling the ES module version, as it's defined in our package.json and they prefer the |
Well, about the unpkg cdn, I don't know of any official npm cdn, the only thing I know is that some time ago npm changed npmcdn to unpkg, in fact, if you go to https://npmcdn.com it redirects to unpkg. Anyway, if the redux team has some better cdn I'm glad to use that. Now, about the bundlers, using a url is part of the es modules spec, using relative local paths is also part of the spec, using names directly is not. So, if those bundlers are using the module version of a package and not supporting the url spec, then they are implementing non-standard behaviour, and I'm not sure what to do about it. IMHO this PR should stay as is, maybe, as said before, change the cdn for other, but keeping the url as it is regardless of bunflers. |
|
So we should use the raw git version? like https://raw.githubusercontent.com/benlesh/symbol-observable/master/es/index.js In that case, we would be pointing to master branch which is a development branch. If we use the last release tag version we would have to manually update in every future release. Is that ok? |
Hm, the package should just be there in the |
That was the original name of the project, but Michael changed it to avoid confusion (I believe npm may have requested he change it). There is no official npm cdn for anything other than package tarballs. |
I don't really care what URL we use. I care about how it's interpreted by most bundlers. I don't think a URL for the module source is going to work. But I'd like testing done to confirm that. |
OK, build a copy of this PR locally for testing. Parcel interprets it as the ⇶ parcel index.html
Server running at http://localhost:1234
npm ERR! Only absolute URLs are supported
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/timdorr/.npm/_logs/2018-08-13T17_50_33_811Z-debug.log
🚨 /Users/timdorr/forks/redux/es/redux.js:1:25: Failed to install https:.
> 1 | import $$observable from 'https://unpkg.com/symbol-observable?module'; Webpack also can't resolve it: ⇶ webpack-cli index.js
Hash: 0e394ac4988b4f310386
Version: webpack 4.16.5
Time: 342ms
Built at: 08/13/2018 1:53:07 PM
1 asset
Entrypoint main = main.js
[0] ./index.js 41 bytes {0} [built]
[1] ./es/redux.js 28.6 KiB [built]
[2] (webpack)/buildin/global.js 489 bytes [built]
ERROR in ./es/redux.js
Module not found: Error: Can't resolve 'https://unpkg.com/symbol-observable?module' in '/Users/timdorr/forks/redux/es'
@ ./es/redux.js 1:0-70 507:12-24 522:11-23
@ ./index.js So, this won't work as-is. |
@timdorr does Redux really need the Although I appreciate that It's mildly exasperating that Redux transpiles to a purported ES output that is not compatible with the ES spec, as loading Redux as-is in the browser without any compile step results in:
I do agree with your point though about the proposed change in this PR. Relying on a third-party CDN for resolving a very popular JS library of course has its own problems, such as a handful of security concerns. Yet I think we could resolve it by moving the concern of polyfilling to the build process which most people [citation needed] are doing nowadays anyway. Thoughts? |
I thought more about this and I become more and more convinced that this is not a problem Redux should solve. We had the same issue in Polymer and decided that name paths were the way forward, given the way NPM works. See package-community/discussions#2 for more context as well as the following standards proposal: https://github.com/domenic/package-name-maps Therefore, I think it is okay (for the timebeing) to simply publish names rather than paths. CDNs like unpkg already take care of this and, in the case of Polymer, tools like Thus, the only change that this PR should contain is the extra rollup plugin to handle the |
rollup.config.js
Outdated
@@ -12,7 +13,9 @@ const config = { | |||
if (env === 'es' || env === 'cjs') { | |||
config.output = { format: env, indent: false } | |||
config.external = ['symbol-observable'] | |||
if (env === 'es') config.paths = { 'symbol-observable': 'https://unpkg.com/symbol-observable?module' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per #3087 (comment) let's revert this change. We can use unpkg
to make sure names are transformed to paths on the fly.
Previous changes from a run with an older version (I think 5.6.0 or older?)
OK, looking at the output there are two problems:
I'll include the build output below, but it doesn't appear this plugin will work for us. A similar, more minimal technique may be to "ponyfill" just es/redux.jsimport $$observable from 'symbol-observable';
... |
This PR should fix the current module build in production. It make two things
process.env.NODE_ENV
in ES module build #2907)I tried locally and it works, also tests keep green, but I'm used to rollup so it would be cool to double check this solution.