This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Gruntfile.js
400 lines (390 loc) · 11.5 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
392
393
394
395
396
397
398
399
400
/*global module:false*/
/*global require:false*/
/*global process:false*/
module.exports = function(grunt) {
// Require normal dependencies.
var dependencies = ['dependencies', 'devDependencies'];
// Add in optionalDependencies if no --no-optional flag is present.
var optionalDependencies = !grunt.option('no-optional');
if (optionalDependencies) {
dependencies.push('optionalDependencies');
}
// Load all grunt tasks matching the `grunt-*` pattern.
require('load-grunt-tasks')(grunt, { scope: dependencies });
// Initialize grunt configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
banner: '/**\n' +
' * <%= pkg.title || pkg.name %> <%= pkg.version %>\n' +
'<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' +
' * <%= pkg.description %>\n' +
' * Licensed under <%= _.pluck(pkg.licenses, "type").join(", ") %>\n *\n' +
' * Maintainers:' +
'<% _.forEach(pkg.maintainers, function(maintainer) {%>\n * <%= maintainer.name %> - <%= maintainer.url %><% }); %>\n *\n' +
' * Last build: <%= grunt.template.today("yyyy-mm-dd h:MM:ss TT Z") %>\n' +
' */\n',
// Task configuration.
clean: {
files: [
'build/*',
'release/*'
]
},
less: {
options: {
cleancss: true
},
files: {
src: 'src/less/**/*.less',
dest: 'build/<%= pkg.name %>.css'
}
},
css2js: {
convert: {
src: 'build/<%= pkg.name %>.css',
dest: 'build/<%= pkg.name %>.css.js'
}
},
concat: {
options: {
banner: '<%= banner %>' + grunt.file.read('src/js/_banner.header.js'),
footer: grunt.file.read('src/js/_banner.footer.js'),
stripBanners: true
},
build: {
src: [
'src/js/**/*.js',
'!src/js/_banner.header.js',
'!src/js/_banner.footer.js',
'!src/js/init.js',
'build/<%= pkg.name %>.css.js',
'src/js/init.js'
],
dest: 'build/<%= pkg.name %>.js'
}
},
jshint: {
options: {
reporter: optionalDependencies ? require('jshint-stylish') : undefined
},
package: {
options: {
jshintrc: '.jshintrc'
},
src: 'package.json'
},
gruntfile: {
options: {
jshintrc: '.jshintrc'
},
src: 'Gruntfile.js'
},
js: {
options: {
jshintrc: '.jshintrc'
},
src: 'src/js/**/*.js'
}
},
sed: {
name: {
path: 'build/',
pattern: '%PKG.NAME%',
replacement: '<%= pkg.name %>',
recursive: true
},
title: {
path: 'build/',
pattern: '%PKG.TITLE%',
replacement: '<%= pkg.title || pkg.name %>',
recursive: true
},
description: {
path: 'build/',
pattern: '%PKG.DESCRIPTION%',
replacement: '<%= pkg.description %>',
recursive: true
},
homepage: {
path: 'build/',
pattern: '%PKG.HOMEPAGE%',
replacement: '<%= pkg.homepage || "" %>',
recursive: true
},
author: {
path: 'build/',
pattern: '%PKG.AUTHOR%',
replacement: '<%= pkg.author.name %>',
recursive: true
},
icon: {
path: 'build/',
pattern: '%PKG.ICON%',
replacement: '<%= pkg.icon || "icon.png" %>',
recursive: true
},
license: {
path: 'build/',
pattern: '%PKG.LICENSE%',
replacement: '<%= _.pluck(pkg.licenses, "type").join(", ") %>',
recursive: true
},
version: {
path: 'build/',
pattern: '%PKG.VERSION%',
replacement: '<%= pkg.version %>',
recursive: true
}
},
uglify: {
options: {
banner: '<%= banner %>',
beautify: true
},
default: {
src: '<%= concat.build.dest %>',
dest: 'build/<%= pkg.name %>.js'
}
},
copy: {
chrome: {
files: [
{
expand: true,
cwd: 'templates/chrome/',
src: ['**'],
dest: 'build/chrome/'
},
{
expand: true,
cwd: 'src/',
src: ['icon.png'],
dest: 'build/chrome/'
},
{
expand: true,
cwd: 'build/',
src: ['<%= pkg.name %>.js'],
dest: 'build/chrome/'
}
]
},
firefox: {
files: [
{
expand: true,
cwd: 'templates/firefox/',
src: ['**'],
dest: 'build/firefox/'
},
{
expand: true,
cwd: 'src/',
src: ['icon.png'],
dest: 'build/firefox/'
},
{
expand: true,
cwd: 'build/',
src: ['<%= pkg.name %>.js'],
dest: 'build/firefox/data/'
}
]
},
safari: {
files: [
{
expand: true,
cwd: 'templates/safari/',
src: ['Info.plist', 'Settings.plist'],
dest: 'build/<%= pkg.name %>.safariextension/'
},
{
expand: true,
cwd: 'templates/safari/',
src: ['update.plist'],
dest: 'build/'
},
{
expand: true,
cwd: 'src/',
src: ['icon.png'],
dest: 'build/<%= pkg.name %>.safariextension/'
},
{
expand: true,
cwd: 'build/',
src: ['<%= pkg.name %>.js'],
dest: 'build/<%= pkg.name %>.safariextension/'
}
]
}
},
watch: {
files: [
// Force-exclude artifacts.
// Despite not being included in the list of files, the watch task can
// be intermittently interrupted by a build:* task with:
// >> File "release" added.
// which may even cause an infinite loop. Seemingly a bug in watch;
// possibly limited to Windows/NTFS/msys. Exclusions must be defined
// first; all arguments are processed/merged sequentially.
'!build',
'!build/**',
'!release',
'!release/**',
'<%= jshint.package.src %>',
'<%= jshint.gruntfile.src %>',
'<%= jshint.js.src %>',
'<%= less.files.src %>',
'<%= qunit.all %>'
],
tasks: ['default'],
options: {
interrupt: true
}
},
release: {
options: {
add: false,
commit: false,
tag: false,
push: false,
pushTags: false,
npm: false
}
},
compress: {
chrome: {
options: {
archive: 'release/chrome/<%= pkg.name %>.zip',
mode: 'zip'
},
expand: true,
cwd: 'build/chrome/',
src: ['**/*'],
dest: '/'
}
},
"mozilla-addon-sdk": {
'release': {
options: {
revision: "1.16"
}
}
},
"mozilla-cfx-xpi": {
'release': {
options: {
"mozilla-addon-sdk": "release",
extension_dir: "build/firefox",
dist_dir: "release/firefox",
// --output-file is an experimental option, not guaranteed to exist.
// @see https://developer.mozilla.org/en-US/Add-ons/SDK/Tools/cfx#Experimental_Options_3
arguments: "--output-file=<%= pkg.name %>.xpi"
}
}
},
'qunit' : {
all: "tests/**/*.html"
},
exec: {
build_safari_ext: {
stdout: false,
stderr: false,
cmd: function () {
var args = [
'build-safari-ext',
this.template.process('<%= pkg.name %>'),
this.template.process(process.cwd() + '/build/<%= pkg.name %>.safariextension'),
process.cwd() + '/release/safari'
];
return args.join(' ');
},
callback: function (error) {
if (error) {
grunt.log.warn('Unable to create ' + String(grunt.template.process('release/safari/<%= pkg.name %>.safariextension')).red);
}
else {
grunt.log.writeln('Created ' + String(grunt.template.process('release/safari/<%= pkg.name %>.safariextension')).cyan);
}
}
}
}
});
// Install tasks.
grunt.registerTask('install', 'Installs dependencies.',
['mozilla-addon-sdk']);
// Default tasks.
grunt.registerTask('default', 'Compiles code.',
['clean', 'less', 'css2js', 'jshint', 'concat', 'copy', 'sed']);
// Realtime development tasks.
// Enjoy: `grunt watch:ff`
// These tasks are highly tailored subsets of the default task having the goal
// of *instant* reloading of a newly built extension into a particular browser.
// The performance target is ~500ms; i.e., the time it takes a human to switch
// from the code editor to the browser.
// Note that grunt watch is not a multi-task; it supports multiple targets,
// but it does not support multiple tasks/sets; when running `grunt watch`,
// all targets are watched, and all tasks of all matching targets are executed
// upon a change. We do not want to tamper with the default `grunt watch` task,
// nor do we want to build all extensions at once (for performance reasons).
// The recommended informal workaround is to dynamically swap out the default
// config of the watch task ad-hoc.
// @see https://github.com/gruntjs/grunt-contrib-watch/issues/71#issuecomment-26152333
// Firefox.
grunt.registerTask('dev:ff', 'Compiles code to build a Firefox extension. (see watch:ff)',
['less', 'css2js', 'jshint:js', 'concat', 'copy:firefox', 'sed']);
grunt.registerTask('watch:ff', 'Enables real-time development for Firefox.', function () {
var config = grunt.config('watch');
config.tasks = ['dev:ff', 'build:firefox', 'autoload:ff'];
// Auto-run once upon invocation.
config.options.atBegin = true;
config.options.spawn = false;
grunt.config('watch', config);
grunt.task.run('watch');
});
// Test tasks.
grunt.registerTask('test', 'Runs tests.',
['qunit']);
grunt.registerTask('travis-ci', 'Compiles code and runs tests.',
['default', 'test']);
// Build tasks.
grunt.registerTask('build', 'Compiles code and builds all extensions.',
['default', 'uglify', 'build:chrome', 'build:firefox', 'build:safari']);
grunt.registerTask('build:chrome', 'Builds the Chrome extension.',
['compress:chrome']);
grunt.registerTask('build:firefox', 'Builds the Firefox extension.',
['mozilla-cfx-xpi']);
grunt.registerTask('build:safari', 'Builds the Safari extension.',
['exec:build_safari_ext']);
// Autoload tasks.
// Firefox.
// @see https://addons.mozilla.org/en-US/firefox/addon/autoinstaller/
grunt.registerTask('autoload:ff', 'Loads the XPI extension into Firefox.', function () {
var done = this.async();
var xpi = 'release/firefox/' + grunt.template.process('<%= pkg.name %>.xpi');
grunt.util.spawn({
cmd: 'wget',
args: [
'--post-file=' + xpi,
'http://localhost:8888'
],
opts: !grunt.option('debug') ? {} : {
stdio: 'inherit'
}
},
function (error, result, code) {
if (code !== 8) {
return grunt.warn('Auto-loading ' + xpi + ' failed:\n\n' +
code + ': ' + error + '\n\n' +
'Ensure you have the AutoInstaller extension installed in Firefox:\n' +
'https://addons.mozilla.org/en-US/firefox/addon/autoinstaller/\n\n'
);
}
grunt.log.ok('Auto-loaded ' + xpi + ' into Firefox.');
done();
});
});
};