├── .gitattributes ├── .gitignore ├── test ├── fixtures │ ├── hello.js │ ├── world.js │ └── project.js └── requirejs_test.js ├── AUTHORS ├── CONTRIBUTING.md ├── docs ├── requirejs-overview.md ├── overview.md ├── requirejs-options.md └── requirejs-examples.md ├── .travis.yml ├── .jshintrc ├── package.json ├── appveyor.yml ├── LICENSE-MIT ├── CHANGELOG ├── tasks └── requirejs.js ├── Gruntfile.js └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | tmp -------------------------------------------------------------------------------- /test/fixtures/hello.js: -------------------------------------------------------------------------------- 1 | define(function(){return "hello";}); 2 | -------------------------------------------------------------------------------- /test/fixtures/world.js: -------------------------------------------------------------------------------- 1 | define(function(){return "world";}); 2 | -------------------------------------------------------------------------------- /test/fixtures/project.js: -------------------------------------------------------------------------------- 1 | require(['hello', 'world'], function(hello, world) { 2 | console.log(hello,world); 3 | }); 4 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Tyler Kellen (http://goingslowly.com/) 2 | Tim Branyen (http://tbranyen.com/) 3 | Chris Talkington (http://christalkington.com/) -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please see the [Contributing to grunt](http://gruntjs.com/contributing) guide for information on contributing to this project. 2 | -------------------------------------------------------------------------------- /docs/requirejs-overview.md: -------------------------------------------------------------------------------- 1 | Task targets and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide. -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | node_js: 6 | - "0.10" 7 | - "0.12" 8 | - "4" 9 | - "5" 10 | - "iojs" 11 | 12 | matrix: 13 | fast_finish: true 14 | 15 | cache: 16 | directories: 17 | - node_modules 18 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "boss": true, 3 | "curly": true, 4 | "eqeqeq": true, 5 | "eqnull": true, 6 | "immed": true, 7 | "latedef": true, 8 | "newcap": true, 9 | "noarg": true, 10 | "node": true, 11 | "sub": true, 12 | "undef": true, 13 | "unused": true 14 | } 15 | -------------------------------------------------------------------------------- /docs/overview.md: -------------------------------------------------------------------------------- 1 | *This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that [you upgrade](http://gruntjs.com/upgrading-from-0.3-to-0.4), but in case you can't please use [v0.3.3](https://github.com/gruntjs/grunt-contrib-requirejs/tree/grunt-0.3-stable).* 2 | -------------------------------------------------------------------------------- /docs/requirejs-options.md: -------------------------------------------------------------------------------- 1 | # Options 2 | 3 | For a full list of possible options, [see the r.js example build file](https://github.com/jrburke/r.js/blob/master/build/example.build.js). 4 | 5 | ## done(done, build) 6 | 7 | The done option is an optional hook to receive the r.js build output. The first argument is the grunt async callback that you are required to call if you provide the done hook. This informs grunt that the task is complete. The second parameter is the build output from r.js. 8 | 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-contrib-requirejs", 3 | "description": "Optimize RequireJS projects using r.js", 4 | "version": "1.0.0", 5 | "author": { 6 | "name": "Grunt Team", 7 | "url": "http://gruntjs.com/" 8 | }, 9 | "repository": "gruntjs/grunt-contrib-requirejs", 10 | "license": "MIT", 11 | "engines": { 12 | "node": ">=0.10.0" 13 | }, 14 | "main": "tasks/requirejs.js", 15 | "scripts": { 16 | "test": "grunt test" 17 | }, 18 | "dependencies": { 19 | "requirejs": "^2.2.0" 20 | }, 21 | "devDependencies": { 22 | "grunt": "^1.0.0", 23 | "grunt-contrib-clean": "^1.0.0", 24 | "grunt-contrib-internal": "^1.1.0", 25 | "grunt-contrib-jshint": "^1.0.0", 26 | "grunt-contrib-nodeunit": "^1.0.0" 27 | }, 28 | "keywords": [ 29 | "gruntplugin" 30 | ], 31 | "files": [ 32 | "tasks" 33 | ], 34 | "appveyor_id": "ithfwohrb4u06rx3" 35 | } 36 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | clone_depth: 10 2 | 3 | version: "{build}" 4 | 5 | # What combinations to test 6 | environment: 7 | matrix: 8 | - nodejs_version: "0.10" 9 | platform: x86 10 | - nodejs_version: "0.12" 11 | platform: x86 12 | - nodejs_version: "4" 13 | platform: x64 14 | - nodejs_version: "4" 15 | platform: x86 16 | - nodejs_version: "5" 17 | platform: x86 18 | 19 | install: 20 | - ps: Install-Product node $env:nodejs_version $env:platform 21 | - npm install 22 | 23 | test_script: 24 | # Output useful info for debugging 25 | - node --version && npm --version 26 | # We test multiple Windows shells because of prior stdout buffering issues 27 | # filed against Grunt. https://github.com/joyent/node/issues/3584 28 | - ps: "npm test # PowerShell" # Pass comment to PS for easier debugging 29 | - cmd: npm test 30 | 31 | build: off 32 | 33 | matrix: 34 | fast_finish: true 35 | 36 | cache: 37 | - node_modules -> package.json 38 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Tyler Kellen, contributors 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /test/requirejs_test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var grunt = require('grunt'); 4 | 5 | exports.requirejs = { 6 | main: function(test) { 7 | var expect, result; 8 | 9 | test.expect(1); 10 | 11 | expect = 'define("hello",[],function(){return"hello"}),define("world",[],function(){return"world"}),require(["hello","world"],function(e,n){console.log(e,n)}),define("project",function(){});'; 12 | result = grunt.file.read('tmp/requirejs.js'); 13 | test.equal(expect, result, 'should optimize javascript modules with requireJS'); 14 | 15 | test.done(); 16 | }, 17 | 18 | template: function(test) { 19 | var expect, result; 20 | 21 | test.expect(1); 22 | 23 | expect = 'define("hello",[],function(){return"hello"}),define("world",[],function(){return"world"}),require(["hello","world"],function(e,n){console.log(e,n)}),define("project",function(){});'; 24 | result = grunt.file.read('tmp/requirejs-template.js'); 25 | test.equal(expect, result, 'should process options with template variables.'); 26 | 27 | test.done(); 28 | }, 29 | 30 | done: function(test) { 31 | var expect, result; 32 | 33 | test.expect(1); 34 | 35 | expect = 7; 36 | result = grunt.file.read('tmp/done-build.txt').split('\n').length; 37 | test.equal(expect, result, 'should provide a done hook with the output'); 38 | 39 | test.done(); 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /docs/requirejs-examples.md: -------------------------------------------------------------------------------- 1 | # Usage Examples 2 | 3 | ```js 4 | requirejs: { 5 | compile: { 6 | options: { 7 | baseUrl: 'path/to/base', 8 | mainConfigFile: 'path/to/config.js', 9 | name: 'path/to/almond', /* assumes a production build using almond, if you don't use almond, you 10 | need to set the "includes" or "modules" option instead of name */ 11 | include: [ 'src/main.js' ], 12 | out: 'path/to/optimized.js' 13 | } 14 | } 15 | } 16 | ``` 17 | 18 | ## Done 19 | 20 | ```js 21 | requirejs: { 22 | compile: { 23 | options: { 24 | baseUrl: 'path/to/base', 25 | mainConfigFile: 'path/to/config.js', 26 | done: function(done, output) { 27 | var duplicates = require('rjs-build-analysis').duplicates(output); 28 | 29 | if (Object.keys(duplicates).length) { 30 | grunt.log.subhead('Duplicates found in requirejs build:'); 31 | grunt.log.warn(duplicates); 32 | return done(new Error('r.js built duplicate modules, please check the excludes option.')); 33 | } 34 | 35 | done(); 36 | } 37 | } 38 | } 39 | } 40 | ``` 41 | 42 | ## Error 43 | 44 | ```js 45 | requirejs: { 46 | compile: { 47 | options: { 48 | baseUrl: 'path/to/base', 49 | mainConfigFile: 'path/to/config.js', 50 | error: function(done, err) { 51 | grunt.log.warn(err); 52 | done(); 53 | } 54 | } 55 | } 56 | } 57 | ``` 58 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | v1.0.0: 2 | date: 2016-03-04 3 | changes: 4 | - Update usage example to show a working usage. 5 | - Remove peerDep and point to main task. 6 | - rjs-build-analysis returns an object not an array. 7 | - Made clear that usage of almond is not required. 8 | - Added error option to handle r.js errors. 9 | v0.4.4: 10 | date: 2014-04-25 11 | changes: 12 | - Reduce logging verbosity unless `--verbose` flag is used. 13 | v0.4.3: 14 | date: 2014-02-26 15 | changes: 16 | - Remove "Gruntfile.js" as package.json main. 17 | v0.4.2: 18 | date: 2014-02-26 19 | changes: 20 | - Catch exceptions in `done`. 21 | v0.4.1: 22 | date: 2013-05-16 23 | changes: 24 | - Add `done` option. 25 | v0.4.0: 26 | date: 2013-02-15 27 | changes: 28 | - First official release for Grunt 0.4.0. 29 | v0.4.0rc7: 30 | date: 2013-01-23 31 | changes: 32 | - Updating to work with grunt v0.4.0rc7. 33 | v0.4.0rc5: 34 | date: 2013-01-09 35 | changes: 36 | - Updating to work with grunt v0.4.0rc5. 37 | v0.3.3: 38 | date: 2012-10-12 39 | changes: 40 | - Rename grunt-contrib-lib dep to grunt-lib-contrib. 41 | v0.3.1: 42 | date: 2012-10-09 43 | changes: 44 | - Bump to RequireJS 2.1.x. 45 | - Run optimizer async. 46 | v0.3.0: 47 | date: 2012-09-23 48 | changes: 49 | - Options no longer accepted from global config key. 50 | v0.2.0: 51 | date: 2012-09-10 52 | changes: 53 | - Refactored from grunt-contrib into individual repo. 54 | -------------------------------------------------------------------------------- /tasks/requirejs.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-contrib-requirejs 3 | * http://gruntjs.com/ 4 | * 5 | * Copyright (c) 2016 Tyler Kellen, contributors 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | var requirejs = require('requirejs'); 14 | var LOG_LEVEL_TRACE = 0, LOG_LEVEL_WARN = 2; 15 | 16 | // TODO: extend this to send build log to grunt.log.ok / grunt.log.error 17 | // by overriding the r.js logger (or submit issue to r.js to expand logging support) 18 | requirejs.define('node/print', [], function() { 19 | return function print(msg) { 20 | if (msg.substring(0, 5) === 'Error') { 21 | grunt.log.errorlns(msg); 22 | grunt.fail.warn('RequireJS failed.'); 23 | } else { 24 | grunt.log.oklns(msg); 25 | } 26 | }; 27 | }); 28 | 29 | grunt.registerMultiTask('requirejs', 'Build a RequireJS project.', function() { 30 | 31 | var done = this.async(); 32 | var options = this.options({ 33 | logLevel: grunt.option('verbose') ? LOG_LEVEL_TRACE : LOG_LEVEL_WARN, 34 | error: false, 35 | done: function(done) { 36 | done(); 37 | } 38 | }); 39 | // The following catches errors in the user-defined `done` function and outputs them. 40 | var tryCatchDone = function(fn, done, output) { 41 | try { 42 | fn(done, output); 43 | } catch (e) { 44 | grunt.fail.warn('There was an error while processing your done function: "' + e + '"'); 45 | } 46 | }; 47 | 48 | // The following catches errors in the user-defined `error` function and passes them. 49 | // if the error function options is not set, this value should be undefined 50 | var tryCatchError = function(fn, done, err) { 51 | try { 52 | fn(done, err); 53 | } catch (e) { 54 | grunt.fail.fatal('There was an error while processing your error function: "' + e + '"'); 55 | } 56 | }; 57 | 58 | requirejs.optimize( 59 | options, 60 | tryCatchDone.bind(null, options.done, done), 61 | options.error ? tryCatchError.bind(null, options.error, done):undefined 62 | ); 63 | 64 | }); 65 | }; 66 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-contrib-requirejs 3 | * http://gruntjs.com/ 4 | * 5 | * Copyright (c) 2016 Tyler Kellen, contributors 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | // Project configuration. 14 | grunt.initConfig({ 15 | jshint: { 16 | all: [ 17 | 'Gruntfile.js', 18 | 'tasks/*.js', 19 | '<%= nodeunit.tests %>' 20 | ], 21 | options: { 22 | jshintrc: '.jshintrc' 23 | } 24 | }, 25 | 26 | // Before generating any new files, remove any previously-created files. 27 | clean: { 28 | test: ['tmp'] 29 | }, 30 | 31 | // Configuration to be run (and then tested). 32 | requirejs: { 33 | compile: { 34 | options: { 35 | baseUrl: 'test/fixtures', 36 | name: 'project', 37 | out: 'tmp/requirejs.js' 38 | } 39 | }, 40 | template: { 41 | options: { 42 | baseUrl: 'test/fixtures', 43 | name: 'project', 44 | out: 'tmp/requirejs-template.js' 45 | } 46 | }, 47 | onOptimize: { 48 | options: { 49 | baseUrl: 'test/fixtures', 50 | name: 'project', 51 | out: 'tmp/requirejs-onoptimize.js', 52 | done: function(done, build) { 53 | grunt.file.write('tmp/done-build.txt', build); 54 | done(); 55 | } 56 | } 57 | } 58 | }, 59 | 60 | // Unit tests. 61 | nodeunit: { 62 | tests: ['test/*_test.js'] 63 | } 64 | }); 65 | 66 | // Actually load this plugin's task(s). 67 | grunt.loadTasks('tasks'); 68 | 69 | // These plugins provide necessary tasks. 70 | grunt.loadNpmTasks('grunt-contrib-clean'); 71 | grunt.loadNpmTasks('grunt-contrib-jshint'); 72 | grunt.loadNpmTasks('grunt-contrib-nodeunit'); 73 | grunt.loadNpmTasks('grunt-contrib-internal'); 74 | 75 | // Whenever the "test" task is run, first clean the "tmp" dir, then run this 76 | // plugin's task(s), then test the result. 77 | grunt.registerTask('test', ['clean', 'jshint', 'requirejs', 'nodeunit']); 78 | 79 | // By default, lint and run all tests. 80 | grunt.registerTask('default', ['test', 'build-contrib']); 81 | 82 | }; 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-contrib-requirejs v1.0.0 [![Build Status: Linux](https://travis-ci.org/gruntjs/grunt-contrib-requirejs.svg?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-requirejs) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/ithfwohrb4u06rx3/branch/master?svg=true)](https://ci.appveyor.com/project/gruntjs/grunt-contrib-requirejs/branch/master) 2 | 3 | > Optimize RequireJS projects using r.js 4 | 5 | 6 | 7 | ## Getting Started 8 | 9 | If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: 10 | 11 | ```shell 12 | npm install grunt-contrib-requirejs --save-dev 13 | ``` 14 | 15 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: 16 | 17 | ```js 18 | grunt.loadNpmTasks('grunt-contrib-requirejs'); 19 | ``` 20 | 21 | *This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that [you upgrade](http://gruntjs.com/upgrading-from-0.3-to-0.4), but in case you can't please use [v0.3.3](https://github.com/gruntjs/grunt-contrib-requirejs/tree/grunt-0.3-stable).* 22 | 23 | 24 | 25 | ## Requirejs task 26 | _Run this task with the `grunt requirejs` command._ 27 | 28 | Task targets and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide. 29 | ### Options 30 | 31 | For a full list of possible options, [see the r.js example build file](https://github.com/jrburke/r.js/blob/master/build/example.build.js). 32 | 33 | #### done(done, build) 34 | 35 | The done option is an optional hook to receive the r.js build output. The first argument is the grunt async callback that you are required to call if you provide the done hook. This informs grunt that the task is complete. The second parameter is the build output from r.js. 36 | 37 | 38 | ### Usage Examples 39 | 40 | ```js 41 | requirejs: { 42 | compile: { 43 | options: { 44 | baseUrl: 'path/to/base', 45 | mainConfigFile: 'path/to/config.js', 46 | name: 'path/to/almond', /* assumes a production build using almond, if you don't use almond, you 47 | need to set the "includes" or "modules" option instead of name */ 48 | include: [ 'src/main.js' ], 49 | out: 'path/to/optimized.js' 50 | } 51 | } 52 | } 53 | ``` 54 | 55 | #### Done 56 | 57 | ```js 58 | requirejs: { 59 | compile: { 60 | options: { 61 | baseUrl: 'path/to/base', 62 | mainConfigFile: 'path/to/config.js', 63 | done: function(done, output) { 64 | var duplicates = require('rjs-build-analysis').duplicates(output); 65 | 66 | if (Object.keys(duplicates).length) { 67 | grunt.log.subhead('Duplicates found in requirejs build:'); 68 | grunt.log.warn(duplicates); 69 | return done(new Error('r.js built duplicate modules, please check the excludes option.')); 70 | } 71 | 72 | done(); 73 | } 74 | } 75 | } 76 | } 77 | ``` 78 | 79 | #### Error 80 | 81 | ```js 82 | requirejs: { 83 | compile: { 84 | options: { 85 | baseUrl: 'path/to/base', 86 | mainConfigFile: 'path/to/config.js', 87 | error: function(done, err) { 88 | grunt.log.warn(err); 89 | done(); 90 | } 91 | } 92 | } 93 | } 94 | ``` 95 | 96 | 97 | ## Release History 98 | 99 | * 2016-03-04   v1.0.0   Update usage example to show a working usage. Remove peerDep and point to main task. rjs-build-analysis returns an object not an array. Made clear that usage of almond is not required. Added error option to handle r.js errors. 100 | * 2014-04-25   v0.4.4   Reduce logging verbosity unless `--verbose` flag is used. 101 | * 2014-02-26   v0.4.3   Remove "Gruntfile.js" as package.json main. 102 | * 2014-02-26   v0.4.2   Catch exceptions in `done`. 103 | * 2013-05-16   v0.4.1   Add `done` option. 104 | * 2013-02-15   v0.4.0   First official release for Grunt 0.4.0. 105 | * 2013-01-23   v0.4.0rc7   Updating to work with grunt v0.4.0rc7. 106 | * 2013-01-09   v0.4.0rc5   Updating to work with grunt v0.4.0rc5. 107 | * 2012-10-12   v0.3.3   Rename grunt-contrib-lib dep to grunt-lib-contrib. 108 | * 2012-10-09   v0.3.1   Bump to RequireJS 2.1.x. Run optimizer async. 109 | * 2012-09-23   v0.3.0   Options no longer accepted from global config key. 110 | * 2012-09-10   v0.2.0   Refactored from grunt-contrib into individual repo. 111 | 112 | --- 113 | 114 | Task submitted by [Tyler Kellen](http://goingslowly.com/) 115 | 116 | *This file was generated on Thu Apr 07 2016 15:25:57.* 117 | --------------------------------------------------------------------------------