-
Notifications
You must be signed in to change notification settings - Fork 21
exports is not defined (jspm bundles, Angular html template import in typescript - commonjs) #47
Comments
Thanks for posting... this seems to be specifically related to the TypeScript plugin itself, where SystemJS builder thinks the format of the output is still an ES module and transpiles it, while the TypeScript plugin has already transpiled into CommonJS. I'd suggest trying posting an issue on the TypeScript plugin project, and feel free to copy me in there. |
Looking at the source code, the text plugin is the one setting the format to ES. The Typescript plugin isn't touching the file. When I change the text plugin to set the format to CJS, everything is working fine. |
If the TypeScript plugin is configured as the |
Indeed, I spent some time trying reproduce this yesterday. One issue seems to be that plugin-typescript overwrites the It's also worth noting that it's not simply the text plug-in that is affected difference in behavior between running bundled vs. unbundled. If you tell TypeScript to output es2015 modules, enabling Rollup optimizations, transpiling in the browser fails immediately, but bundling works perfectly. My understanding is that SystemJS invokes the As you say this seems to be a plugin-typescript specific issue but I wanted to make note of it here. @MoonStorm do you plan to open an issue with plugin-typescript? Feel free to copy me on it as well. |
The problem is that when outputting commonjs typescript will always transpile
to
but plugin-text outputs the string as an amd default export when running the browser and as an esm default export when bundling. This makes interoperability rather difficult. I would suggest changing the amd mode to output the string as the 'default' property and setting the esModule flag, ie.
This will mean that the in browser behaviour and bundler behaviour are equivalent. |
Unfortunately the above solution breaks when using vanilla commonjs :(. I don't see an easy solution to this. @MoonStorm can I ask why you have typescript configured to output commonjs instead of system format? |
@frankwallis all of Angular 2's in browser demos, plunkrs, and whatnot, have been using this setting for over a year, resulting in endless warnings. I have no idea why as register is clearly preferable. |
@frankwallis I have attemped twice to switch to system module format. There is always some dependency that's not playing nice. I've wasted a lot of time trying to make this work. |
@frankwallis @aluanhaddad thanks for the updates, yeah TypeScript not supporting the I guess we should really be evangelising the system output from TypeScript then much more? |
@guybedford as of 2.2 TypeScript does add the marker when transpliling to CommonJS but the problem is that Angular hasn't upgraded yet so I don't think their CommonJS distributions have it. More broadly, I don't think TypeScript adds it when transpliling to AMD or UMD. I can open an issue on the TypeScript repo for that. |
@MoonStorm which libraries are you having trouble with? let me know and I'll be happy to give you a hand and see if we can add whatever changes may be appropriate to the jspm registry. |
@aluanhaddad nice, that's good to know. |
@aluanhaddad Thanks for offering to help but I'm not going to switch any time soon as the waters are still way too muddy. After spending days applying fixes left and right I remember that the last drop was when expanding I'll be using my custom |
@MoonStorm glad you have a solution. @guybedford, @frankwallis if System.register is the only supported format then we should clarify that as there are millions of plunkrs that use CommonJS with TypeScript and SystemJS. Something interesting to note is that in TypeScript 2.3 there will be a custom transformations API. I haven't looked into it in detail yet but it might allow plugin-typescript to work better as a transpiler plugin (vs as a loader) in workflows that use |
@aluanhaddad it could be worth getting the main examples of the tool changed to the system pattern if that is preferable. But if the next TypeScript version will add the Good to know about the transformations API as well! |
When using this plugin to import html files with an import statement in typescript, set to produce commonjs modules, I'm getting
exports is not defined
, in the browser, from the bundles created viajspm
. The output looks something similar to:No problem when transpiling directly in the browser though.
I changed the plugin to
and nothing complains any more. The generated js through the bundle looks like:
Also notice the difference between using
registerDynamic
vsregister
.Any idea what's causing this?
The text was updated successfully, but these errors were encountered: