forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
341 lines (276 loc) · 12 KB
/
build.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
var fs = require('fs'),
exec = require('child_process').exec;
var buildArgs = process.argv.slice(2),
buildArgsAsObject = { },
rootPath = process.cwd(),
distributionPath = 'dist/';
buildArgs.forEach(function(arg) {
var key = arg.split('=')[0],
value = arg.split('=')[1];
buildArgsAsObject[key] = value;
});
var modulesToInclude = buildArgsAsObject.modules ? buildArgsAsObject.modules.split(',') : [ ];
var modulesToExclude = buildArgsAsObject.exclude ? buildArgsAsObject.exclude.split(',') : [ ];
var minifier = buildArgsAsObject.minifier || 'uglifyjs';
var mininfierCmd;
var noStrict = 'no-strict' in buildArgsAsObject;
var noSVGExport = 'no-svg-export' in buildArgsAsObject;
var noES5Compat = 'no-es5-compat' in buildArgsAsObject;
var requirejs = 'requirejs' in buildArgsAsObject ? 'requirejs' : false;
var sourceMap = 'sourcemap' in buildArgsAsObject;
// set amdLib var to encourage later support of other AMD systems
var amdLib = requirejs;
// if we want requirejs AMD support, use uglify
var amdUglifyFlags = '';
if (amdLib === 'requirejs' && minifier !== 'uglifyjs') {
console.log('[notice]: require.js support requires uglifyjs as minifier; changed minifier to uglifyjs.');
minifier = 'uglifyjs';
amdUglifyFlags = " -r 'require,exports,window,fabric' -e window:window,undefined ";
}
// if we want sourceMap support, uglify or google closure compiler are supported
var sourceMapFlags = '';
if (sourceMap) {
if (minifier !== 'uglifyjs' && minifier !== 'closure') {
console.log('[notice]: sourceMap support requires uglifyjs or google closure compiler as minifier; changed minifier to uglifyjs.');
minifier = 'uglifyjs';
}
sourceMapFlags = minifier === 'uglifyjs' ? ' --source-map all.min.js.map' : ' --create_source_map all.min.js.map --source_map_format=V3';
}
if (minifier === 'yui') {
mininfierCmd = 'java -jar ' + rootPath + '/lib/yuicompressor-2.4.6.jar all.js -o all.min.js';
}
else if (minifier === 'closure') {
mininfierCmd = 'java -jar ' + rootPath + '/lib/google_closure_compiler.jar --js all.js --js_output_file all.min.js' + sourceMapFlags;
}
else if (minifier === 'uglifyjs') {
mininfierCmd = 'uglifyjs ' + amdUglifyFlags + ' --output all.min.js all.js' + sourceMapFlags;
}
var buildSh = 'build-sh' in buildArgsAsObject;
var buildMinified = 'build-minified' in buildArgsAsObject;
var includeAllModules = (modulesToInclude.length === 1 && modulesToInclude[0] === 'ALL') || buildMinified;
var noSVGImport = (modulesToInclude.indexOf('parser') === -1 && !includeAllModules) || modulesToExclude.indexOf('parser') > -1;
var distFileContents =
'/* build: `node build.js modules=' +
modulesToInclude.join(',') +
(modulesToExclude.length ? (' exclude=' + modulesToExclude.join(',')) : '') +
(noStrict ? ' no-strict' : '') +
(noSVGExport ? ' no-svg-export' : '') +
(noES5Compat ? ' no-es5-compat' : '') +
(requirejs ? ' requirejs' : '') +
(sourceMap ? ' sourcemap' : '') +
' minifier=' + minifier +
'` */';
function appendFileContents(fileNames, callback) {
(function readNextFile() {
if (fileNames.length <= 0) {
return callback();
}
var fileName = fileNames.shift();
if (!fileName) {
return readNextFile();
}
fs.readFile(__dirname + '/' + fileName, function (err, data) {
if (err) throw err;
var strData = String(data);
if (noStrict) {
strData = strData.replace(/"use strict";?\n?/, '');
}
if (noSVGExport) {
strData = strData.replace(/\/\* _TO_SVG_START_ \*\/[\s\S]*?\/\* _TO_SVG_END_ \*\//g, '');
}
if (noES5Compat) {
strData = strData.replace(/\/\* _ES5_COMPAT_START_ \*\/[\s\S]*?\/\* _ES5_COMPAT_END_ \*\//g, '');
}
if (noSVGImport) {
strData = strData.replace(/\/\* _FROM_SVG_START_ \*\/[\s\S]*?\/\* _FROM_SVG_END_ \*\//g, '');
}
distFileContents += ('\n' + strData + '\n');
readNextFile();
});
})();
}
function ifSpecifiedInclude(moduleName, fileName) {
var isInIncludedList = modulesToInclude.indexOf(moduleName) > -1;
var isInExcludedList = modulesToExclude.indexOf(moduleName) > -1;
// excluded list takes precedence over modules=ALL
return ((isInIncludedList || includeAllModules) && !isInExcludedList) ? fileName : '';
}
function ifSpecifiedDependencyInclude(included, excluded, fileName) {
return (
(
(modulesToInclude.indexOf(included) > -1 || includeAllModules) &&
(modulesToExclude.indexOf(excluded) == -1))
? fileName
: ''
);
}
function ifSpecifiedAMDInclude(amdLib) {
var supportedLibraries = ['requirejs'];
if (supportedLibraries.indexOf(amdLib) > -1) {
return 'src/amd/' + amdLib + '.js';
}
return '';
}
var filesToInclude = [
'HEADER.js',
ifSpecifiedDependencyInclude('text', 'cufon', 'lib/cufon.js'),
ifSpecifiedDependencyInclude('serialization', 'json', 'lib/json2.js'),
ifSpecifiedInclude('gestures', 'lib/event.js'),
'src/mixins/observable.mixin.js',
'src/mixins/collection.mixin.js',
'src/util/misc.js',
'src/util/arc.js',
'src/util/lang_array.js',
'src/util/lang_object.js',
'src/util/lang_string.js',
'src/util/lang_function.js',
'src/util/lang_class.js',
'src/util/dom_event.js',
'src/util/dom_style.js',
'src/util/dom_misc.js',
'src/util/dom_request.js',
'src/log.js',
ifSpecifiedInclude('animation', 'src/util/animate.js'),
//'src/util/animate.js',
ifSpecifiedInclude('easing', 'src/util/anim_ease.js'),
ifSpecifiedInclude('parser', 'src/parser.js'),
ifSpecifiedInclude('parser', 'src/elements_parser.js'),
'src/point.class.js',
'src/intersection.class.js',
'src/color.class.js',
ifSpecifiedInclude('gradient', 'src/gradient.class.js'),
ifSpecifiedInclude('pattern', 'src/pattern.class.js'),
ifSpecifiedInclude('shadow', 'src/shadow.class.js'),
'src/static_canvas.class.js',
ifSpecifiedInclude('freedrawing', 'src/brushes/base_brush.class.js'),
ifSpecifiedInclude('freedrawing', 'src/brushes/pencil_brush.class.js'),
ifSpecifiedInclude('freedrawing', 'src/brushes/circle_brush.class.js'),
ifSpecifiedInclude('freedrawing', 'src/brushes/spray_brush.class.js'),
ifSpecifiedInclude('freedrawing', 'src/brushes/pattern_brush.class.js'),
ifSpecifiedInclude('interaction', 'src/canvas.class.js'),
ifSpecifiedInclude('interaction', 'src/mixins/canvas_events.mixin.js'),
ifSpecifiedInclude('interaction', 'src/mixins/canvas_grouping.mixin.js'),
'src/mixins/canvas_dataurl_exporter.mixin.js',
ifSpecifiedInclude('serialization', 'src/mixins/canvas_serialization.mixin.js'),
ifSpecifiedInclude('gestures', 'src/mixins/canvas_gestures.mixin.js'),
'src/shapes/object.class.js',
'src/mixins/object_origin.mixin.js',
'src/mixins/object_geometry.mixin.js',
'src/mixins/object_stacking.mixin.js',
'src/mixins/object.svg_export.js',
'src/mixins/stateful.mixin.js',
ifSpecifiedInclude('interaction', 'src/mixins/object_interactivity.mixin.js'),
ifSpecifiedInclude('animation', 'src/mixins/animation.mixin.js'),
//'src/mixins/animation.mixin.js',
'src/shapes/line.class.js',
'src/shapes/circle.class.js',
'src/shapes/triangle.class.js',
'src/shapes/ellipse.class.js',
'src/shapes/rect.class.js',
'src/shapes/polyline.class.js',
'src/shapes/polygon.class.js',
'src/shapes/path.class.js',
'src/shapes/path_group.class.js',
'src/shapes/group.class.js',
'src/shapes/image.class.js',
ifSpecifiedInclude('object_straightening', 'src/mixins/object_straightening.mixin.js'),
ifSpecifiedInclude('image_filters', 'src/filters/base_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/brightness_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/convolute_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/gradienttransparency_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/grayscale_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/invert_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/mask_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/noise_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/pixelate_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/removewhite_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/sepia_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/sepia2_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/tint_filter.class.js'),
ifSpecifiedInclude('text', 'src/shapes/text.class.js'),
ifSpecifiedInclude('cufon', 'src/shapes/text.cufon.js'),
ifSpecifiedInclude('itext', 'src/shapes/itext.class.js'),
ifSpecifiedInclude('itext', 'src/mixins/itext_behavior.mixin.js'),
ifSpecifiedInclude('itext', 'src/mixins/itext_click_behavior.mixin.js'),
ifSpecifiedInclude('itext', 'src/mixins/itext_key_behavior.mixin.js'),
ifSpecifiedInclude('itext', 'src/mixins/itext.svg_export.js'),
ifSpecifiedInclude('node', 'src/node.js'),
ifSpecifiedAMDInclude(amdLib)
];
if (buildMinified) {
for (var i = 0; i < filesToInclude.length; i++) {
if (!filesToInclude[i]) continue;
var fileNameWithoutSlashes = filesToInclude[i].replace(/\//g, '^');
exec('uglifyjs -nc ' + amdUglifyFlags + filesToInclude[i] + ' > tmp/' + fileNameWithoutSlashes);
}
}
else if (buildSh) {
var filesStr = filesToInclude.join(' ');
var isBasicBuild = modulesToInclude.length === 0;
var minFilesStr = filesToInclude
.filter(function(f) { return f !== '' })
.map(function(fileName) {
return 'tmp/' + fileName.replace(/\//g, '^');
})
.join(' ');
var fileName = isBasicBuild ? 'fabric' : modulesToInclude.join(',');
var escapedHeader = distFileContents.replace(/`/g, '\\`');
var path = '../fabricjs.com/build/files/' + fileName + '.js';
fs.appendFile('build.sh',
'echo "' + escapedHeader + '" > ' + path + ' && cat ' +
filesStr + ' >> ' + path + '\n');
path = '../fabricjs.com/build/files/' + fileName + '.min.js';
fs.appendFile('build.sh',
'echo "' + escapedHeader + '" > ' + path + ' && cat ' +
minFilesStr + ' >> ' + path + '\n')
}
else {
// Change the current working directory
process.chdir(distributionPath);
appendFileContents(filesToInclude, function() {
fs.writeFile('all.js', distFileContents, function (err) {
if (err) {
console.log(err);
throw err;
}
// add js wrapping in AMD closure for requirejs if necessary
if (amdLib !== false) {
exec('uglifyjs all.js ' + amdUglifyFlags + ' -b --output all.js');
}
if (amdLib !== false) {
console.log('Built distribution to ' + distributionPath + 'all.js (' + amdLib + '-compatible)');
} else {
console.log('Built distribution to ' + distributionPath + 'all.js');
}
exec(mininfierCmd, function (error, output) {
if (error) {
console.error('Minification failed using', minifier, 'with', mininfierCmd);
process.exit(1);
}
console.log('Minified using', minifier, 'to ' + distributionPath + 'all.min.js');
if (sourceMapFlags) {
console.log('Built sourceMap to ' + distributionPath + 'all.min.js.map');
}
exec('gzip -c all.min.js > all.min.js.gz', function (error, output) {
console.log('Gzipped to ' + distributionPath + 'all.min.js.gz');
});
});
// Always build requirejs AMD module in all.require.js
// add necessary requirejs footer code to filesToInclude if we haven't before
if (amdLib === false) {
amdLib = "requirejs";
filesToInclude[filesToInclude.length] = ifSpecifiedAMDInclude(amdLib);
}
appendFileContents(filesToInclude, function() {
fs.writeFile('all.require.js', distFileContents, function (err) {
if (err) {
console.log(err);
throw err;
}
exec('uglifyjs all.require.js ' + amdUglifyFlags + ' -b --output all.require.js');
console.log('Built distribution to ' + distributionPath + 'all.require.js (requirejs-compatible)');
});
});
});
});
}