-
Notifications
You must be signed in to change notification settings - Fork 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
Compile TS to native JS modules #2858
Comments
This one requires build target for ES2015 modules, which we started include in #2804 . I think there'll be still some churns to actually able to use it but please give it a shot with 5.5 b. |
I tried again with RxJS 5.5.0 and the only thing that is missing are |
I configured the server to rewrite urls to include the extension and it works! Another problem that remains is that typescript can't find typings for the imports. |
@kwonoj I tried the newer |
From my experience the typings files should be emitted together with JS in the same directory |
I am not using native modules at this moment so bit unclear things to me, please share details if you don't mind.
|
Take a look at the file
Example code: import {Observable} from '../node_modules/@reactivex/rxjs/dist/esm2015/Observable.js' The detected type of It seems that tsc will only look at |
/cc @jasonaden for visibility. Seems this requires postprocessing script. |
I think it would make more sense if such script was implemented as part of build process in typescript itself after all 🤔 |
Maybe worth to ask tsc team as issue? (Or already there is?) |
To quote myself
I actually went to tsc first b/c I thought that's their fault, but from reading the issues I got the feeling they don't want to do that. BUT ... from their replies, I also got the feeling that they didn't quite get what the users wanted and thought 'user fault' too early. An issue raised by RxJS would be treated differently. I don't think that they can get away with compiling to esm2015 but not supporting the browser import loader. I mean the browser loader is by far more important than the node module resolution algo and rollup and the rest of them together. And AFAIK the browser agreed on that they only accept fully qualified names (in the end: URLs) as module names. |
Let's sum up all the ES module quirks again:
(3) is actually the default behavior of TS. (1) and (2) would need a post processing script if TS wont implement it. Post processing script pseudo code: for (every Import statement) {
if (Import points to other package) {
path = find_relative_path_to('node_modules') + path
path += read('package.json')['main'] // read main property from module's package.json
}
if (path points to directory) { path += '/index.js' }
else { path += '.js' }
} |
Friendly ping. Any progress on this? What direction will Rx go? /Cc @benlesh you asked me for a link to this issue. |
@kaste We do not have dedicated plan / progress for this at this moment, as we haven't deeply explored which way we'll go. |
🔮 |
I can take this one. I wrote a babel plugin that does exactly this. Since you do not currently use Babel I imagine that you likely don't want to add it. No worries! I'm happy to write a cli tool that wraps this, so that's all that will need to be added as a devDependency. If this plan sounds fine to the maintainers I am happy to submit a PR. |
I am actually not very opposed to have babel as dep, if it doesn't hurt bulid time too much. In my understanding this would be required only some of pkg (like ESM) only - is that correct? /cc @benlesh for babel side options. + if we're going to use babel, we need to ensure build output shouldn't be mutated other than import path adjustment. |
@kwonoj It would only run that single babel plugin that appends the I understand if you don't want all of the ceremony of adding babel though, so I'm happy to tuck this away into a cli tool to keep things tidy here if that's what you prefer. |
It seems like we should be able to do this with TypeScript or TS transformations. I'll ask around |
@benlesh this seems current discussion proposal: microsoft/TypeScript#16577 |
One arbitrary question: How widely native module import is being used currently? is this something we should tackle immediately, or either something we can wait until TS have conclusions to support it? My initial guess is this probably only runs on latest browser, have limited usage (but I don't write browser target quite lot, so could be wrong) |
@kwonoj I personally need native modules for experimenting with Polymer 3.0 so it's not that important. |
@kwonoj Do you expect a guessing answer here or one that is based on facts? 😺 |
@kwonoj browser support for native modules is pretty good and once FF 60 is out, it will be natively supported everywhere. According to caniuse.com, the current browser support is 68% of all browsers and versions in use, and once FF gets it it will be around 72%, which is pretty major and I am sure it will jump up closer to 80% very soon, meaning a lot of companies will jump over to it. |
v6 exports esm / esm2015 altogether, while mjs is not there but it's not stable so out of scope for this issue I believe. |
I don't see a module bundle in v6. Where is it? I see the umd bundle is in bundles/ but I don't see a modules bundle any where. |
@matthewp in pkg, it have |
Not sure if I understood this correctly, do you expect some other formats then esm output itself? |
Ok, I didn't see those folders. I see them now. Looking at them, they still omit the |
I think this is the right scoped one, right? The OP is showing an example of use in the browser. |
RxJS version: 5.4.3
Code to reproduce:
Expected behavior: There should be a way to load RxJS via native JS module
Actual behavior: Can't access Rx when using JS modules (error:
require
is not defined)The text was updated successfully, but these errors were encountered: