Skip to content
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

Suggestion: Module code generation UMD #2036

Closed
codler opened this issue Feb 14, 2015 · 16 comments · Fixed by #2605
Closed

Suggestion: Module code generation UMD #2036

codler opened this issue Feb 14, 2015 · 16 comments · Fixed by #2605
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@codler
Copy link

codler commented Feb 14, 2015

Add a third variant umd (Universal Module Definition) in module flag on compiler.

tsc --module umd

This way you can reuse your JS code in both Nodejs and Browser without needing to compile twice (amd and commonjs) and still be able to code modular style (import/export) in TS.

@danquirk
Copy link
Member

We would need a better understanding of how many people are using this module style before doing the work to support it. Do you have any examples of large projects using this?

@danquirk danquirk added the Suggestion An idea for TypeScript label Feb 16, 2015
@codler
Copy link
Author

codler commented Feb 16, 2015

Yes, many large projects are having there own style of umd.
Example

@coreh
Copy link

coreh commented Feb 17, 2015

6to5 Babel also supports emitting UMD code:

https://babeljs.io/docs/usage/modules/

@fdecampredon
Copy link

note that emitting true umd is quite problematic not only you have to grasp the names of modules for the global case (aka 'react' becomes React) but you have to changes ALL the require call with something like derequire to prevents amd loader that parse require from breaking.

@csnover
Copy link
Contributor

csnover commented Feb 20, 2015

note that emitting true umd is quite problematic not only you have to grasp the names of modules for the global case (aka 'react' becomes React) but

Eh? Are you meaning that in order to output a global object you normally need to specify a different variable name for it?

you have to changes ALL the require call with something like derequire to prevents amd loader that parse require from breaking.

Not 100% sure what you are talking about here. There was a bug in a very old version of the example UMD wrapper code, but there is nothing that requires a UMD module to do factory scanning.

[edits: sorry, it’s way too late for me to be posting things on github 😴 ]

@fdecampredon
Copy link

Not 100% sure what you are talking about here. There was a bug in a very old version of the example UMD wrapper code, but there is nothing that requires a UMD module to do factory scanning.

Some AMD loader scans your file for require call and try to look for dependencies based on that scanning so you have to derequire your file in some case.

Eh? Are you meaning that in order to output a global object you normally need to specify a different variable name for it?

Yup exactly UMD also manages global objects so you'll have to provide the module with 2 names.

@csnover
Copy link
Contributor

csnover commented Feb 20, 2015

Yup exactly UMD also manages global objects so you'll have to provide the module with 2 names.

Maybe TS can get away with outputting a UMD lite format that is just CJS-or-AMD, since the compiler already will not let you export external modules if you don’t specify a module format? I think that’s really what people want anyway, just something that works with a browser module loader and works with the built-in Node.js loader, so we can get away from the stupidity of having to either load an AMD loader into Node.js to load AMD-format modules, or run Browserify to mostly-load CJS-format modules in a browser, or double compilation for each platform.

@pocesar
Copy link

pocesar commented Apr 23, 2015

nice timing. I'm having to create a manual header and footer on the compiled .js file that wraps the UMD header and the factory function at the bottom. (and add a module.exports hack, setting module to commonjs)

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 23, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Apr 23, 2015

Thanks @csnover

@bryanforbes
Copy link
Contributor

Will this be available in 1.5?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 23, 2015

This is going to be available in 1.6 1.5

@benlesh
Copy link

benlesh commented May 25, 2015

When is 1.6 alpha/beta coming? I'd really like this feature for the redevelopment of RxJS 3...

@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2015

Correction it should be available in the next public release for 1.5; you can use the latest from master or release-1.5 as well.

@benlesh
Copy link

benlesh commented May 25, 2015

@mhegazy ... so just ---module umd ?

@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2015

yup.

@Zorgatone
Copy link

Wow I just realized I can use UMD now 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants