Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 23869b0f60ffc0359ac2b26c8aad6401778a0617 @tkellen tkellen committed Sep 8, 2012
Showing with 273 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 AUTHORS
  3. +22 −0 LICENSE-MIT
  4. +42 −0 README.md
  5. +78 −0 grunt.js
  6. +41 −0 package.json
  7. +48 −0 tasks/requirejs.js
  8. +1 −0 test/fixtures/hello.js
  9. +3 −0 test/fixtures/project.js
  10. +1 −0 test/fixtures/world.js
  11. +31 −0 test/requirejs_test.js
@@ -0,0 +1,3 @@
+node_modules
+npm-debug.log
+tmp
@@ -0,0 +1,3 @@
+Tyler Kellen (http://goingslowly.com/)
+Tim Branyen (http://tbranyen.com/)
+Chris Talkington (http://christalkington.com/)
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Tyler Kellen, contributors.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
+# grunt-contrib-requirejs
+> Optimize RequireJS projects using r.js (part of the [grunt-contrib](https://github.com/gruntjs/grunt-contrib) collection). Submitted by [Tyler Kellen](https://github.com/tbranyen).
+
+## Getting Started
+Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-contrib-requirejs`
+
+Then add this line to your project's `grunt.js` gruntfile:
+
+```javascript
+grunt.loadNpmTasks('grunt-contrib-requirejs');
+```
+
+[grunt]: https://github.com/cowboy/grunt
+[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
+
+### Overview
+
+Inside your `grunt.js` file add a section named `requirejs`. This section specifies the options passed to [RequireJS Optimizer](http://requirejs.org/docs/optimization.html).
+
+#### Parameters
+
+##### options ```object```
+
+This controls how this task (and its helpers) operate and should contain key:value pairs, see options below.
+
+#### Options
+
+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).
+
+#### Config Example
+
+``` javascript
+requirejs: {
+ compile: {
+ options: {
+ baseUrl: "path/to/base",
+ mainConfigFile: "path/to/config.js",
+ out: "path/to/optimized.js"
+ }
+ }
+}
+```
@@ -0,0 +1,78 @@
+/*
+ * grunt-contrib-requirejs
+ * http://gruntjs.com/
+ *
+ * Copyright (c) 2012 Tyler Kellen, contributors
+ * Licensed under the MIT license.
+ * https://github.com/gruntjs/grunt-contrib-requirejs/blob/master/LICENSE-MIT
+ */
+
+module.exports = function(grunt) {
+ 'use strict';
+
+ // Project configuration.
+ grunt.initConfig({
+ lint: {
+ all: ['grunt.js', 'tasks/*.js', '<config:nodeunit.tasks>']
+ },
+
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ es5: true
+ }
+ },
+
+ // Before generating any new files, remove any previously-created files.
+ clean: {
+ test: ['tmp']
+ },
+
+ // Configuration to be run (and then tested).
+ requirejs: {
+ compile: {
+ options: {
+ baseUrl: 'test/fixtures',
+ name: 'project',
+ out: 'tmp/requirejs.js'
+ }
+ },
+ template: {
+ options: {
+ baseUrl: 'test/fixtures',
+ name: 'project',
+ out: 'tmp/requirejs-template.js'
+ }
+ }
+ },
+
+ // Unit tests.
+ nodeunit: {
+ tasks: ['test/*_test.js']
+ }
+ });
+
+ // Actually load this plugin's task(s).
+ grunt.loadTasks('tasks');
+
+ // The clean plugin helps in testing.
+ grunt.loadNpmTasks('grunt-contrib-clean');
+
+ // Whenever the 'test' task is run, first clean the 'tmp' dir, then run this
+ // plugin's task(s), then test the result.
+ grunt.renameTask('test', 'nodeunit');
+ grunt.registerTask('test', 'clean requirejs nodeunit');
+
+ // By default, lint and run all tests.
+ grunt.registerTask('default', 'lint test');
+};
@@ -0,0 +1,41 @@
+{
+ "name": "grunt-contrib-requirejs",
+ "description": "Optimize RequireJS projects using r.js.",
+ "version": "0.2.0",
+ "homepage": "https://github.com/gruntjs/grunt-contrib-requirejs",
+ "author": {
+ "name": "Tyler Kellen",
+ "url": "http://goingslowly.com/"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/gruntjs/grunt-contrib-requirejs.git"
+ },
+ "bugs": {
+ "url": "https://github.com/gruntjs/grunt-contrib-requirejs/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/gruntjs/grunt-contrib-requirejs/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "grunt.js",
+ "engines": {
+ "node": "*"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "dependencies": {
+ "requirejs": "~2.0.6",
+ "grunt-contrib-lib": "~0.2.0"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.15",
+ "grunt-contrib-clean": "~0.2.0"
+ },
+ "keywords": [
+ "gruntplugin"
+ ]
+}
@@ -0,0 +1,48 @@
+/*
+ * grunt-contrib-requirejs
+ * http://gruntjs.com/
+ *
+ * Copyright (c) 2012 Tyler Kellen, contributors
+ * Licensed under the MIT license.
+ * https://github.com/gruntjs/grunt-contrib-requirejs/blob/master/LICENSE-MIT
+ */
+
+module.exports = function(grunt) {
+ 'use strict';
+
+ // TODO: ditch this when grunt v0.4 is released
+ grunt.util = grunt.util || grunt.utils;
+
+ var _ = grunt.util._;
+ var kindOf = grunt.util.kindOf;
+ var helpers = require('grunt-contrib-lib').init(grunt);
+
+ var requirejs = require('requirejs');
+
+ // TODO: extend this to send build log to grunt.log.ok / grunt.log.error
+ // by overriding the r.js logger (or submit issue to r.js to expand logging support)
+ requirejs.define('node/print', [], function() {
+ return function print(msg) {
+ if (msg.substring(0, 5) === 'Error') {
+ grunt.log.errorlns(msg);
+ grunt.fail.warn('RequireJS failed.');
+ } else {
+ grunt.log.oklns(msg);
+ }
+ };
+ });
+
+ grunt.registerMultiTask('requirejs', 'Build a RequireJS project.', function() {
+ var options = helpers.options(this, {logLevel: 0});
+
+ _.each(options, function(value, key) {
+ if (kindOf(value) === 'string') {
+ options[key] = grunt.template.process(value);
+ }
+ });
+
+ grunt.verbose.writeflags(options, 'Options');
+
+ requirejs.optimize(options);
+ });
+};
@@ -0,0 +1 @@
+define(function(){return "hello";});
@@ -0,0 +1,3 @@
+require(['hello', 'world'], function(hello, world) {
+ console.log(hello,world);
+});
@@ -0,0 +1 @@
+define(function(){return "world";});
@@ -0,0 +1,31 @@
+var grunt = require('grunt');
+
+exports['requirejs'] = {
+ main: function(test) {
+ 'use strict';
+
+ var expect, result;
+
+ test.expect(1);
+
+ expect = 'define("hello",[],function(){return"hello"}),define("world",[],function(){return"world"}),require(["hello","world"],function(e,t){console.log(e,t)}),define("project",function(){})';
+ result = grunt.file.read('tmp/requirejs.js');
+ test.equal(expect, result, 'should optimize javascript modules with requireJS');
+
+ test.done();
+ },
+
+ template: function(test) {
+ 'use strict';
+
+ var expect, result;
+
+ test.expect(1);
+
+ expect = 'define("hello",[],function(){return"hello"}),define("world",[],function(){return"world"}),require(["hello","world"],function(e,t){console.log(e,t)}),define("project",function(){})';
+ result = grunt.file.read('tmp/requirejs-template.js');
+ test.equal(expect, result, 'should process options with template variables.');
+
+ test.done();
+ }
+};

0 comments on commit 23869b0

Please sign in to comment.