Skip to content

Suggestion: Module code generation UMD #2036

Description

@codler

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.

Activity

  1. danquirk commented on Feb 16, 2015

    @danquirk
    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?

  2. codler commented on Feb 16, 2015

    @codler
    Author

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

  3. coreh commented on Feb 17, 2015

    @coreh

    6to5 Babel also supports emitting UMD code:

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

  4. fdecampredon commented on Feb 17, 2015

    @fdecampredon

    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.

  5. csnover commented on Feb 20, 2015

    @csnover
    Contributor

    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 😴 ]

  6. fdecampredon commented on Feb 20, 2015

    @fdecampredon

    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.

  7. csnover commented on Feb 20, 2015

    @csnover
    Contributor

    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.

  8. pocesar commented on Apr 23, 2015

    @pocesar

    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)

  9. mhegazy commented on Apr 23, 2015

    @mhegazy
    Contributor
  10. bryanforbes commented on Apr 23, 2015

    @bryanforbes
    Contributor

    Will this be available in 1.5?

  11. mhegazy commented on Apr 23, 2015

    @mhegazy
    Contributor

    This is going to be available in 1.6 1.5

  12. benlesh commented on May 25, 2015

    @benlesh

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

  13. mhegazy commented on May 25, 2015

    @mhegazy
    Contributor

    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.

  14. benlesh commented on May 25, 2015

    @benlesh

    Mohamed Hegazy (@mhegazy) ... so just ---module umd ?

  15. mhegazy commented on May 25, 2015

    @mhegazy
    Contributor

    yup.

  16. Zorgatone commented on Nov 2, 2015

    @Zorgatone

    Wow I just realized I can use UMD now 😄

  17. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions