-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
391 lines (359 loc) · 14.2 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*global module:false*/
module.exports = function (grunt) {
var LIVERELOAD_PORT = 35731,
HTTP_PORT = 9400,
KARMA_PORT = 9877,
WATCHED_FILES_SRC = [
'src/**/*'
],
WATCHED_FILES_SPEC = [
'spec/**/*'
],
WATCHED_FILES_DIST = [
'dist/**/*'
],
WATCHED_FILES_DEMO = [
'demo/**/*'
],
SINON_SOURCE_DIR = 'node_modules/karma-chai-plugins/node_modules/sinon/lib/sinon/',
path = require( "path" ),
requireFromString = require( "require-from-string" ),
/**
* Receives an object and the name of a path property on that object. Translates the path property to a new path,
* based on a directory prefix. Does not return anything, modifies the object itself.
*
* The directory prefix can be relative (e.g. "../../"). It may or may not end in a slash.
*
* @param {string} dirPrefix
* @param {Object} object
* @param {string} propertyName
* @param {boolean} [verbose=false]
*/
translatePathProperty = function ( dirPrefix, object, propertyName, verbose ) {
var originalPath = object[propertyName];
if ( originalPath ) {
object[propertyName] = path.normalize( dirPrefix + path.sep + originalPath );
if ( verbose ) grunt.log.writeln( 'Translating path property "' + propertyName + '": ' + originalPath + " => " + object[propertyName] );
}
},
/**
* Reads an r.js build profile and returns it as an options set for a grunt-contrib-requirejs task.
*
* For a discussion, see https://github.com/gruntjs/grunt-contrib-requirejs/issues/13
*
* Paths in the build profile are relative to the profile location. In the returned options object, they are
* transformed to be relative to the Gruntfile. (The list is nowhere near complete. More properties need to be
* transformed as build profiles become more complex.)
*
* @param {string} profilePath relative to the Gruntfile
* @param {boolean} [verbose=false]
* @returns {Object}
*/
getRequirejsBuildProfile = function ( profilePath, verbose ) {
var profileContent = grunt.file.read( profilePath ),
profile = requireFromString( "module.exports = " + profileContent ),
dirPrefix = path.dirname( profilePath );
if ( verbose ) grunt.log.writeln( "Loading r.js build profile " + profilePath );
// Add more paths here as needed.
translatePathProperty( dirPrefix, profile, "mainConfigFile", verbose );
translatePathProperty( dirPrefix, profile, "out", verbose );
if ( verbose ) grunt.log.writeln();
return profile;
};
// Project configuration.
grunt.config.init({
pkg: grunt.file.readJSON('package.json'),
meta: {
version: '<%= pkg.version %>',
banner: '// Marionette.Handlebars, v<%= meta.version %>\n' +
'// Copyright (c) 2015-<%= grunt.template.today("yyyy") %> Michael Heim, Zeilenwechsel.de\n' +
'// Distributed under MIT license\n' +
'// http://github.com/hashchange/marionette.handlebars\n' +
'\n'
},
preprocess: {
build: {
files: {
'dist/marionette.handlebars.js': 'src/marionette.handlebars.js'
}
},
interactive: {
files: {
'web-mocha/index.html': 'web-mocha/_index.html'
}
}
},
concat: {
options: {
banner: "<%= meta.banner %>",
process: function( src, filepath ) {
var bowerVersion = grunt.file.readJSON( "bower.json" ).version,
npmVersion = grunt.file.readJSON( "package.json" ).version;
if ( npmVersion === undefined || npmVersion === "" ) grunt.fail.fatal( "Version number not specified in package.json. Specify it in bower.json and package.json" );
if ( npmVersion !== bowerVersion ) grunt.fail.fatal( "Version numbers in package.json and bower.json are not identical. Make them match." + " " + npmVersion );
if ( ! /^\d+\.\d+.\d+$/.test( npmVersion ) ) grunt.fail.fatal( 'Version numbers in package.json and bower.json are not semantic. Provide a version number in the format n.n.n, e.g "1.2.3"' );
return src.replace( "__COMPONENT_VERSION_PLACEHOLDER__", npmVersion );
}
},
build: {
src: 'dist/marionette.handlebars.js',
dest: 'dist/marionette.handlebars.js'
}
},
uglify: {
options: {
banner: "<%= meta.banner %>",
mangle: {
except: ['jQuery', 'Zepto', 'Backbone', '_']
},
sourceMap: true
},
core: {
src: 'dist/marionette.handlebars.js',
dest: 'dist/marionette.handlebars.min.js'
}
},
karma: {
options: {
configFile: 'karma.conf.js',
browsers: ['PhantomJS'],
port: KARMA_PORT
},
test: {
reporters: ['progress'],
singleRun: true
},
"test-legacy": {
configFile: 'karma.legacy.conf.js',
reporters: ['progress'],
singleRun: true
},
build: {
reporters: ['progress'],
singleRun: true
}
},
jshint: {
components: {
// Workaround for merging .jshintrc with Gruntfile options, see http://goo.gl/Of8QoR
options: grunt.util._.merge({
globals: {
// Add vars which are shared between various sub-components
// (before concatenation makes them local)
}
}, grunt.file.readJSON('.jshintrc')),
files: {
src: ['src/**/*.js']
}
},
concatenated: {
options: grunt.util._.merge({
// Suppressing 'W034: Unnecessary directive "use strict"'.
// Redundant nested "use strict" is ok in concatenated file,
// no adverse effects.
'-W034': true
}, grunt.file.readJSON('.jshintrc')),
files: {
src: 'dist/**/marionette.handlebars.js'
}
}
},
'sails-linker': {
options: {
startTag: '<!--SCRIPTS-->',
endTag: '<!--SCRIPTS END-->',
fileTmpl: '<script src="../%s"></script>',
// relative doesn't seem to have any effect, ever
relative: true,
// appRoot is a misnomer for "strip out this prefix from the file path before inserting",
// should be stripPrefix
appRoot: ''
},
interactive_spec: {
options: {
startTag: '<!--SPEC SCRIPTS-->',
endTag: '<!--SPEC SCRIPTS END-->'
},
files: {
// the target file is changed in place; for generating copies, run preprocess first
'web-mocha/index.html': ['spec/**/*.+(spec|test|tests).js']
}
},
interactive_sinon: {
options: {
startTag: '<!--SINON COMPONENT SCRIPTS-->',
endTag: '<!--SINON COMPONENT SCRIPTS END-->'
},
files: {
// the target file is changed in place; for generating copies, run preprocess first
//
// The util/core.js file must be loaded first, and typeof.js must be loaded before match.js.
//
// mock.js must be loaded last (specifically, after spy.js). For the pattern achieving it, see
// http://gruntjs.com/configuring-tasks#globbing-patterns
'web-mocha/index.html': [
SINON_SOURCE_DIR + 'util/core.js',
SINON_SOURCE_DIR + 'typeof.js',
SINON_SOURCE_DIR + '**/*.js',
'!' + SINON_SOURCE_DIR + 'mock.js',
SINON_SOURCE_DIR + 'mock.js'
]
}
}
},
requirejs : {
unifiedBuild : {
options : getRequirejsBuildProfile( 'demo/amd/rjs/config/unified/build-config.js', false )
},
splitBuildVendor : {
options : getRequirejsBuildProfile( 'demo/amd/rjs/config/jsbin-parts/vendor-config.js', false )
},
splitBuildApp : {
options : getRequirejsBuildProfile( 'demo/amd/rjs/config/jsbin-parts/app-config.js', false )
}
},
// Use focus to run Grunt watch with a hand-picked set of simultaneous watch targets.
focus: {
demo: {
include: ['livereloadDemo']
},
demoCi: {
include: ['build', 'livereloadDemo']
},
demoCiDirty: {
include: ['buildDirty', 'livereloadDemo']
}
},
// Use watch to monitor files for changes, and to kick off a task then.
watch: {
options: {
nospawn: false
},
// Live-reloads the web page when the source files or the spec files change. Meant for test pages.
livereloadTest: {
options: {
livereload: LIVERELOAD_PORT
},
files: WATCHED_FILES_SRC.concat( WATCHED_FILES_SPEC )
},
// Live-reloads the web page when the dist files or the demo files change. Meant for demo pages.
livereloadDemo: {
options: {
livereload: LIVERELOAD_PORT
},
files: WATCHED_FILES_DEMO.concat( WATCHED_FILES_DIST )
},
// Runs the "build" task (ie, runs linter and tests, then compiles the dist files) when the source files or the
// spec files change. Meant for continuous integration tasks ("ci", "demo-ci").
build: {
tasks: ['build'],
files: WATCHED_FILES_SRC.concat( WATCHED_FILES_SPEC )
},
// Runs the "build-dirty" task (ie, compiles the dist files without running linter and tests) when the source
// files change. Meant for "dirty" continuous integration tasks ("ci-dirty", "demo-ci-dirty").
buildDirty: {
tasks: ['build-dirty'],
files: WATCHED_FILES_SRC
}
},
// Spins up a web server.
connect: {
options: {
port: HTTP_PORT,
// For restricting access to localhost only, change the hostname from '*' to 'localhost'
hostname: '*',
open: true,
base: '.'
},
livereload: {
livereload: LIVERELOAD_PORT
},
test: {
options: {
open: 'http://localhost:<%= connect.options.port %>/web-mocha/',
livereload: LIVERELOAD_PORT
}
},
testNoReload: {
options: {
open: 'http://localhost:<%= connect.options.port %>/web-mocha/',
keepalive: true
}
},
demo: {
options: {
open: 'http://localhost:<%= connect.options.port %>/demo/',
livereload: LIVERELOAD_PORT
}
}
},
replace: {
version: {
src: ['bower.json', 'package.json'],
overwrite: true,
replacements: [{
from: /"version"\s*:\s*"((\d+\.\d+\.)(\d+))"\s*,/,
to: function (matchedWord, index, fullText, regexMatches) {
var version = grunt.option('inc') ? regexMatches[1] + (parseInt(regexMatches[2], 10) + 1) : grunt.option('to');
if (version === undefined) grunt.fail.fatal('Version number not specified. Use the --to option, e.g. --to=1.2.3, or the --inc option to increment the revision');
if (typeof version !== "string") grunt.fail.fatal('Version number is not a string. Provide a semantic version number, e.g. --to=1.2.3');
if (!/^\d+\.\d+.\d+$/.test(version)) grunt.fail.fatal('Version number is not semantic. Provide a version number in the format n.n.n, e.g. --to=1.2.3');
grunt.log.writeln('Modifying file: Changing the version number from ' + regexMatches[0] + ' to ' + version);
return '"version": "' + version + '",';
}
}]
}
},
getver: {
files: ['bower.json', 'package.json']
}
});
grunt.loadNpmTasks('grunt-preprocess');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-sails-linker');
grunt.loadNpmTasks('grunt-text-replace');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-focus');
grunt.registerTask('lint', ['jshint:components']);
grunt.registerTask('hint', ['jshint:components']); // alias
grunt.registerTask('test', ['jshint:components', 'karma:test']);
grunt.registerTask('test-legacy', ['jshint:components', 'karma:test-legacy']);
grunt.registerTask('webtest', ['preprocess:interactive', 'sails-linker:interactive_sinon', 'sails-linker:interactive_spec', 'connect:testNoReload']);
grunt.registerTask('interactive', ['preprocess:interactive', 'sails-linker:interactive_sinon', 'sails-linker:interactive_spec', 'connect:test', 'watch:livereloadTest']);
grunt.registerTask('demo', ['connect:demo', 'focus:demo']);
grunt.registerTask('build', ['jshint:components', 'karma:build', 'preprocess:build', 'concat', 'uglify', 'jshint:concatenated', 'requirejs']);
grunt.registerTask('ci', ['build', 'watch:build']);
grunt.registerTask('setver', ['replace:version']);
grunt.registerTask('getver', function () {
grunt.config.get('getver.files').forEach(function (file) {
var config = grunt.file.readJSON(file);
grunt.log.writeln('Version number in ' + file + ': ' + config.version);
});
});
// Special tasks, not mentioned in Readme documentation:
//
// - test-legacy:
// runs the unit tests with legacy Marionette (version 2.x)
// - requirejs:
// creates build files for the AMD demo with r.js
// - build-dirty:
// builds the project without running checks (no linter, no tests)
// - ci-dirty:
// builds the project without running checks (no linter, no tests) on every source change
// - demo-ci:
// Runs the demo (= "demo" task), and also rebuilds the project on every source change (= "ci" task)
// - demo-ci-dirty:
// Runs the demo (= "demo" task), and also rebuilds the project "dirty", without tests or linter, on every source
// change (= "ci-dirty" task)
grunt.registerTask('build-dirty', ['preprocess:build', 'concat', 'uglify', 'requirejs']);
grunt.registerTask('ci-dirty', ['build-dirty', 'watch:buildDirty']);
grunt.registerTask('demo-ci', ['build', 'connect:demo', 'focus:demoCi']);
grunt.registerTask('demo-ci-dirty', ['build-dirty', 'connect:demo', 'focus:demoCiDirty']);
// Make 'build' the default task.
grunt.registerTask('default', ['build']);
};