-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGruntfile.js
188 lines (161 loc) · 5.92 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var assert = require('assert');
var expectedBundles = 4;
/**
* Tests provided build object
* against list of expected bundles
*
* @param {object} options - config object for the task
* @param {function} cb - callback function
* @param {object} buildObject - generated options object for r.js
* @returns {void}
*/
function testBuild(options, cb, buildObject)
{
// pretending writing to a file
setTimeout(function()
{
// it will be invoked for each bundle with respective buildObject
if (buildObject)
{
assert(buildObject.name in options);
expectedBundles--;
}
// and without arguments after all bundles have been processed
else
{
assert.strictEqual(0, expectedBundles);
cb();
}
}, 10);
}
module.exports = function(grunt)
{
// Project configuration.
grunt.initConfig({
'eslint':
{
options:
{
configFile: '.eslintrc'
},
target: ['tasks/multibundle-requirejs.js', 'Gruntfile.js']
},
'clean':
{
tests: ['test/tmp']
},
'file_compare':
{
check_common : ['test/fixtures/expected/common.8c4a46cef59ebdd05e6cc94e41d7e574.js', 'test/tmp/common.8c4a46cef59ebdd05e6cc94e41d7e574.js'],
check_maps : ['test/fixtures/expected/maps.b371bd03bd498257ded2aae300de5d13.js', 'test/tmp/maps.b371bd03bd498257ded2aae300de5d13.js'],
check_optional: ['test/fixtures/expected/optional.f242c6ec7db101d485e624c441061180.js', 'test/tmp/optional.f242c6ec7db101d485e624c441061180.js'],
check_user : ['test/fixtures/expected/user.2a25559b93ae406914f97d940e98aa3d.js', 'test/tmp/user.2a25559b93ae406914f97d940e98aa3d.js']
},
// Configuration to be run (and then tested).
'multibundle-requirejs':
{
options:
{
'_config':
{
// 4 is silent in r.js world
logLevel: process.env.quiet ? 4 : 1,
destination: 'test/tmp',
sharedBundles: ['optional', 'common'],
// or custom function `hashFiles(output, componentOptions)`
hashFiles: true,
// should be a function that returns
// mapper instance (receiving function or writable stream)
// Note: it needs to be wrapper into a function
// to prevent grunt messing up with the object's state
handleMapping: function(options, gruntInstance, done)
{
// will be called one extra time with no arguments after all the bundles processed
// also accepts writable streams in object mode, (e.g. `multibundle-mapper`)
return testBuild.bind(null, options, function()
{
console.log('Finished all bundles.');
// grunt style callback
done(true);
});
},
// pass options to r.js
baseUrl: '.',
optimize: 'uglify',
sharedPaths:
{
// test location namespacing
'app' : 'test/fixtures/input/app',
'assets': 'test/fixtures/input/assets',
// needed for rendr modules
'rendr' : 'node_modules/rendr'
},
preserveLicenseComments: false
},
// optional modules
'optional':
[
{'omniture' : 'assets/vendor/s_code.js'},
'app/lib/tracking/pixel.js',
'app/lib/tracking/omniture.js'
],
// Creates `<destination>/common.<hash>.js` file that includes all the modules specified in the bundle,
// shared modules between all the pages.
'common':
[
// node modules
{'requirejs' : 'node_modules/requirejs/require.js'},
// multiple entry points module
{'rendr/shared' : 'node_modules/rendr/shared/app.js'},
{'rendr/client' : 'node_modules/rendr/client/router.js'},
// module that requires files directly
// it needs `nodeIdCompat:true`
{'deeply' : 'node_modules/deeply/index.js'},
// modules needed to be shimmed
{'async' : {src: 'node_modules/async/lib/async.js', exports: 'async'}},
// module with implicit dependencies
{'backbone' : {src: 'node_modules/backbone/backbone.js', deps: ['jquery', 'underscore', 'jqueryHammer'], exports: 'Backbone'}},
// replace underscore with lodash
{'underscore' : {src: 'node_modules/lodash/index.js', exports: '_'}},
// checked in assets
{'hammer' : 'assets/vendor/hammer.js'},
// assets needed to be shimmed
{'jquery' : {src: 'assets/vendor/jquery.js', exports: 'jQuery'}},
// execute plugin to add methods to jQuery
{'jqueryHammer' : {src: 'assets/vendor/jquery.hammer.js', deps: ['jquery', 'hammer'] , insertRequire: true}},
// main script
{'main' : 'app/main.js'},
// app helper files
'app/helper*.js',
// lib
'app/lib/**/*.js'
],
// Creates separate bundle for user page components – `<destination>/user.<hash>.js`
'user':
[
'app/models/user/**/*.js',
'app/views/user/**/*.js'
],
// Creates separate bundle for map page components – `<destination>/maps.<hash>.js`
'maps':
[
'app/models/maps/**/*.js',
'app/views/maps/**/*.js'
]
}
}
});
// Actually load this plugin's task(s).
grunt.loadTasks('tasks');
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-file-compare');
// Whenever the "test" task is run, run this plugin's task(s),
// then test the result.
grunt.registerTask('test', ['clean', 'multibundle-requirejs', 'file_compare']);
// use eslint for linting
grunt.registerTask('lint', ['eslint']);
// By default, lint and run all tests.
grunt.registerTask('default', ['lint', 'test']);
};