Skip to content

Commit d0cf594

Browse files
committed
testing rjs+almond task
1 parent 229b8a6 commit d0cf594

File tree

14 files changed

+166
-38
lines changed

14 files changed

+166
-38
lines changed

tasks/rjs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module.exports = function(grunt) {
2222
if (options.almond === true) {
2323
grunt.log.ok('Including almond.js');
2424
// resolve almond path
25+
if (typeof options.paths === 'undefined') {
26+
options.paths = {};
27+
}
2528
options.paths.almond = require.resolve('almond').replace('.js', '');
2629
// include almond to each module
2730
options.modules.forEach(function (module, idx) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define(["require","exports","module"],function(a,b){b.name="alpha"})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define(["./sub/betaSub"],function(a){return{name:"beta",subName:a.name}})

test/fixtures/rjs-almond/expected/main.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/rjs-almond/expected/plugins.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define({name:"betaSubName"})

test/fixtures/rjs-almond/grunt.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*global module:false*/
2+
module.exports = function(grunt) {
3+
4+
// Project configuration.
5+
grunt.initConfig({
6+
// the staging directory used during the process
7+
staging: 'intermediate',
8+
// final build output
9+
output: 'publish',
10+
// filter any files matching one of the below pattern during mkdirs task
11+
// the pattern in the .gitignore file should work too.
12+
exclude: '.git* build/** node_modules/** grunt.js package.json *.md'.split(' '),
13+
mkdirs: {
14+
staging: '<config:exclude>'
15+
},
16+
// concat css/**/*.css files, inline @import, output a single minified css
17+
css: {
18+
'css/style.css': ['css/**/*.css']
19+
},
20+
// Renames JS/CSS to prepend a hash of their contents for easier
21+
// versioning
22+
rev: {
23+
js: 'js/**/*.js',
24+
css: 'css/**/*.css',
25+
img: 'img/**'
26+
},
27+
// update references in html to revved files
28+
usemin: {
29+
files: ['**/*.html']
30+
},
31+
// html minification
32+
html: '<config:usemin>',
33+
// Optimizes JPGs and PNGs (with jpegtran & optipng)
34+
img: {
35+
dist: '<config:rev.img>'
36+
},
37+
watch: {
38+
files: '<config:lint.files>',
39+
tasks: 'lint qunit'
40+
},
41+
lint: {
42+
files: ['grunt.js', 'js/**/*.js', 'test/**/*.js']
43+
},
44+
qunit: {
45+
files: ['test/**/*.html']
46+
},
47+
jshint: {
48+
options: {
49+
curly: true,
50+
eqeqeq: true,
51+
immed: true,
52+
latedef: true,
53+
newcap: true,
54+
noarg: true,
55+
sub: true,
56+
undef: true,
57+
boss: true,
58+
eqnull: true,
59+
browser: true
60+
},
61+
globals: {
62+
jQuery: true
63+
}
64+
},
65+
rjs: {
66+
almond: true,
67+
modules: [{
68+
name: 'main',
69+
}],
70+
dir: 'js',
71+
appDir: 'js',
72+
baseUrl: './',
73+
pragmas: {
74+
doExclude: true
75+
},
76+
skipModuleInsertion: false,
77+
optimizeAllPluginResources: true,
78+
findNestedDependencies: true
79+
}
80+
});
81+
82+
83+
// in rjs setup, the concat and min task are overriden to use rjs optimizr
84+
grunt.renameTask('concat', '_concat').registerTask('concat', 'rjs (noop)', function() {
85+
grunt.log.writeln('the concat in rjs setup is a noop, rjs optimizer somewhat replace js concatenation');
86+
});
87+
grunt.renameTask('min', '_min').registerTask('min', 'rjs');
88+
89+
90+
// uncomment this line if you're using the build script as a grunt plugin
91+
// it should be installed locally, even better if put in your package.json's
92+
// dependency
93+
//
94+
grunt.loadNpmTasks(require('path').join(__dirname, '../'));
95+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define(function(require, exports) {
2+
exports.name = 'alpha';
3+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['./sub/betaSub'], function (betaSub) {
2+
return {
3+
name: 'beta',
4+
subName: betaSub.name
5+
};
6+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require(['alpha', 'beta'], function (alpha, beta) {
2+
});
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define({
2+
name: 'betaSubName'
3+
});

test/fixtures/rjs/expected/build.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/helpers/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,11 @@ helpers.copyFile = function(src, dst, cb) {
247247
fs.createReadStream(src).pipe(fs.createWriteStream(dst)).on('close', cb);
248248
});
249249
};
250+
251+
// a simple file remove helper
252+
helpers.rmFile = function(path, cb) {
253+
if (!cb) {
254+
return fs.unlinkSync(path);
255+
}
256+
fs.unlink(path, cb);
257+
}

test/tasks/test-rjs.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,62 @@
44

55
var helpers = require('../helpers');
66

7-
describe("RJS task", function() {
7+
var describeTask = function(taskName, fixturePath) {
88

9-
before(function(done) {
10-
helpers.before(function(err) {
11-
if(err) return done(err);
9+
describe(taskName, function() {
1210

13-
helpers.copyFile('test/fixtures/rjs/grunt.js', '.test/grunt.js', function(err) {
11+
before(function(done) {
12+
helpers.before(function(err) {
1413
if(err) return done(err);
15-
var files = [
16-
'rjs/sample/alpha.js',
17-
'rjs/sample/beta.js',
18-
'rjs/sample/main.js'
19-
];
2014

21-
helpers.copy(files, '.test/js', function(err) {
15+
helpers.copyFile(fixturePath + 'grunt.js', '.test/grunt.js', function(err) {
2216
if(err) return done(err);
23-
helpers.copyFile('test/fixtures/rjs/sample/sub/betaSub.js', '.test/js/sub/betaSub.js', done);
17+
var files = [
18+
'rjs/sample/alpha.js',
19+
'rjs/sample/beta.js',
20+
'rjs/sample/main.js'
21+
];
22+
23+
helpers.copy(files, '.test/js', function(err) {
24+
if(err) return done(err);
25+
helpers.copyFile(fixturePath + 'sample/sub/betaSub.js', '.test/js/sub/betaSub.js', done);
26+
});
2427
});
2528
});
2629
});
27-
});
2830

29-
describe("As a build script user I want to be able to run the rjs task So that I can see the rjs task in action", function() {
31+
describe("As a build script user I want to be able to run the rjs task So that I can see the rjs task in action", function() {
3032

31-
describe("rjs task", function() {
33+
describe("rjs task", function() {
3234

33-
// XXX the rjs task takes up to 8s on my machine with almost nothing to
34-
// process (sample files are really basic...)
35-
// figure out what's going wrong
36-
it("Given I run the 'rjs' task", function(done) {
37-
// runt the rjs task
38-
helpers.run('rjs', done);
39-
});
35+
// XXX the rjs task takes up to 8s on my machine with almost nothing to
36+
// process (sample files are really basic...)
37+
// figure out what's going wrong
38+
it("Given I run the 'rjs' task", function(done) {
39+
// runt the rjs task
40+
helpers.run('rjs', done);
41+
});
4042

41-
it("When the script ends", function(done) {
42-
// not doing anything particularly usefull in this step but the hook is here
43-
// if we need to
44-
done();
45-
});
43+
it("When the script ends", function(done) {
44+
// remove build.txt as it contains system-dependent almond path
45+
helpers.rmFile('.test/js/build.txt', function(err) {
46+
if (err) return done(err);
47+
return done();
48+
});
49+
});
50+
51+
it("Then '.test/js' should be the same as " + fixturePath + "expected/'", function(done) {
52+
helpers.assertDir('.test/js', fixturePath + 'expected/');
53+
done();
54+
});
4655

47-
it("Then '.test/js' should be the same as 'test/fixtures/rjs/expected/'", function(done) {
48-
helpers.assertDir('.test/js', 'test/fixtures/rjs/expected/');
49-
done();
5056
});
5157

5258
});
5359

5460
});
5561

56-
});
62+
};
63+
64+
describeTask("RJS task", 'test/fixtures/rjs/');
65+
describeTask("RJS task (almond)", 'test/fixtures/rjs-almond/');

0 commit comments

Comments
 (0)