Skip to content

Commit 8f59af2

Browse files
committed
Add infrastructure for building module combination files
1 parent 9027d1d commit 8f59af2

File tree

5 files changed

+120
-20
lines changed

5 files changed

+120
-20
lines changed

build.js

+54-19
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@ else if (minifier === 'uglifyjs') {
2727
mininfierCmd = 'uglifyjs --output dist/all.min.js dist/all.js';
2828
}
2929

30-
var includeAllModules = modulesToInclude.length === 1 && modulesToInclude[0] === 'ALL';
3130
var noStrict = 'no-strict' in buildArgsAsObject;
3231
var noSVGExport = 'no-svg-export' in buildArgsAsObject;
3332
var noES5Compat = 'no-es5-compat' in buildArgsAsObject;
3433

34+
var buildSh = 'build-sh' in buildArgsAsObject;
35+
var buildMinified = 'build-minified' in buildArgsAsObject;
36+
37+
var includeAllModules = (modulesToInclude.length === 1 && modulesToInclude[0] === 'ALL') || buildMinified;
38+
3539
var distFileContents =
3640
'/* build: `node build.js modules=' +
3741
modulesToInclude.join(',') +
3842
(modulesToExclude.length ? (' exclude=' + modulesToExclude.join(',')) : '') +
3943
(noStrict ? ' no-strict' : '') +
4044
(noSVGExport ? ' no-svg-export' : '') +
4145
(noES5Compat ? ' no-es5-compat' : '') +
42-
'` */\n';
46+
'` */';
4347

