5

I know browserify can consume UMD modules by means of transforms, but when I want to build a lib using browserify, how can I build an UMD module? Is there any transform I can use?

hjl
  • 2,794
  • 3
  • 18
  • 26
  • What does "browserify can consume UMD modules by means of transforms" mean? – JMM May 11 '15 at 21:03
  • `deamdify` like this transform allows you to use AMD module with browserify – hjl May 12 '15 at 01:54
  • Right, but you shouldn't need any transform to use a *UMD* module with browserify, although you may need to `noParse` it. – JMM May 12 '15 at 12:06

2 Answers2

4

If you want to build a UMD module with browserify use the standalone option. Like:

browserify('./entry.js', {
  standalone: 'moduleName',
})
JMM
  • 26,019
  • 3
  • 50
  • 55
  • isn't it that `standalone` exposes the module to global namespace? – hjl May 12 '15 at 01:53
  • 2
    `standalone` creates a UMD module. If the code doesn't detect that it's in an AMD or CommonJS environment, it sets a global variable. – JMM May 12 '15 at 12:05
4

From the CLI: browserify -s NameOfModule

This creates a standalone module exposed under the name of your choosing.

Utilize by loading the output file in a browser and access via window.NameOfModule, or see RequireJS docs on how to use it that way.

Dean Radcliffe
  • 2,194
  • 22
  • 29