├── .editorconfig ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE.md ├── README.md ├── index.js ├── package.json ├── templates ├── amd.js ├── amdCommonWeb.js ├── amdWeb.js ├── common.js ├── node.js ├── returnExports.js └── web.js └── test ├── fixture ├── amd │ ├── testExports.js │ ├── testNamespace.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js ├── amdCommonWeb │ ├── testExports.js │ ├── testExportsMap.js │ ├── testNamespace.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js ├── amdWeb │ ├── testExports.js │ ├── testNamespace.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js ├── common │ ├── testExports.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js ├── foo.js ├── node │ ├── testExports.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js ├── returnExports │ ├── testExports.js │ ├── testNamespace.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js └── web │ ├── testExports.js │ ├── testNamespace.js │ ├── testWithDependencies.js │ └── testWithoutDependencies.js └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | indent_style = space 11 | indent_size = 2 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "bitwise": false, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "esnext": true, 7 | "evil": false, 8 | "forin": false, 9 | "immed": true, 10 | "indent": 2, 11 | "lastsemic": false, 12 | "maxdepth": false, 13 | "multistr": false, 14 | "newcap": true, 15 | "noarg": true, 16 | "node": true, 17 | "onevar": false, 18 | "quotmark": "single", 19 | "regexp": true, 20 | "smarttabs": true, 21 | "strict": true, 22 | "trailing": true, 23 | "undef": true, 24 | "unused": true 25 | } 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - "8" 6 | - "6" 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2014 Eduardo Lundgren (http://eduardo.io) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gulp-umd 2 | ============ 3 | 4 | [![Build Status](http://img.shields.io/travis/eduardolundgren/gulp-umd.svg?style=flat)](https://travis-ci.org/eduardolundgren/gulp-umd) 5 | 6 | This repository provides a simple way to build your files with support for the design and implementation of the Universal Module Definition (UMD) API for JavaScript modules. These are modules which are capable of working everywhere, be it in the client, on the server or elsewhere. 7 | 8 | The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others). In many cases it uses [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) as a base, with special-casing added to handle [CommonJS](http://wiki.commonjs.org/wiki/CommonJS) compatibility. 9 | 10 | ## Variations 11 | 12 | ### Regular Module 13 | 14 | * amdNodeWeb / returnExports / [templates/returnExports.js](https://github.com/umdjs/umd/blob/master/templates/returnExports.js) - 15 | Defines a module that works in Node, AMD and browser globals. If you also want 16 | to export a global even when AMD is in play (useful if you are loading other 17 | scripts that still expect that global), use 18 | [returnExportsGlobal.js](https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js). 19 | 20 | * amd / [templates/amd.js](test/fixture/amd/testWithDependencies.js) 21 | Defines a module that works in AMD. 22 | 23 | * amdCommonWeb / [templates/amdCommonWeb.js](test/fixture/amdCommonWeb/testWithDependencies.js) 24 | Defines a module that works in CommonJS Strict, AMD and browser globals. 25 | 26 | * amdWeb / [templates/amdWeb.js](test/fixture/amdWeb/testWithDependencies.js) 27 | Defines a module that works in AMD and browser globals. 28 | 29 | * common / [templates/common.js](test/fixture/common/testWithDependencies.js) 30 | Defines a module that works in CommonJS Strict. 31 | 32 | * node / [templates/node.js](test/fixture/node/testWithDependencies.js) 33 | Defines a module that works in Node. 34 | 35 | * web / [templates/web.js](test/fixture/web/testWithDependencies.js) 36 | Defines a module that works in browser globals. 37 | 38 | See more variation options that can be added as [templates onto this project](https://github.com/eduardolundgren/gulp-umd/tree/master/templates) on the [UMD (Universal Module Definition) patterns](https://github.com/umdjs/umd). 39 | 40 | ## Options 41 | 42 | The following options are the ones available with the current default values: 43 | 44 | ```js 45 | { 46 | dependencies: function(file) { 47 | return []; 48 | }, 49 | exports: function(file) { 50 | return capitalizeFilename(file); 51 | }, 52 | namespace: function(file) { 53 | return capitalizeFilename(file); 54 | }, 55 | templateName: 'amdNodeWeb', 56 | template: path.join(__dirname, 'templates/returnExports.js'), 57 | templateSource: 'module.exports = <%= exports %>' 58 | } 59 | ``` 60 | 61 | * `dependencies`: 62 | Function which returns an array of dependencies. 63 | Each dependency is specified either as a string, or as an object of the form. 64 | ```js 65 | { 66 | dependencies: function (file) { 67 | return { 68 | name: 'defaultModuleName', 69 | amd: 'moduleNameInAMD', 70 | cjs: 'moduleNameInCommonJsAndNodeJs', 71 | global: 'moduleNameInBrowserGlobals', 72 | param: 'ModuleIdentifier' 73 | } 74 | } 75 | ``` 76 | 77 | * `exports`: 78 | Specifies the item (or for CommonJS, *item's*) which the module will export. 79 | For non CommonJS, this value should be a string specifying the exported item. 80 | ```js 81 | { 82 | exports: function (file) { 83 | return 'Foo.Bar'; 84 | } 85 | } 86 | ``` 87 | For CommonJS, this value should be an object with keys specifying the names and values specifying the exported items. 88 | ```js 89 | { 90 | exports: function (file) { 91 | return { 92 | 'Foo': 'Foo', 93 | 'FooBar': 'Foo.Bar' 94 | }; 95 | } 96 | } 97 | ``` 98 | 99 | * `namespace`: 100 | Specifies the global namespace to export to. Only used for Web globals. 101 | ```js 102 | { 103 | namespace: function (file) { 104 | return 'My.Global.Namespace'; 105 | } 106 | } 107 | ``` 108 | 109 | * `templateName`: 110 | Specifies the name of the template to use. 111 | Available template names are amd, amdNodeWeb, amdCommonWeb, amdWeb, common, node, returnExports and web. 112 | *See above for descriptions.* 113 | If specified, overrides the template and templateSource. 114 | ```js 115 | { 116 | templateName: 'amdNodeWeb' 117 | } 118 | ``` 119 | 120 | * `templateSource`: 121 | Specifies the lodash template source to use when wrapping input files. 122 | If specified, overrides template. 123 | ```js 124 | { 125 | template: '<%= contents %>' 126 | } 127 | ``` 128 | 129 | * `template`: 130 | Specifies the path to a file containing a lodash template to use when wrapping input files. 131 | ``` 132 | { 133 | template: '/path/to/my/template' 134 | } 135 | ``` 136 | 137 | ## Examples 138 | 139 | #### Build a simple module 140 | 141 | Let's wrap `src/foo.js` file with UMD definition: 142 | 143 | ```js 144 | 'use strict'; 145 | function Foo() {} 146 | ``` 147 | 148 | Then, in the gulp task: 149 | 150 | ```js 151 | gulp.task('umd', function() { 152 | return gulp.src('src/*.js') 153 | .pipe(umd()) 154 | .pipe(gulp.dest('build')); 155 | }); 156 | ``` 157 | 158 | After build `build/foo.js` will look like: 159 | 160 | ```js 161 | (function(root, factory) { 162 | if (typeof define === 'function' && define.amd) { 163 | define([], factory); 164 | } else if (typeof exports === 'object') { 165 | module.exports = factory(); 166 | } else { 167 | root.Foo = factory(); 168 | } 169 | }(this, function() { 170 | 'use strict'; 171 | function Foo() {} 172 | return Foo; 173 | })); 174 | ``` 175 | 176 | Note that by default the filename `foo.js` is uppercased and will be used as the return exports for your module and also for the global namespace, in this case `root.Foo`. This is configurable, see the advanced build section below. 177 | 178 | #### Build with dependencies 179 | 180 | Let's wrap `src/foo.js` file with UMD definition defining some dependencies: 181 | 182 | ```js 183 | 'use strict'; 184 | function Foo() {} 185 | ``` 186 | 187 | Then, in the gulp task: 188 | 189 | ```js 190 | gulp.task('umd', function(file) { 191 | return gulp.src('src/*.js') 192 | .pipe(umd({ 193 | dependencies: function(file) { 194 | return [ 195 | { 196 | name: 'moduleName1', 197 | amd: 'moduleName1_amd', 198 | cjs: 'moduleName1_cjs', 199 | global: 'moduleName1_glob', 200 | param: 'moduleName1' 201 | }, 202 | { 203 | name: 'moduleName2', 204 | amd: 'moduleName2_amd', 205 | cjs: 'moduleName2_cjs', 206 | global: 'moduleName2_glob', 207 | param: 'moduleName2' 208 | } 209 | ]; 210 | } 211 | })) 212 | .pipe(gulp.dest('build')); 213 | }); 214 | ``` 215 | 216 | After build `build/foo.js` will look like: 217 | 218 | ```js 219 | (function(root, factory) { 220 | if (typeof define === 'function' && define.amd) { 221 | define(['moduleName1_amd', 'moduleName2_amd'], factory); 222 | } else if (typeof exports === 'object') { 223 | module.exports = factory(require('moduleName1_cjs'), require('moduleName2_cjs')); 224 | } else { 225 | root.Foo = factory(root.moduleName1_glob, root.moduleName2_glob); 226 | } 227 | }(this, function(moduleName1, moduleName2) { 228 | 'use strict'; 229 | function Foo() {} 230 | return Foo; 231 | })); 232 | ``` 233 | 234 | The advanced configuration for the dependencies allows you to have full control of how your UMD wrapper should handle dependency names. 235 | 236 | #### Advanced build 237 | 238 | Let's wrap `src/foo.js` file with UMD definition and exports the `Foo.Bar` class: 239 | 240 | ```js 241 | 'use strict'; 242 | function Foo() {}; 243 | Foo.Bar = function() {}; 244 | ``` 245 | 246 | Then, in the gulp task: 247 | 248 | ```js 249 | gulp.task('umd', function() { 250 | return gulp.src('src/*.js') 251 | .pipe(umd({ 252 | exports: function(file) { 253 | return 'Foo.Bar'; 254 | }, 255 | namespace: function(file) { 256 | return 'Foo.Bar'; 257 | } 258 | })) 259 | .pipe(gulp.dest('build')); 260 | }); 261 | ``` 262 | 263 | After build `build/foo.js will look like: 264 | 265 | ```js 266 | (function(root, factory) { 267 | if (typeof define === 'function' && define.amd) { 268 | define([], factory); 269 | } else if (typeof exports === 'object') { 270 | module.exports = factory(); 271 | } else { 272 | root.Foo.Bar = factory(); 273 | } 274 | }(this, function() { 275 | 'use strict'; 276 | function Foo() {}; 277 | Foo.Bar = function() {}; 278 | return Foo.Bar; 279 | })); 280 | ``` 281 | 282 | ## Templates 283 | 284 | In order to use any of the variations defined on the [UMD (Universal Module Definition) patterns](https://github.com/umdjs/umd) repository you can use the following template keys: 285 | 286 | * `<%= amd %>`: Contains the AMD normalized values from the options dependencies array, e.g. `['a', 'b']` turns into `['a', 'b']`. 287 | * `<%= cjs %>`: Contains the CommonJS normalized values from the options dependencies array, e.g. `['a', 'b']` turns into `require('a'), require('b')`. 288 | * `<%= commaCjs %>`: As above, prefixed with ', ' if not empty. 289 | * `<%= global %>`: Contains the browser globals normalized values from the options dependencies array, e.g. `['a', 'b']` turns into `root.a, root.b`. 290 | * `<%= commaGlobal %>`: As above, prefixed with ', ' if not empty. 291 | * `<%= namespace %>`: The namespace where the exported value is going to be set on the browser, e.g. `root.Foo.Bar`. 292 | * `<%= exports %>`: What the module should return, e.g. `Foo.Bar`. By default it returns the filename with uppercase without extension, e.g. `foo.js` returns `Foo`. 293 | If using CommonJS, this value may be an object as specified by the result of options.exports 294 | * `<%= param %>`: Comma seperated list of variable names which are bound to their respective modules, eg `a, b`. 295 | * `<%= commaParam %>`: As above, prefixed with ', ' if not empty. 296 | 297 | You can also use [umd-templates](https://www.npmjs.com/package/umd-templates), using the `patternName.path` property if `template` option is used, and `patternName.template` if `templateSource` is used. 298 | 299 | ## Contributing 300 | 301 | 1. Fork it! 302 | 2. Create your feature branch: `git checkout -b my-new-feature` 303 | 3. Commit your changes: `git commit -m 'Add some feature'` 304 | 4. Push to the branch: `git push origin my-new-feature` 305 | 5. Submit a pull request :D 306 | 307 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var concat = require('concat-stream'); 4 | var fs = require('fs'); 5 | var path = require('path'); 6 | var _template = require('lodash.template'); 7 | var through = require('through2'); 8 | 9 | var defaultOptions = { 10 | dependencies: function() { 11 | return []; 12 | }, 13 | exports: function(file) { 14 | return capitalizeFilename(file); 15 | }, 16 | namespace: function(file) { 17 | return capitalizeFilename(file); 18 | }, 19 | template: path.join(__dirname, 'templates', 'returnExports.js') 20 | }; 21 | 22 | function umd(options) { 23 | options = Object.assign({}, defaultOptions, options); 24 | 25 | var text; 26 | 27 | if(options.templateName) { 28 | text = options.templateName; 29 | if (text === 'amdNodeWeb') { 30 | text = 'returnExports'; 31 | } 32 | text = path.join(__dirname, 'templates', text + '.js'); 33 | text = fs.readFileSync(text); 34 | } 35 | else if(options.templateSource) { 36 | text = options.templateSource; 37 | } 38 | else { 39 | text = fs.readFileSync(options.template); 40 | } 41 | 42 | var compiled = _template(text); 43 | 44 | return through.obj(function(file, enc, next) { 45 | var data; 46 | var err; 47 | 48 | try { 49 | data = buildFileTemplateData(file, options); 50 | wrap(file, compiled, data); 51 | } catch (e) { 52 | err = e; 53 | } 54 | 55 | next(err, file); 56 | }); 57 | } 58 | 59 | function buildFileTemplateData(file, options) { 60 | var amd = []; 61 | var cjs = []; 62 | var global = []; 63 | var param = []; 64 | var requires = []; 65 | var dependencies = options.dependencies(file); 66 | var commaPrefix; 67 | 68 | dependencies.forEach(function(dep) { 69 | if (typeof dep === 'string') { 70 | dep = { 71 | amd: dep, 72 | cjs: dep, 73 | global: dep, 74 | param: dep 75 | }; 76 | } 77 | amd.push('\'' + (dep.amd || dep.name) + '\''); 78 | cjs.push('require(\'' + (dep.cjs || dep.name) + '\')'); 79 | global.push('root.' + (dep.global || dep.name)); 80 | param.push(dep.param || dep.name); 81 | requires.push((dep.param || dep.name) + '=require(\'' + (dep.cjs || dep.name) + '\')'); 82 | }); 83 | 84 | commaPrefix = function (items) { 85 | return items.map(function (value) { 86 | return ', ' + value; 87 | }).join(''); 88 | }; 89 | 90 | return { 91 | dependencies: dependencies, 92 | exports: options.exports(file), 93 | namespace: options.namespace(file), 94 | // Adds resolved dependencies for each environment into the template data 95 | amd: '[' + amd.join(', ') + ']', 96 | cjs: cjs.join(', '), 97 | commaCjs: commaPrefix(cjs), 98 | global: global.join(', '), 99 | commaGlobal: commaPrefix(global), 100 | param: param.join(', '), 101 | commaParam: commaPrefix(param) 102 | // ======================================================================= 103 | }; 104 | } 105 | 106 | function capitalizeFilename(file) { 107 | var name = path.basename(file.path, path.extname(file.path)); 108 | return name.charAt(0).toUpperCase() + name.substring(1); 109 | } 110 | 111 | function wrap(file, template, data) { 112 | data.file = file; 113 | 114 | if (file.isStream()) { 115 | var contents = through(); 116 | 117 | file.contents.pipe(concat({encoding: 'utf-8'}, function(s) { 118 | data.contents = s; 119 | contents.push(template(data)); 120 | contents.push(null); 121 | })); 122 | file.contents = contents; 123 | } else if (file.isBuffer()) { 124 | data.contents = file.contents.toString(); 125 | file.contents = Buffer.from(template(data)); 126 | } 127 | } 128 | 129 | module.exports = umd; 130 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-umd", 3 | "version": "2.0.0", 4 | "description": "Gulp plugin for build JavaScript files as Universal Module Definition, aka UMD.", 5 | "license": "MIT", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/eduardolundgren/gulp-umd.git" 9 | }, 10 | "author": "Eduardo Lundgren (http://eduardo.io)", 11 | "contributors": [ 12 | "Cody A. Taylor " 13 | ], 14 | "homepage": "https://github.com/eduardolundgren/gulp-umd#readme", 15 | "bugs": { 16 | "url": "https://github.com/eduardolundgren/gulp-umd/issues" 17 | }, 18 | "main": "index.js", 19 | "scripts": { 20 | "test": "nodeunit test/test.js" 21 | }, 22 | "keywords": [ 23 | "amd", 24 | "cjs", 25 | "commonjs", 26 | "gulpplugin", 27 | "module", 28 | "umd", 29 | "wrap" 30 | ], 31 | "dependencies": { 32 | "concat-stream": "^1.6.2", 33 | "lodash.template": "^4.4.0", 34 | "through2": "^2.0.3" 35 | }, 36 | "devDependencies": { 37 | "gulp": "^3.9.1", 38 | "nodeunit": "^0.11.2" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /templates/amd.js: -------------------------------------------------------------------------------- 1 | ;define(<%= amd %>, function (<%= param %>) { 2 | <%= contents %> 3 | return <%= exports %>; 4 | }); 5 | -------------------------------------------------------------------------------- /templates/amdCommonWeb.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(<%= amd %>, factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports<%= commaCjs %>); 6 | } else { 7 | // Browser globals 8 | factory((root.<%= namespace %> = {})<%= commaGlobal %>); 9 | } 10 | }(this, function (exports<%= commaParam %>) { 11 | <%= contents %><% 12 | if (typeof exports == 'string') { %> 13 | exports.<%= exports %> = <%= exports %>;<% 14 | } else { 15 | for (var key in exports) { 16 | if ({}.hasOwnProperty.call(exports,key)) { 17 | %> 18 | exports.<%= key %> = <%= exports[key] %>;<% 19 | } 20 | } 21 | } 22 | %> 23 | })); 24 | -------------------------------------------------------------------------------- /templates/amdWeb.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(<%= amd %>, factory); 4 | } else { 5 | root.<%= namespace %> = factory(<%= global %>); 6 | } 7 | }(this, function (<%= param %>) { 8 | <%= contents %> 9 | return <%= exports %>; 10 | })); 11 | -------------------------------------------------------------------------------- /templates/common.js: -------------------------------------------------------------------------------- 1 | ;(function (exports<%= commaParam %>) { 2 | <%= contents %><% 3 | if (typeof exports == 'string') { %> 4 | exports.<%= exports %> = <%= exports %>;<% 5 | } else { 6 | for (var key in exports) { 7 | if ({}.hasOwnProperty.call(exports,key)) { 8 | %> 9 | exports.<%= key %> = <%= exports[key] %>;<% 10 | } 11 | } 12 | } 13 | %> 14 | })(this<%= commaCjs %>); 15 | -------------------------------------------------------------------------------- /templates/node.js: -------------------------------------------------------------------------------- 1 | (function (<%= param %>){ 2 | <%= contents %> 3 | module.exports = <%= exports %>; 4 | })(<%= cjs %>); 5 | -------------------------------------------------------------------------------- /templates/returnExports.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(<%= amd %>, factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(<%= cjs %>); 6 | } else { 7 | root.<%= namespace %> = factory(<%= global %>); 8 | } 9 | }(this, function(<%= param %>) { 10 | <%= contents %> 11 | return <%= exports %>; 12 | })); 13 | -------------------------------------------------------------------------------- /templates/web.js: -------------------------------------------------------------------------------- 1 | ;(function () { 2 | <%= contents %> 3 | this.<%= namespace %> = <%= exports %>; 4 | }).call(this); 5 | -------------------------------------------------------------------------------- /test/fixture/amd/testExports.js: -------------------------------------------------------------------------------- 1 | ;define([], function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | return Foo.Bar; 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixture/amd/testNamespace.js: -------------------------------------------------------------------------------- 1 | ;define([], function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | return Foo; 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixture/amd/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;define(['m0', 'm1amd', 'm2amd'], function (m0, m1param, m2param) { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | return Foo; 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixture/amd/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;define([], function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | return Foo; 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixture/amdCommonWeb/testExports.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports); 6 | } else { 7 | // Browser globals 8 | factory((root.Foo = {})); 9 | } 10 | }(this, function (exports) { 11 | 'use strict'; 12 | function Foo() {} 13 | 14 | exports.Foo = Foo; 15 | })); 16 | -------------------------------------------------------------------------------- /test/fixture/amdCommonWeb/testExportsMap.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports); 6 | } else { 7 | // Browser globals 8 | factory((root.Foo = {})); 9 | } 10 | }(this, function (exports) { 11 | 'use strict'; 12 | function Foo() {} 13 | 14 | exports.FooFunc = Foo; 15 | exports.FooLength = Foo.length; 16 | })); 17 | -------------------------------------------------------------------------------- /test/fixture/amdCommonWeb/testNamespace.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports); 6 | } else { 7 | // Browser globals 8 | factory((root.Foo.Bar = {})); 9 | } 10 | }(this, function (exports) { 11 | 'use strict'; 12 | function Foo() {} 13 | 14 | exports.Foo = Foo; 15 | })); 16 | -------------------------------------------------------------------------------- /test/fixture/amdCommonWeb/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(['m0', 'm1amd', 'm2amd'], factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports, require('m0'), require('m1cjs'), require('m2cjs')); 6 | } else { 7 | // Browser globals 8 | factory((root.Foo = {}), root.m0, root.m1glob, root.m2glob); 9 | } 10 | }(this, function (exports, m0, m1param, m2param) { 11 | 'use strict'; 12 | function Foo() {} 13 | 14 | exports.Foo = Foo; 15 | })); 16 | -------------------------------------------------------------------------------- /test/fixture/amdCommonWeb/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | factory(exports); 6 | } else { 7 | // Browser globals 8 | factory((root.Foo = {})); 9 | } 10 | }(this, function (exports) { 11 | 'use strict'; 12 | function Foo() {} 13 | 14 | exports.Foo = Foo; 15 | })); 16 | -------------------------------------------------------------------------------- /test/fixture/amdWeb/testExports.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else { 5 | root.Foo = factory(); 6 | } 7 | }(this, function () { 8 | 'use strict'; 9 | function Foo() {} 10 | 11 | return Foo.Bar; 12 | })); 13 | -------------------------------------------------------------------------------- /test/fixture/amdWeb/testNamespace.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else { 5 | root.Foo.Bar = factory(); 6 | } 7 | }(this, function () { 8 | 'use strict'; 9 | function Foo() {} 10 | 11 | return Foo; 12 | })); 13 | -------------------------------------------------------------------------------- /test/fixture/amdWeb/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(['m0', 'm1amd', 'm2amd'], factory); 4 | } else { 5 | root.Foo = factory(root.m0, root.m1glob, root.m2glob); 6 | } 7 | }(this, function (m0, m1param, m2param) { 8 | 'use strict'; 9 | function Foo() {} 10 | 11 | return Foo; 12 | })); 13 | -------------------------------------------------------------------------------- /test/fixture/amdWeb/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else { 5 | root.Foo = factory(); 6 | } 7 | }(this, function () { 8 | 'use strict'; 9 | function Foo() {} 10 | 11 | return Foo; 12 | })); 13 | -------------------------------------------------------------------------------- /test/fixture/common/testExports.js: -------------------------------------------------------------------------------- 1 | ;(function (exports) { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | exports.FooBar = Foo.Bar; 6 | })(this); 7 | -------------------------------------------------------------------------------- /test/fixture/common/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function (exports, m0, m1param, m2param) { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | exports.Foo = Foo; 6 | })(this, require('m0'), require('m1cjs'), require('m2cjs')); 7 | -------------------------------------------------------------------------------- /test/fixture/common/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function (exports) { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | exports.Foo = Foo; 6 | })(this); 7 | -------------------------------------------------------------------------------- /test/fixture/foo.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | function Foo() {} 3 | -------------------------------------------------------------------------------- /test/fixture/node/testExports.js: -------------------------------------------------------------------------------- 1 | (function (){ 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | module.exports = Foo.Bar; 6 | })(); 7 | -------------------------------------------------------------------------------- /test/fixture/node/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | (function (m0, m1param, m2param){ 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | module.exports = Foo; 6 | })(require('m0'), require('m1cjs'), require('m2cjs')); 7 | -------------------------------------------------------------------------------- /test/fixture/node/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | (function (){ 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | module.exports = Foo; 6 | })(); 7 | -------------------------------------------------------------------------------- /test/fixture/returnExports/testExports.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(); 6 | } else { 7 | root.Foo = factory(); 8 | } 9 | }(this, function() { 10 | 'use strict'; 11 | function Foo() {} 12 | 13 | return Foo.Bar; 14 | })); 15 | -------------------------------------------------------------------------------- /test/fixture/returnExports/testNamespace.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(); 6 | } else { 7 | root.Foo.Bar = factory(); 8 | } 9 | }(this, function() { 10 | 'use strict'; 11 | function Foo() {} 12 | 13 | return Foo; 14 | })); 15 | -------------------------------------------------------------------------------- /test/fixture/returnExports/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define(['m0', 'm1amd', 'm2amd'], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(require('m0'), require('m1cjs'), require('m2cjs')); 6 | } else { 7 | root.Foo = factory(root.m0, root.m1glob, root.m2glob); 8 | } 9 | }(this, function(m0, m1param, m2param) { 10 | 'use strict'; 11 | function Foo() {} 12 | 13 | return Foo; 14 | })); 15 | -------------------------------------------------------------------------------- /test/fixture/returnExports/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(); 6 | } else { 7 | root.Foo = factory(); 8 | } 9 | }(this, function() { 10 | 'use strict'; 11 | function Foo() {} 12 | 13 | return Foo; 14 | })); 15 | -------------------------------------------------------------------------------- /test/fixture/web/testExports.js: -------------------------------------------------------------------------------- 1 | ;(function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | this.Foo = Foo.Bar; 6 | }).call(this); 7 | -------------------------------------------------------------------------------- /test/fixture/web/testNamespace.js: -------------------------------------------------------------------------------- 1 | ;(function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | this.Foo.Bar = Foo; 6 | }).call(this); 7 | -------------------------------------------------------------------------------- /test/fixture/web/testWithDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | this.Foo = Foo; 6 | }).call(this); 7 | -------------------------------------------------------------------------------- /test/fixture/web/testWithoutDependencies.js: -------------------------------------------------------------------------------- 1 | ;(function () { 2 | 'use strict'; 3 | function Foo() {} 4 | 5 | this.Foo = Foo; 6 | }).call(this); 7 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var concat = require('concat-stream'); 5 | var umd = require('../'); 6 | var fs = require('fs'); 7 | var path = require('path'); 8 | 9 | var genericTest = function (options, fixtureFilename) { 10 | return function (test) { 11 | var isDone = { 12 | buffer: false, 13 | stream: false 14 | }; 15 | var done = key => { 16 | isDone[key] = true; 17 | if (isDone.buffer && isDone.stream) { 18 | test.done(); 19 | } 20 | }; 21 | var compare = fs.readFileSync(path.join(__dirname, 'fixture', fixtureFilename), 'utf-8'); 22 | var assertContents = contents => { 23 | test.equal(contents, compare, 'Wrapped file content is different from test template ' + fixtureFilename); 24 | }; 25 | 26 | gulp.src('test/fixture/foo.js', {buffer: false}) 27 | .pipe(umd(options)) 28 | .pipe(concat({encoding: 'object'}, function(files) { 29 | var file = files[0]; 30 | file.contents.pipe(concat({encoding: 'utf-8'}, contents => { 31 | assertContents(contents); 32 | done('stream'); 33 | })); 34 | })); 35 | gulp.src('test/fixture/foo.js', {buffer: true}) 36 | .pipe(umd(options)) 37 | .pipe(concat({encoding: 'object'}, function(files) { 38 | var file = files[0]; 39 | assertContents(file.contents.toString()); 40 | done('buffer'); 41 | })); 42 | }; 43 | }; 44 | 45 | module.exports = { 46 | /* Generic test structure 47 | misc: { 48 | testExports: genericTest( 49 | { 50 | templateName: 'xxxx', 51 | exports: function() { 52 | return 'Foo.Bar'; 53 | } 54 | }, 55 | 'xxxxx/testExports.js' 56 | ), 57 | 58 | testNamespace: genericTest( 59 | { 60 | templateName: 'xxxx', 61 | namespace: function() { 62 | return 'Foo.Bar'; 63 | } 64 | }, 65 | 'xxxxx/testNamespace.js' 66 | ), 67 | 68 | testWithDependencies: genericTest( 69 | { 70 | templateName: 'xxxx', 71 | dependencies: function() { 72 | return [ 73 | 'm0', 74 | { 75 | name: 'm1', 76 | amd: 'm1amd', 77 | cjs: 'm1cjs', 78 | global: 'm1glob', 79 | param: 'm1param' 80 | }, 81 | { 82 | name: 'm2', 83 | amd: 'm2amd', 84 | cjs: 'm2cjs', 85 | global: 'm2glob', 86 | param: 'm2param' 87 | } 88 | ]; 89 | } 90 | }, 91 | 'xxxxx/testWithDependencies.js' 92 | ), 93 | 94 | testWithoutDependencies: genericTest( 95 | { 96 | templateName: 'xxxx' 97 | 98 | }, 99 | 'xxxxx/testWithoutDependencies.js' 100 | ) 101 | }, 102 | /**/ 103 | 104 | amd: { 105 | testExports: genericTest( 106 | { 107 | templateName: 'amd', 108 | exports: function() { 109 | return 'Foo.Bar'; 110 | } 111 | }, 112 | 'amd/testExports.js' 113 | ), 114 | 115 | testNamespace: genericTest( 116 | { 117 | templateName: 'amd', 118 | namespace: function() { 119 | return 'Foo.Bar'; 120 | } 121 | }, 122 | 'amd/testNamespace.js' 123 | ), 124 | 125 | testWithDependencies: genericTest( 126 | { 127 | templateName: 'amd', 128 | dependencies: function() { 129 | return [ 130 | 'm0', 131 | { 132 | name: 'm1', 133 | amd: 'm1amd', 134 | cjs: 'm1cjs', 135 | global: 'm1glob', 136 | param: 'm1param' 137 | }, 138 | { 139 | name: 'm2', 140 | amd: 'm2amd', 141 | cjs: 'm2cjs', 142 | global: 'm2glob', 143 | param: 'm2param' 144 | } 145 | ]; 146 | } 147 | }, 148 | 'amd/testWithDependencies.js' 149 | ), 150 | 151 | testWithoutDependencies: genericTest( 152 | { 153 | templateName: 'amd' 154 | }, 155 | 'amd/testWithoutDependencies.js' 156 | ) 157 | }, 158 | 159 | amdCommonWeb: { 160 | testExports: genericTest( 161 | { 162 | templateName: 'amdCommonWeb', 163 | exports: function() { 164 | return 'Foo'; 165 | } 166 | }, 167 | 'amdCommonWeb/testExports.js' 168 | ), 169 | 170 | testExportsMap: genericTest( 171 | { 172 | templateName: 'amdCommonWeb', 173 | exports: function() { 174 | return { 175 | 'FooFunc': 'Foo', 176 | 'FooLength': 'Foo.length' 177 | }; 178 | } 179 | }, 180 | 'amdCommonWeb/testExportsMap.js' 181 | ), 182 | 183 | testNamespace: genericTest( 184 | { 185 | templateName: 'amdCommonWeb', 186 | namespace: function() { 187 | return 'Foo.Bar'; 188 | } 189 | }, 190 | 'amdCommonWeb/testNamespace.js' 191 | ), 192 | 193 | testWithDependencies: genericTest( 194 | { 195 | templateName: 'amdCommonWeb', 196 | dependencies: function() { 197 | return [ 198 | 'm0', 199 | { 200 | name: 'm1', 201 | amd: 'm1amd', 202 | cjs: 'm1cjs', 203 | global: 'm1glob', 204 | param: 'm1param' 205 | }, 206 | { 207 | name: 'm2', 208 | amd: 'm2amd', 209 | cjs: 'm2cjs', 210 | global: 'm2glob', 211 | param: 'm2param' 212 | } 213 | ]; 214 | } 215 | }, 216 | 'amdCommonWeb/testWithDependencies.js' 217 | ), 218 | 219 | testWithoutDependencies: genericTest( 220 | { 221 | templateName: 'amdCommonWeb' 222 | }, 223 | 'amdCommonWeb/testWithoutDependencies.js' 224 | ) 225 | }, 226 | 227 | amdWeb: { 228 | testExports: genericTest( 229 | { 230 | templateName: 'amdWeb', 231 | exports: function() { 232 | return 'Foo.Bar'; 233 | } 234 | }, 235 | 'amdWeb/testExports.js' 236 | ), 237 | 238 | testNamespace: genericTest( 239 | { 240 | templateName: 'amdWeb', 241 | namespace: function() { 242 | return 'Foo.Bar'; 243 | } 244 | }, 245 | 'amdWeb/testNamespace.js' 246 | ), 247 | 248 | testWithDependencies: genericTest( 249 | { 250 | templateName: 'amdWeb', 251 | dependencies: function() { 252 | return [ 253 | 'm0', 254 | { 255 | name: 'm1', 256 | amd: 'm1amd', 257 | cjs: 'm1cjs', 258 | global: 'm1glob', 259 | param: 'm1param' 260 | }, 261 | { 262 | name: 'm2', 263 | amd: 'm2amd', 264 | cjs: 'm2cjs', 265 | global: 'm2glob', 266 | param: 'm2param' 267 | } 268 | ]; 269 | } 270 | }, 271 | 'amdWeb/testWithDependencies.js' 272 | ), 273 | 274 | testWithoutDependencies: genericTest( 275 | { 276 | templateName: 'amdWeb' 277 | }, 278 | 'amdWeb/testWithoutDependencies.js' 279 | ) 280 | }, 281 | 282 | common: { 283 | testExports: genericTest( 284 | { 285 | templateName: 'common', 286 | exports: function() { 287 | return { 288 | 'FooBar': 'Foo.Bar' 289 | }; 290 | } 291 | }, 292 | 'common/testExports.js' 293 | ), 294 | 295 | testWithDependencies: genericTest( 296 | { 297 | templateName: 'common', 298 | dependencies: function() { 299 | return [ 300 | 'm0', 301 | { 302 | name: 'm1', 303 | amd: 'm1amd', 304 | cjs: 'm1cjs', 305 | global: 'm1glob', 306 | param: 'm1param' 307 | }, 308 | { 309 | name: 'm2', 310 | amd: 'm2amd', 311 | cjs: 'm2cjs', 312 | global: 'm2glob', 313 | param: 'm2param' 314 | } 315 | ]; 316 | } 317 | }, 318 | 'common/testWithDependencies.js' 319 | ), 320 | 321 | testWithoutDependencies: genericTest( 322 | { 323 | templateName: 'common' 324 | }, 325 | 'common/testWithoutDependencies.js' 326 | ) 327 | }, 328 | 329 | node: { 330 | testExports: genericTest( 331 | { 332 | templateName: 'node', 333 | exports: function() { 334 | return 'Foo.Bar'; 335 | } 336 | }, 337 | 'node/testExports.js' 338 | ), 339 | 340 | testWithDependencies: genericTest( 341 | { 342 | templateName: 'node', 343 | dependencies: function() { 344 | return [ 345 | 'm0', 346 | { 347 | name: 'm1', 348 | amd: 'm1amd', 349 | cjs: 'm1cjs', 350 | global: 'm1glob', 351 | param: 'm1param' 352 | }, 353 | { 354 | name: 'm2', 355 | amd: 'm2amd', 356 | cjs: 'm2cjs', 357 | global: 'm2glob', 358 | param: 'm2param' 359 | } 360 | ]; 361 | } 362 | }, 363 | 'node/testWithDependencies.js' 364 | ), 365 | 366 | testWithoutDependencies: genericTest( 367 | { 368 | templateName: 'node' 369 | }, 370 | 'node/testWithoutDependencies.js' 371 | ) 372 | }, 373 | 374 | 'umd / returnExports': { 375 | testExports: genericTest( 376 | { 377 | exports: function() { 378 | return 'Foo.Bar'; 379 | } 380 | }, 381 | 'returnExports/testExports.js' 382 | ), 383 | 384 | testNamespace: genericTest( 385 | { 386 | namespace: function() { 387 | return 'Foo.Bar'; 388 | } 389 | }, 390 | 'returnExports/testNamespace.js' 391 | ), 392 | 393 | testWithDependencies: genericTest( 394 | { 395 | dependencies: function() { 396 | return [ 397 | 'm0', 398 | { 399 | name: 'm1', 400 | amd: 'm1amd', 401 | cjs: 'm1cjs', 402 | global: 'm1glob', 403 | param: 'm1param' 404 | }, 405 | { 406 | name: 'm2', 407 | amd: 'm2amd', 408 | cjs: 'm2cjs', 409 | global: 'm2glob', 410 | param: 'm2param' 411 | } 412 | ]; 413 | } 414 | }, 415 | 'returnExports/testWithDependencies.js' 416 | ), 417 | 418 | testWithoutDependencies: genericTest( 419 | { 420 | }, 421 | 'returnExports/testWithoutDependencies.js' 422 | ), 423 | 424 | testWithoutDependenciesAlias: genericTest( 425 | { 426 | templateName: 'amdNodeWeb' 427 | }, 428 | 'returnExports/testWithoutDependencies.js' 429 | ) 430 | }, 431 | 432 | web: { 433 | testExports: genericTest( 434 | { 435 | templateName: 'web', 436 | exports: function() { 437 | return 'Foo.Bar'; 438 | } 439 | }, 440 | 'web/testExports.js' 441 | ), 442 | 443 | testNamespace: genericTest( 444 | { 445 | templateName: 'web', 446 | namespace: function() { 447 | return 'Foo.Bar'; 448 | } 449 | }, 450 | 'web/testNamespace.js' 451 | ), 452 | 453 | testWithDependencies: genericTest( 454 | { 455 | templateName: 'web', 456 | dependencies: function() { 457 | return [ 458 | 'm0', 459 | { 460 | name: 'm1', 461 | amd: 'm1amd', 462 | cjs: 'm1cjs', 463 | global: 'm1glob', 464 | param: 'm1param' 465 | }, 466 | { 467 | name: 'm2', 468 | amd: 'm2amd', 469 | cjs: 'm2cjs', 470 | global: 'm2glob', 471 | param: 'm2param' 472 | } 473 | ]; 474 | } 475 | }, 476 | 'web/testWithDependencies.js' 477 | ), 478 | 479 | testWithoutDependencies: genericTest( 480 | { 481 | templateName: 'web' 482 | }, 483 | 'web/testWithoutDependencies.js' 484 | ) 485 | } 486 | }; 487 | --------------------------------------------------------------------------------