more ... < s c r i p t s r c = " m y _ j a v a s c r i p t _ f i l e . j s " > < / s c r i p t > < s c r i p t s r c = " m o r e _ j a v a s c r i p t . j s " > < / s c r i p t > < s c r i p t s r c = " s p e c i a l _ j a v a s c r i p t _ c o d e . j s " > < / s c r i p t > < s c r i p t s r c = " v e r y _ s p e c i a l _ j a v a s c r i p t _ c o d e . j s " > < / s c r i p t >
x p o r t s . a d d = f u n c t i o n ( a , b ) { r e t u r n a + b ; } ; e x p o r t s . m u l t i p l y = f u n c t i o n ( a , b ) { r e t u r n a * b ; } ; m a i n - m o d u l e . j s v a r m a t h = r e q u i r e ( ' . / m a t h ' ) ; e x p o r t s . c a l c u l a t e = f u n c t i o n ( x , y , z ) { r e t u r n m a t h . a d d ( m a t h . m u l t i p l y ( x , y ) , m a t h . m u l t i p l y ( y , z ) ) ; } ; Hint: Node.js modules = CommonJS modules
r i f y m a i n - m o d u l e . j s - - o u t f i l e b u n d l e . j s - - s t a n d a l o n e m a i n m o d u l e Actually, - - s t a n d a l o n e puts a Universal Module Definition into the bundle ... and can then be used with an AMD loader (RequireJS) ... or falls back to exporting a global variable < s c r i p t s r c = " b u n d l e . j s " > < / s c r i p t > < s c r i p t > < / s c r i p t > m a i n m o d u l e . c a l c u l a t e ( 4 2 , 1 3 , 2 ) ;
Build bundle A with - - r e q u i r e m o d u l e –> exports m o d u l e Build bundle B with - - e x t e r n a l m o d u l e –> uses m o d u l e from bundle A
file contents (brfs) convert AMD modules to CommonJS modules (deAMDify) include bower components in your bundle (debowerify) include component.js components in your bundle (decomponentify) ... many, many more
Node.js and in the browser? 2. Build it with browserify! 3. Let browserify generate a UMD 4. Publish to npm 5. Others can use it via npm/browserify, AMD or script tag 6. The world has just become a better place
for file size (Node core & npm) Browserify adds some boilerplate code Frameworks that have a built-in module system (like AngularJS) + CommonJS modules –> might be confusing
step required clunky syntax no npm (though there is bower) EcmaScript 6 Modules with transpiler will become standard currently: build step required in future: directly supported by browsers