├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .gitattributes ├── .github └── workflows │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test ├── _browser.html ├── expected ├── complex │ └── complex_init_dir.js ├── complex_init.js ├── simple_init.js └── umd_init.js ├── fixtures ├── .eslintrc.json ├── complex_init.js ├── complex_init_dir.js ├── config_init_dir.js ├── simple_init.js ├── umd_init.js └── vendor │ ├── complex_amd_file.js │ ├── non_md_file.js │ ├── simple_amd_file.js │ └── umd_file.js └── main.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | test/expected/*.js 2 | test/expected/*/*.js 3 | test/tmp/* 4 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true 4 | }, 5 | "rules": { 6 | "array-bracket-spacing": [2, "never"], 7 | "block-scoped-var": 2, 8 | "brace-style": [2, "1tbs"], 9 | "camelcase": 1, 10 | "computed-property-spacing": [2, "never"], 11 | "curly": 2, 12 | "eol-last": 2, 13 | "eqeqeq": [2, "smart"], 14 | "max-depth": [1, 3], 15 | "max-len": [1, 80], 16 | "max-statements": [1, 30], 17 | "new-cap": 1, 18 | "no-extend-native": 2, 19 | "no-mixed-spaces-and-tabs": 2, 20 | "no-trailing-spaces": 2, 21 | "no-unused-vars": 2, 22 | "no-use-before-define": [2, "nofunc"], 23 | "object-curly-spacing": [2, "always"], 24 | "quotes": [2, "single", "avoid-escape"], 25 | "semi": [2, "always"], 26 | "keyword-spacing": [2], 27 | "space-unary-ops": 2, 28 | "indent": ["error", 2], 29 | "no-undef": [2] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js text eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | env: 11 | CI: true 12 | 13 | strategy: 14 | matrix: 15 | node-version: [14.x, 16.x, 18.x] 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v3 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm install -g npm@latest # to support newer lock file 24 | - run: npm ci 25 | - run: npm run test 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_STORE 3 | npm-debug.log 4 | coverage 5 | .idea 6 | test/tmp 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Robin Thrift 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 13 | all 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 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-requirejs 2 | 3 | [![gulp-requirejs on npm](https://img.shields.io/npm/v/gulp-requirejs.svg?style=flat)](https://www.npmjs.com/package/gulp-requirejs) 4 | ![Build Status](https://github.com/jorrit/gulp-requirejs/workflows/Node.js%20CI/badge.svg) 5 | [![Coverage](https://coveralls.io/repos/github/jorrit/gulp-requirejs/badge.svg)](https://coveralls.io/github/jorrit/gulp-requirejs) 6 | 7 | ## Information 8 | 9 | A small, simple, very easy wrapper around the [require.js optimizer](https://github.com/jrburke/r.js) to work with [gulp.js](https://github.com/gulpjs/gulp). 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
Packagegulp-requirejs
Descriptionuses require.js's r.js optimizer to combine require.js AMD modules into one file
Node Version≧ 4
24 | 25 | 26 | ## Installation 27 | 28 | Simply add `gulp-requirejs` as a dev-dependency in your package.json or run 29 | 30 | ```bash 31 | $ npm install --save-dev gulp-requirejs 32 | ``` 33 | 34 | ## Usage 35 | 36 | Because the require.js optimizer (_r.js_) is a kind of build system in itself we can't use the `gulp.src([...])` syntax at the moment (I might add this in future), instead this wrapper itself emits a pipable stream, holding a 'virtual' file, in which the result of the r.js build process are saved. 37 | 38 | The resulting stream can be treated like a regular `gulp.src(...)` stream. 39 | 40 | >NOTE: The built in minification/obfuscation is deactivated by default. It is recommended to use a gulp plugin like gulp-uglify for minification, but you can enable r.js minification by setting the `optimize` option to `uglify` to minify using r.js. 41 | 42 | ```javascript 43 | var gulp = require('gulp'), 44 | rjs = require('gulp-requirejs'); 45 | 46 | gulp.task('requirejsBuild', function() { 47 | return rjs({ 48 | baseUrl: 'root/directory/of/js/files/', 49 | out: 'FILENAME_TO_BE_OUTPUTTED', 50 | name: 'mainfile', // no extension 51 | shim: { 52 | // standard require.js shim options 53 | }, 54 | // ... more require.js options 55 | }) 56 | .pipe(gulp.dest('./deploy/')); // pipe it to the output DIR 57 | }); 58 | ``` 59 | 60 | If you use instead of out the dir option, you do not need the pipe at all, see this example in Gulp 4 syntax and mocha test: 61 | 62 | ```javascript 63 | const rjs = require('gulp-requirejs'); 64 | 65 | async function requirejsBuild(cb) { 66 | return rjs({ 67 | dir: 'deploy', 68 | mainConfigFile: 'config.js', 69 | path: { 70 | 'config': '../config_init' 71 | }, 72 | modules: [{ 73 | name: 'FILENAME_TO_BE_OUTPUTTED', // no extension 74 | include : [ .. ] 75 | ... 76 | }] 77 | }) ... 78 | }; 79 | 80 | exports.requirejsBuild = requirejsBuild; 81 | ``` 82 | 83 | Note: In order to let gulp know that the optimization completes, return the rjs stream. 84 | 85 | See [requirejs.org](https://requirejs.org/docs/optimization.html) for more information about the supported parameters. 86 | 87 | ### Error handling 88 | 89 | gulp-requirejs will emit errors when you don't pass an options object, if the `baseUrl` or `out` properties are undefined or when the requirejs optimizer detects an error. 90 | 91 | ### Source maps 92 | 93 | When source maps are enabled via the r.js `generateSourceMaps` option the file in the stream `rjs()` contains a `sourceMap` property with the sourcemap as an object. 94 | 95 | Use [gulp-sourcemaps](https://www.npmjs.com/package/gulp-sourcemaps) to process this object in your gulp configuration. 96 | 97 | ```javascript 98 | var gulp = require('gulp'), 99 | rjs = require('gulp-requirejs') 100 | sourcemaps = require('gulp-sourcemaps'); 101 | 102 | gulp.task('requirejsBuild', function() { 103 | return rjs({ 104 | baseUrl: 'root/directory/of/js/files/', 105 | out: 'FILENAME_TO_BE_OUTPUTTED', 106 | name: 'mainfile', // no extension 107 | generateSourceMaps: true, 108 | shim: { 109 | // standard require.js shim options 110 | }, 111 | // ... more require.js options 112 | }) 113 | .pipe(sourcemaps.init({loadMaps: true})) // initialize gulp-sourcemaps with the existing map 114 | .pipe(sourcemaps.write()) // write the source maps 115 | .pipe(gulp.dest('./deploy/')); // pipe it to the output DIR 116 | }); 117 | ``` 118 | 119 | ## Options 120 | 121 | The options object supports the same parameters as the [require.js optimizer](https://github.com/jrburke/r.js). 122 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var requirejs = require('requirejs'); 2 | var PluginError = require('plugin-error'); 3 | var Vinyl = require('vinyl'); 4 | var es = require('event-stream'); 5 | 6 | // Consts 7 | var PLUGIN_NAME = 'gulp-requirejs'; 8 | 9 | function validateOptions(opts) { 10 | if (!opts) { 11 | throw new PluginError(PLUGIN_NAME, 'Missing options object.'); 12 | } 13 | 14 | if ( !opts.out && typeof opts.out !== 'string' 15 | && !opts.dir && typeof opts.dir !== 'string' ) { 16 | throw new PluginError(PLUGIN_NAME, 'Either single file outputs are ' + 17 | 'supported right now, please pass a valid output file name as the out ' + 18 | 'option.'); 19 | } 20 | 21 | if (!opts.baseUrl) { 22 | throw new PluginError(PLUGIN_NAME, 'Pipeing dirs/files is not ' + 23 | 'supported right now, please specify the base path for your script as ' + 24 | 'the baseUrl option.'); 25 | } 26 | } 27 | 28 | function createFile(filename, output, buildResponse, sourceMap) { 29 | var newFile = new Vinyl({ 30 | path: filename, 31 | contents: Buffer.from(output) 32 | }); 33 | // Add a string containing the list of added dependencies for 34 | // debugging purposes. 35 | newFile.buildResponse = buildResponse.replace('FUNCTION', filename); 36 | if (sourceMap) { 37 | newFile.sourceMap = JSON.parse(sourceMap); 38 | } 39 | return newFile; 40 | } 41 | 42 | module.exports = function(opts) { 43 | 'use strict'; 44 | 45 | validateOptions(opts); 46 | 47 | // Disable optimization by default. 48 | opts.optimize = opts.optimize || 'none'; 49 | 50 | // create the stream and save the file name 51 | // (opts.out will be replaced by a callback function later) 52 | var stream = es.pause(); 53 | var filename = opts.out || opts.dir; 54 | var output = null; 55 | var sourceMapOutput = null; 56 | 57 | // Set .out to a function to catch result text and sourcemap. 58 | if (opts.out) { 59 | opts.out = function(text, sourceMap) { 60 | output = text; 61 | sourceMapOutput = sourceMap; 62 | }; 63 | } 64 | 65 | var success = function(buildResponse) { 66 | if (output) { 67 | stream.write( 68 | createFile(filename, output, buildResponse, sourceMapOutput) 69 | ); 70 | } 71 | stream.resume(); 72 | stream.end(); 73 | }; 74 | var error = function(error) { 75 | stream.emit('error', new PluginError(PLUGIN_NAME, error)); 76 | }; 77 | 78 | // just a small wrapper around the r.js optimizer, we write a new gutil.File 79 | // (vinyl) to the Stream, mocking a file, which can be handled 80 | // regular gulp plugins. 81 | requirejs.optimize(opts, success, error); 82 | 83 | // return the stream for chain .pipe()ing 84 | return stream; 85 | }; 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-requirejs", 3 | "description": "Builds projects using require.js's optimizer", 4 | "version": "1.4.0", 5 | "author": "Jorrit Schippers (https://www.ncode.nl/)", 6 | "bugs": "https://github.com/jorrit/gulp-requirejs/issues", 7 | "contributors": [ 8 | "Robin Thrift (http://webbrickworks.com/)", 9 | "Jorrit Schippers (https://www.ncode.nl/)" 10 | ], 11 | "dependencies": { 12 | "event-stream": "^4.0.1", 13 | "plugin-error": "^1.0.1", 14 | "requirejs": "^2.3.6", 15 | "vinyl": "^2.2.0" 16 | }, 17 | "devDependencies": { 18 | "coveralls": "^3.1.0", 19 | "eslint": "^8.31.0", 20 | "mocha": "^10.2.0", 21 | "mocha-lcov-reporter": "^1.3.0", 22 | "nyc": "^15.1.0", 23 | "should": "^13.2.3" 24 | }, 25 | "engines": { 26 | "node": ">= 6" 27 | }, 28 | "files": [ 29 | "index.js" 30 | ], 31 | "homepage": "https://github.com/jorrit/gulp-requirejs", 32 | "keywords": [ 33 | "amd", 34 | "gulpplugin", 35 | "requirejs" 36 | ], 37 | "license": "MIT", 38 | "main": "./index.js", 39 | "repository": { 40 | "type": "git", 41 | "url": "https://github.com/jorrit/gulp-requirejs.git" 42 | }, 43 | "scripts": { 44 | "lint": "eslint .", 45 | "pretest": "npm run lint", 46 | "test": "nyc mocha", 47 | "coverage": "nyc report --reporter=text-lcov | coveralls" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test/_browser.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TESTING REQUIRE JS STUFF 6 | 7 | 8 | This document can be used to test if everything is loaded correct in the browser by require.js 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /test/expected/complex/complex_init_dir.js: -------------------------------------------------------------------------------- 1 | define('simple_amd_file',[],function() { 2 | 3 | var Mult = function(a, b) { 4 | return a * b; 5 | }; 6 | 7 | return Mult; 8 | 9 | }); 10 | 11 | // src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js 12 | (function (root, factory) { 13 | if (typeof define === 'function' && define.amd) { 14 | // AMD. Register as an anonymous module. 15 | define('umd_file',factory); 16 | } else { 17 | // Browser globals 18 | root.amdWeb = factory(root.b); 19 | } 20 | }(this, function() { 21 | //use b in some fashion. 22 | 23 | // Just return a value to define the module export. 24 | // This example returns an object, but the module 25 | // can return a function as the exported value. 26 | return { 27 | test: function() { 28 | console.log('Test Log from the UMD file'); 29 | } 30 | }; 31 | })); 32 | 33 | (function(root) { 34 | 35 | root.myLib = {}; 36 | 37 | root.myLib.sum = function(a, b) { 38 | return a + b; 39 | }; // END PROTYPE OF sum 40 | 41 | })(this); 42 | 43 | define("non_md_file", (function (global) { 44 | return function () { 45 | var ret, fn; 46 | return ret || global.myLib; 47 | }; 48 | }(this))); 49 | 50 | define('complex_amd_file',['non_md_file', 'simple_amd_file'], function(MyLib, mult) { 51 | 52 | var SumMulti = function(a, b) { 53 | return mult(MyLib.sum(a, b), b); 54 | }; 55 | 56 | return SumMulti; 57 | 58 | }); 59 | 60 | requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'], 61 | function(mult, UMDLib, sumMulti) { 62 | console.log('executing the COMPLEX init file'); 63 | console.log(mult(3, 5), '<= this should be 15'); 64 | UMDLib.test(); // should log 'Test Log from the UMD file' 65 | console.log(sumMulti(5, 8), '<= this should be 104'); 66 | } 67 | ); 68 | 69 | define("complex_init_dir", function(){}); 70 | 71 | -------------------------------------------------------------------------------- /test/expected/complex_init.js: -------------------------------------------------------------------------------- 1 | define('simple_amd_file',[],function() { 2 | 3 | var Mult = function(a, b) { 4 | return a * b; 5 | }; 6 | 7 | return Mult; 8 | 9 | }); 10 | 11 | // src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js 12 | (function (root, factory) { 13 | if (typeof define === 'function' && define.amd) { 14 | // AMD. Register as an anonymous module. 15 | define('umd_file',factory); 16 | } else { 17 | // Browser globals 18 | root.amdWeb = factory(root.b); 19 | } 20 | }(this, function() { 21 | //use b in some fashion. 22 | 23 | // Just return a value to define the module export. 24 | // This example returns an object, but the module 25 | // can return a function as the exported value. 26 | return { 27 | test: function() { 28 | console.log('Test Log from the UMD file'); 29 | } 30 | }; 31 | })); 32 | 33 | (function(root) { 34 | 35 | root.myLib = {}; 36 | 37 | root.myLib.sum = function(a, b) { 38 | return a + b; 39 | }; // END PROTYPE OF sum 40 | 41 | })(this); 42 | 43 | define("non_md_file", (function (global) { 44 | return function () { 45 | var ret, fn; 46 | return ret || global.myLib; 47 | }; 48 | }(this))); 49 | 50 | define('complex_amd_file',['non_md_file', 'simple_amd_file'], function(MyLib, mult) { 51 | 52 | var SumMulti = function(a, b) { 53 | return mult(MyLib.sum(a, b), b); 54 | }; 55 | 56 | return SumMulti; 57 | 58 | }); 59 | 60 | requirejs.config({ 61 | baseUrl: '/fixtures/vendor', 62 | 63 | shim: { 64 | 'non_md_file': { 65 | exports: 'myLib' 66 | } 67 | } 68 | }); 69 | 70 | requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'], 71 | function(mult, UMDLib, sumMulti) { 72 | console.log('executing the COMPLEX init file'); 73 | console.log(mult(3, 5), '<= this should be 15'); 74 | UMDLib.test(); // should log 'Test Log from the UMD file' 75 | console.log(sumMulti(5, 8), '<= this should be 104'); 76 | }); 77 | 78 | define("../complex_init", function(){}); 79 | 80 | 81 | define("../complex_init", function(){}); 82 | -------------------------------------------------------------------------------- /test/expected/simple_init.js: -------------------------------------------------------------------------------- 1 | define('vendor/simple_amd_file',[],function() { 2 | 3 | var Mult = function(a, b) { 4 | return a * b; 5 | }; 6 | 7 | return Mult; 8 | 9 | }); 10 | 11 | requirejs(['vendor/simple_amd_file'], function(mult) { 12 | console.log('executing the simple init file'); 13 | console.log(mult(3, 5)); 14 | }); 15 | 16 | define("simple_init", function(){}); 17 | 18 | 19 | define("simple_init", function(){}); 20 | -------------------------------------------------------------------------------- /test/expected/umd_init.js: -------------------------------------------------------------------------------- 1 | define('vendor/simple_amd_file',[],function() { 2 | 3 | var Mult = function(a, b) { 4 | return a * b; 5 | }; 6 | 7 | return Mult; 8 | 9 | }); 10 | 11 | // src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js 12 | (function (root, factory) { 13 | if (typeof define === 'function' && define.amd) { 14 | // AMD. Register as an anonymous module. 15 | define('vendor/umd_file',factory); 16 | } else { 17 | // Browser globals 18 | root.amdWeb = factory(root.b); 19 | } 20 | }(this, function() { 21 | //use b in some fashion. 22 | 23 | // Just return a value to define the module export. 24 | // This example returns an object, but the module 25 | // can return a function as the exported value. 26 | return { 27 | test: function() { 28 | console.log('Test Log from the UMD file'); 29 | } 30 | }; 31 | })); 32 | 33 | requirejs(['vendor/simple_amd_file', 'vendor/umd_file'], 34 | function(mult, UMDLib) { 35 | console.log('executing the UMD+AMD init file'); 36 | console.log(mult(3, 5)); 37 | UMDLib.test(); // should log 'Test Log from the UMD file' 38 | }); 39 | 40 | define("umd_init", function(){}); 41 | 42 | 43 | define("umd_init", function(){}); 44 | -------------------------------------------------------------------------------- /test/fixtures/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "amd": true 4 | }, 5 | "globals": { 6 | "requirejs": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/complex_init.js: -------------------------------------------------------------------------------- 1 | requirejs.config({ 2 | baseUrl: '/fixtures/vendor', 3 | 4 | shim: { 5 | 'non_md_file': { 6 | exports: 'myLib' 7 | } 8 | } 9 | }); 10 | 11 | requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'], 12 | function(mult, UMDLib, sumMulti) { 13 | console.log('executing the COMPLEX init file'); 14 | console.log(mult(3, 5), '<= this should be 15'); 15 | UMDLib.test(); // should log 'Test Log from the UMD file' 16 | console.log(sumMulti(5, 8), '<= this should be 104'); 17 | }); 18 | -------------------------------------------------------------------------------- /test/fixtures/complex_init_dir.js: -------------------------------------------------------------------------------- 1 | requirejs(['simple_amd_file', 'umd_file', 'complex_amd_file'], 2 | function(mult, UMDLib, sumMulti) { 3 | console.log('executing the COMPLEX init file'); 4 | console.log(mult(3, 5), '<= this should be 15'); 5 | UMDLib.test(); // should log 'Test Log from the UMD file' 6 | console.log(sumMulti(5, 8), '<= this should be 104'); 7 | } 8 | ); 9 | -------------------------------------------------------------------------------- /test/fixtures/config_init_dir.js: -------------------------------------------------------------------------------- 1 | //The build will inline common dependencies into this file. 2 | requirejs.config({ 3 | paths: { 4 | 'complex_init_dir': '../complex_init_dir', 5 | }, 6 | shim: { 7 | 'non_md_file': { 8 | exports: 'myLib' 9 | } 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /test/fixtures/simple_init.js: -------------------------------------------------------------------------------- 1 | requirejs(['vendor/simple_amd_file'], function(mult) { 2 | console.log('executing the simple init file'); 3 | console.log(mult(3, 5)); 4 | }); 5 | -------------------------------------------------------------------------------- /test/fixtures/umd_init.js: -------------------------------------------------------------------------------- 1 | requirejs(['vendor/simple_amd_file', 'vendor/umd_file'], 2 | function(mult, UMDLib) { 3 | console.log('executing the UMD+AMD init file'); 4 | console.log(mult(3, 5)); 5 | UMDLib.test(); // should log 'Test Log from the UMD file' 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixtures/vendor/complex_amd_file.js: -------------------------------------------------------------------------------- 1 | define(['non_md_file', 'simple_amd_file'], function(MyLib, mult) { 2 | 3 | var SumMulti = function(a, b) { 4 | return mult(MyLib.sum(a, b), b); 5 | }; 6 | 7 | return SumMulti; 8 | 9 | }); 10 | -------------------------------------------------------------------------------- /test/fixtures/vendor/non_md_file.js: -------------------------------------------------------------------------------- 1 | (function(root) { 2 | 3 | root.myLib = {}; 4 | 5 | root.myLib.sum = function(a, b) { 6 | return a + b; 7 | }; // END PROTYPE OF sum 8 | 9 | })(this); 10 | -------------------------------------------------------------------------------- /test/fixtures/vendor/simple_amd_file.js: -------------------------------------------------------------------------------- 1 | define(function() { 2 | 3 | var Mult = function(a, b) { 4 | return a * b; 5 | }; 6 | 7 | return Mult; 8 | 9 | }); 10 | -------------------------------------------------------------------------------- /test/fixtures/vendor/umd_file.js: -------------------------------------------------------------------------------- 1 | // src for the wrapper: https://github.com/umdjs/umd/blob/master/amdWeb.js 2 | (function (root, factory) { 3 | if (typeof define === 'function' && define.amd) { 4 | // AMD. Register as an anonymous module. 5 | define(factory); 6 | } else { 7 | // Browser globals 8 | root.amdWeb = factory(root.b); 9 | } 10 | }(this, function() { 11 | //use b in some fashion. 12 | 13 | // Just return a value to define the module export. 14 | // This example returns an object, but the module 15 | // can return a function as the exported value. 16 | return { 17 | test: function() { 18 | console.log('Test Log from the UMD file'); 19 | } 20 | }; 21 | })); 22 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable max-len */ 2 | /* eslint-env mocha */ 3 | var grjs = require('../'); 4 | var should = require('should'); 5 | var fs = require('fs'); 6 | 7 | require('mocha'); 8 | 9 | describe('gulp-requirejs', function() { 10 | it('should emit end when stream is done', function(done) { 11 | var stream = grjs({ 12 | out: 'simple_init.js', 13 | 14 | baseUrl: 'test/fixtures/', 15 | 16 | findNestedDependencies: true, 17 | skipPragmas: true, 18 | 19 | name: 'simple_init', 20 | 21 | include: ['simple_init'], 22 | 23 | create: true 24 | }); 25 | 26 | stream.on('end', done); 27 | }); 28 | 29 | it('should emit source map file when configured', function(done) { 30 | var stream = grjs({ 31 | out: 'simple_init.js', 32 | 33 | baseUrl: 'test/fixtures/', 34 | 35 | findNestedDependencies: true, 36 | skipPragmas: true, 37 | generateSourceMaps: true, 38 | 39 | name: 'simple_init', 40 | 41 | include: ['simple_init'], 42 | 43 | create: true 44 | }); 45 | 46 | stream.on('data', function(output) { 47 | try { 48 | output.path.should.equal('simple_init.js'); 49 | output.should.have.property('sourceMap'); 50 | output.sourceMap.should.be.Object(); 51 | output.sourceMap.sources.should.containEql('vendor/simple_amd_file.js'); 52 | output.sourceMap.sources.should.containEql('simple_init.js'); 53 | done(); 54 | } catch (e) { 55 | done(e); 56 | } 57 | }); 58 | }); 59 | 60 | describe('simple AMD file', function() { 61 | 62 | it('should concat the files in the correct order', function(done) { 63 | var stream = grjs({ 64 | out: 'simple_init.js', 65 | 66 | baseUrl: 'test/fixtures/', 67 | 68 | findNestedDependencies: true, 69 | skipPragmas: true, 70 | 71 | name: 'simple_init', 72 | 73 | include: ['simple_init'], 74 | 75 | create: true 76 | }); 77 | 78 | stream.on('data', function(output) { 79 | try { 80 | should.exist(output); 81 | should.exist(output.path); 82 | should.exist(output.relative); 83 | should.exist(output.contents); 84 | should.exist(output.buildResponse); 85 | 86 | output.relative.should.equal('simple_init.js'); 87 | String(output.contents).should.equal(fs.readFileSync('test/expected/simple_init.js', 'utf8')); 88 | output.buildResponse.should.match(/simple_init\.js[\s\S]*simple_amd_file\.js[\s\S]*simple_init\.js/m); 89 | done(); 90 | } catch (e) { 91 | done(e); 92 | } 93 | }); 94 | }); 95 | 96 | }); 97 | 98 | describe('AMD und UMD mix', function() { 99 | 100 | it('should concat the files in the correct order', function(done) { 101 | var stream = grjs({ 102 | out: 'umd_init.js', 103 | 104 | baseUrl: 'test/fixtures/', 105 | 106 | findNestedDependencies: true, 107 | skipPragmas: true, 108 | 109 | name: 'umd_init', 110 | 111 | include: ['umd_init'], 112 | 113 | create: true 114 | }); 115 | 116 | stream.on('data', function(output) { 117 | try { 118 | should.exist(output); 119 | should.exist(output.path); 120 | should.exist(output.relative); 121 | should.exist(output.contents); 122 | 123 | output.relative.should.equal('umd_init.js'); 124 | String(output.contents).should.equal(fs.readFileSync('test/expected/umd_init.js', 'utf8')); 125 | done(); 126 | } catch (e) { 127 | done(e); 128 | } 129 | }); 130 | }); 131 | 132 | }); 133 | 134 | describe('amd file with shim', function() { 135 | it('should concat the files in the correct order, and build wrappers for the shimmed files', function(done) { 136 | var stream = grjs({ 137 | out: 'complex_init.js', 138 | 139 | baseUrl: 'test/fixtures/vendor', 140 | 141 | findNestedDependencies: true, 142 | skipPragmas: true, 143 | 144 | name: '../complex_init', 145 | 146 | include: ['../complex_init'], 147 | 148 | create: true, 149 | 150 | shim: { 151 | 'non_md_file': { 152 | exports: 'myLib' 153 | } 154 | } 155 | }); 156 | 157 | stream.on('data', function(output) { 158 | try { 159 | should.exist(output); 160 | should.exist(output.path); 161 | should.exist(output.relative); 162 | should.exist(output.contents); 163 | 164 | output.relative.should.equal('complex_init.js'); 165 | String(output.contents).should.equal(fs.readFileSync('test/expected/complex_init.js', 'utf8')); 166 | done(); 167 | } catch (e) { 168 | done(e); 169 | } 170 | }); 171 | }); 172 | }); 173 | 174 | describe('amd dir with shim', function() { 175 | var pathname = 'test/tmp/'; 176 | var moduleName = 'complex_init_dir'; 177 | it('should concat the files in the correct order into modules.name, and build wrappers for the shimmed files', function(done) { 178 | grjs({ 179 | mainConfigFile: 'test/fixtures/config_init_dir.js', 180 | dir: pathname, 181 | path: { 182 | 'config': '../config_init_dir' 183 | }, 184 | modules: [{ 185 | name: moduleName, // no extension 186 | includes: [ 187 | 'simple_amd_file', 188 | 'umd_file', 189 | 'complex_amd_file', 190 | 'non_md_file' 191 | ] 192 | }], 193 | enforceDefine: true, 194 | baseUrl: 'test/fixtures/vendor', 195 | optimize: 'none', 196 | findNestedDependencies: true 197 | }); 198 | var contents = fs.readFileSync( 'test/expected/complex/' + moduleName + '.js', 'utf8'); 199 | var length = contents.length; 200 | // wait, as require.js deletes and copies files 201 | setTimeout(function(){ 202 | var test = fs.readFileSync( pathname + moduleName + '.js', 'utf8'); 203 | test.length.should.equal(length); 204 | test.should.equal(contents); 205 | done(); 206 | }, 1); 207 | }); 208 | }); 209 | 210 | describe('ERRORS: ', function() { 211 | 212 | it('should throw an error if we forget to pass in an options object', function(done) { 213 | 214 | (function() { 215 | grjs(); 216 | }).should.throwError(/^Miss.*/); 217 | 218 | done(); 219 | }); 220 | 221 | it('should throw an error if we forget to set the baseUrl', function(done) { 222 | 223 | (function() { 224 | grjs({ 225 | out: 'test.js' 226 | }); 227 | }).should.throwError(/^Pip.*/); 228 | 229 | done(); 230 | }); 231 | 232 | 233 | it('should throw an error if we forget to set the output (out or dir)', function(done) { 234 | 235 | (function() { 236 | grjs({ 237 | baseUrl: 'test/dir' 238 | }); 239 | }).should.throwError(/^Either.*/); 240 | 241 | done(); 242 | }); 243 | 244 | it('should emit an error event when the require.js optimizer finds an error', function(done) { 245 | 246 | var stream = grjs({ 247 | baseUrl: 'test/dir', 248 | out: 'testURL' 249 | }); 250 | 251 | stream.on('error', function() { 252 | done(); 253 | }); 254 | 255 | }); 256 | 257 | }); 258 | 259 | }); 260 | --------------------------------------------------------------------------------