forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
174 lines (160 loc) · 6.28 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
/*global module:false*/
module.exports = function(grunt) {
var _ = grunt.util._;
// Project configuration
grunt.initConfig({
// Metadata
pkg: grunt.file.readJSON('package.json'),
fragments: './build/fragments/',
banner: '/*!\n' +
' * Knockout JavaScript library v<%= pkg.version %>\n' +
' * (c) The Knockout.js team - <%= pkg.homepage %>\n' +
' * License: <%= pkg.licenses[0].type %> (<%= pkg.licenses[0].url %>)\n' +
' */\n\n',
checktrailingspaces: {
main: {
src: [
"**/*.{js,html,css,bat,ps1,sh}",
"!build/output/**",
"!node_modules/**"
],
filter: 'isFile'
}
},
build: {
debug: './build/output/knockout-latest.debug.js',
min: './build/output/knockout-latest.js'
},
dist: {
debug: './dist/knockout.debug.js',
min: './dist/knockout.js'
},
test: {
phantomjs: 'spec/runner.phantom.js',
node: 'spec/runner.node.js'
}
});
grunt.registerTask('clean', 'Clean up output files.', function (target) {
var output = grunt.config('build');
var files = [ output.debug, output.min ];
var options = { force: (target == 'force') };
_.forEach(files, function (file) {
if (grunt.file.exists(file))
grunt.file.delete(file, options);
});
return !this.errorCount;
});
var trailingSpaceRegex = /[ ]$/;
grunt.registerMultiTask('checktrailingspaces', 'checktrailingspaces', function() {
var matches = [];
this.files[0].src.forEach(function(filepath) {
var content = grunt.file.read(filepath),
lines = content.split(/\r*\n/);
lines.forEach(function(line, index) {
if (trailingSpaceRegex.test(line)) {
matches.push([filepath, (index+1), line].join(':'));
}
});
});
if (matches.length) {
grunt.log.error("The following files have trailing spaces that need to be cleaned up:");
grunt.log.writeln(matches.join('\n'));
return false;
}
});
function getReferencedSources(sourceReferencesFilename) {
// Returns the array of filenames referenced by a file like source-references.js
var result;
global.knockoutDebugCallback = function(sources) { result = sources; };
eval(grunt.file.read(sourceReferencesFilename));
return result;
}
function getCombinedSources() {
var fragments = grunt.config('fragments'),
sourceFilenames = [
fragments + 'extern-pre.js',
fragments + 'amd-pre.js',
getReferencedSources(fragments + 'source-references.js'),
fragments + 'amd-post.js',
fragments + 'extern-post.js'
],
flattenedSourceFilenames = Array.prototype.concat.apply([], sourceFilenames),
combinedSources = flattenedSourceFilenames.map(function(filename) {
return grunt.file.read('./' + filename);
}).join('');
return combinedSources.replace('##VERSION##', grunt.config('pkg.version'));
}
function buildDebug(output) {
var source = [];
source.push(grunt.config('banner'));
source.push('(function(){\n');
source.push('var DEBUG=true;\n');
source.push(getCombinedSources());
source.push('})();\n');
grunt.file.write(output, source.join('').replace(/\r\n/g, '\n'));
}
function buildMin(output, done) {
var cc = require('closure-compiler');
var options = {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
output_wrapper: '(function() {%output%})();'
};
grunt.log.write('Compiling...');
cc.compile('/**@const*/var DEBUG=false;' + getCombinedSources(), options, function (err, stdout, stderr) {
if (err) {
grunt.log.error(err);
done(false);
} else {
grunt.log.ok();
grunt.file.write(output, (grunt.config('banner') + stdout).replace(/\r\n/g, '\n'));
done(true);
}
});
}
grunt.registerMultiTask('build', 'Build', function() {
if (!this.errorCount) {
var output = this.data;
if (this.target === 'debug') {
buildDebug(output);
} else if (this.target === 'min') {
buildMin(output, this.async());
}
}
return !this.errorCount;
});
grunt.registerMultiTask('test', 'Run tests', function () {
var done = this.async();
grunt.util.spawn({ cmd: this.target, args: [this.data] },
function (error, result, code) {
if (code === 127 /*not found*/) {
grunt.verbose.error(result.stderr);
// ignore this error
done(true);
} else {
grunt.log.writeln(result.stdout);
if (error)
grunt.log.error(result.stderr);
done(!error);
}
}
);
});
grunt.registerTask('dist', function() {
var version = grunt.config('pkg.version'),
buildConfig = grunt.config('build'),
distConfig = grunt.config('dist');
grunt.file.copy(buildConfig.debug, distConfig.debug);
grunt.file.copy(buildConfig.min, distConfig.min);
console.log('To publish, run:');
console.log(' git add bower.json');
console.log(' git add -f ' + distConfig.debug);
console.log(' git add -f ' + distConfig.min);
console.log(' git checkout head');
console.log(' git commit -m \'Version ' + version + ' for distribution\'');
console.log(' git tag -a v' + version + ' -m \'Add tag v' + version + '\'');
console.log(' git checkout master');
console.log(' git push origin --tags');
});
// Default task.
grunt.registerTask('default', ['clean', 'checktrailingspaces', 'build', 'test']);
};