4448
function appendFileContents(fileNames, callback) {
4549

@@ -67,7 +71,7 @@ function appendFileContents(fileNames, callback) {
6771
if (noES5Compat) {
6872
strData = strData.replace(/\/\* _ES5_COMPAT_START_ \*\/[\s\S]*\/\* _ES5_COMPAT_END_ \*\//, '');
6973
}
70-
distFileContents += (strData + '\n');
74+
distFileContents += ('\n' + strData + '\n');
7175
readNextFile();
7276
});
7377

@@ -93,7 +97,6 @@ function ifSpecifiedDependencyInclude(included, excluded, fileName) {
9397
}
9498

9599
var filesToInclude = [
96-
97100
'HEADER.js',
98101

99102
ifSpecifiedDependencyInclude('text', 'cufon', 'lib/cufon.js'),
@@ -183,26 +186,58 @@ var filesToInclude = [
183186
ifSpecifiedInclude('node', 'src/node.js')
184187
];
185188

186-
appendFileContents(filesToInclude, function() {
187-
fs.writeFile('dist/all.js', distFileContents, function (err) {
188-
if (err) {
189-
console.log(err);
190-
throw err;
191-
}
189+
if (buildMinified) {
190+
for (var i = 0; i < filesToInclude.length; i++) {
191+
var fileNameWithoutSlashes = filesToInclude[i].replace(/\//g, '^');
192192

193-
console.log('Built distribution to dist/all.js');
193+
if (!filesToInclude[i]) continue;
194194

195-
exec(mininfierCmd, function (error, output) {
196-
if (!error) {
197-
console.log('Minified using', minifier, 'to dist/all.min.js');
195+
exec('uglifyjs -nc ' + filesToInclude[i] + ' > tmp/' + fileNameWithoutSlashes);
196+
}
197+
}
198+
else if (buildSh) {
199+
200+
var filesStr = filesToInclude.join(' ');
201+
var isBasicBuild = modulesToInclude.length === 0;
202+
203+
var minFilesStr = filesToInclude
204+
.filter(function(f) { return f !== '' })
205+
.map(function(fileName) {
206+
return 'tmp/' + fileName.replace(/\//g, '^');
207+
})
208+
.join(' ');
209+
210+
var fileName = isBasicBuild ? 'fabric' : modulesToInclude.join(',');
211+
212+
var escapedHeader = distFileContents.replace(/`/g, '\\`');
213+
var path = '../fabricjs.com/build/files/' + fileName + '.js';
214+
fs.appendFile('build.sh',
215+
'echo "' + escapedHeader + '" > ' + path + ' && cat ' +
216+
filesStr + ' >> ' + path + '\n');
217+
218+
path = '../fabricjs.com/build/files/' + fileName + '.min.js';
219+
fs.appendFile('build.sh',
220+
'echo "' + escapedHeader + '" > ' + path + ' && cat ' +
221+
minFilesStr + ' >> ' + path + '\n')
222+
}
223+
else {
224+
appendFileContents(filesToInclude, function() {
225+
fs.writeFile('dist/all.js', distFileContents, function (err) {
226+
if (err) {
227+
console.log(err);
228+
throw err;
198229
}
199-
exec('gzip -c dist/all.min.js > dist/all.min.js.gz', function (error, output) {
200-
console.log('Gzipped to dist/all.min.js.gz');
201230

202-
exec('ls -l dist', function (error, output) {
203-
console.log(output.replace(/^.*/, ''));
231+
console.log('Built distribution to dist/all.js');
232+
233+
exec(mininfierCmd, function (error, output) {
234+
if (!error) {
235+
console.log('Minified using', minifier, 'to dist/all.min.js');
236+
}
237+
exec('gzip -c dist/all.min.js > dist/all.min.js.gz', function (error, output) {
238+
console.log('Gzipped to dist/all.min.js.gz');
204239
});
205240
});
206241
});
207242
});
208-
});
243+
}

build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

build_all

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node build.js build-minified; # builds minified src files
2+
node create_build_script.js; # creates build.js with all module combinations (minified and not)
3+
rm -rf ../fabricjs.com/build/files/*; # remove all previous module files
4+
./build.sh; # builds actual distribution files with all module combinations (minified and not)
5+
rm -rf tmp/*; # remove tmp files
6+
echo "" > build.sh; # erase build.sh

create_build_script.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var fs = require('fs'),
2+
execSync = require('execSync').exec;
3+
4+
var modules = [
5+
'text',
6+
// 'cufon',
7+
// 'json',
8+
'gestures',
9+
'easing',
10+
'parser',
11+
'freedrawing',
12+
'interaction',
13+
'serialization',
14+
// 'stateful',
15+
// 'object_straightening',
16+
'image_filters',
17+
'node'
18+
];
19+
20+
// http://stackoverflow.com/questions/5752002/find-all-possible-subset-combos-in-an-array
21+
var combine = function(a, min) {
22+
var fn = function(n, src, got, all) {
23+
if (n == 0) {
24+
if (got.length > 0) {
25+
all[all.length] = got;
26+
}
27+
return;
28+
}
29+
for (var j = 0, len = src.length; j < len; j++) {
30+
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
31+
}
32+
return;
33+
}
34+
var all = [];
35+
for (var i = min, _len = a.length; i < _len; i++) {
36+
fn(i, a, [], all);
37+
}
38+
all.push(a);
39+
return all;
40+
}
41+
42+
var combinations = combine(modules, 1);
43+
var startTime = new Date;
44+
45+
fs.writeFile('build.sh', '#!/usr/bin/env sh\n\n', function() {
46+
47+
for (var i = 0, len = combinations.length; i < len; i++) {
48+
49+
var modulesStr = combinations[i].join(',');
50+
var command = 'node build.js build-sh modules=' + modulesStr;
51+
52+
execSync(command);
53+
}
54+
55+
// create basic (minimal) build
56+
execSync('node build.js build-sh modules=');
57+
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"devDependencies": {
2222
"qunit": "0.5.x",
2323
"jshint": "1.1.x",
24-
"uglify-js": "2.2.x"
24+
"uglify-js": "2.2.x",
25+
"execSync": "0.0.4"
2526
},
2627
"engines": { "node": ">=0.4.0 && <1.0.0" },
2728
"main": "./dist/all.js"

0 commit comments

Comments
 (0)