-
Notifications
You must be signed in to change notification settings - Fork 9
/
Gruntfile.js
111 lines (97 loc) · 3.12 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
module.exports = function (grunt) {
var fs = require('fs'),
path = require('path'),
isjs = /\.js$/;
// I hate that this ordering is hard to automate. JS Bin has it right, but meh, this is a small project
var sourceScripts = ['webrtc.io.js', 'min.js', 'utils.js', 'bind.js', 'xhr.js', 'track.js', 'three.js', 'main.js', 'ball.js', 'game.js', 'start.js'];
var scripts = sourceScripts.filter(function (file) {
return isjs.test(file);
}).map(function (file) {
return 'public/js/' + file;
});
var pkg = grunt.file.readJSON('package.json');
var distpaths = {
script: 'public/js/build/<%= pkg.name %>-<%= pkg.version %>.js',
map: 'public/js/build/<%= pkg.name %>.map.json', // don't version this so we overwrite
min: 'public/js/build/<%= pkg.name %>-<%= pkg.version %>.min.js'
};
var config = {
pkg: pkg,
jshint: {
dist: {
files: {
src: ['grunt.js', 'lib/**/*.js']
}
},
build: {
files: {
src: [].slice.apply(scripts)
}
}
},
concat: {
options: {
separator: ';'
},
dist: {
src: [].slice.apply(scripts),
dest: distpaths.script
}
},
uglify: {
options: {
compress: { unsafe: false },
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n',
sourceMap: distpaths.map,
sourceMapPrefix: 2,
sourceMapRoot: '/js'
},
dist: {
src: scripts,
dest: distpaths.min
// 'public/js/<%= pkg.name %>-<%= pkg.version %>.min.js' : scripts
}
},
watch: {
files: '<%= lint.files %>',
tasks: 'lint'
},
run: {}
};
grunt.registerTask("postbuild", function() {
var filename = grunt.template.process(distpaths.min, pkg),
text = fs.readFileSync( filename, "utf8" ),
map;
grunt.log.writeln('checking ' + filename + ' (' + text.length + ')');
// Modify map/min so that it points to files in the same folder;
// see https://github.com/mishoo/UglifyJS2/issues/47
if (/\.map.json$/.test(filename)) {
text = text.replace( /"public\//g, "\"/" );
fs.writeFileSync( filename, text, "utf-8" );
}
if (/\.min\.js$/.test(filename)) {
// Wrap sourceMap directive in multiline comments (#13274)
text = text.replace(/\n?(\/\/@\s*sourceMappingURL=)(.*)/,
function( _, directive, path ) {
map = "\n" + directive + path.replace( /^public\/js/, "/js" );
return "";
});
if (map) {
text = text.replace( /(^\/\*[\w\W]*?)\s*\*\/|$/,
function( _, comment ) {
return ( comment || "\n/*" ) + map + "\n*/";
});
}
fs.writeFileSync( filename, text, "utf-8" );
}
});
// Project configuration.
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
// Default task.
grunt.registerTask('build', ['concat', 'uglify', 'postbuild']);
grunt.registerTask('default', ['concat']);
};