forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
309 lines (252 loc) · 11.6 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
var fs = require('fs'),
exec = require('child_process').exec;
var buildArgs = process.argv.slice(2),
buildArgsAsObject = { },
rootPath = process.cwd();
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 distributionPath = buildArgsAsObject.dest || 'dist/';
var minifier = buildArgsAsObject.minifier || 'uglifyjs';
var mininfierCmd;
var noStrict = 'no-strict' in buildArgsAsObject;
var noSVGExport = 'no-svg-export' 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 fabric.min.js.map' : ' --create_source_map fabric.min.js.map --source_map_format=V3';
}
if (minifier === 'yui') {
mininfierCmd = 'java -jar ' + rootPath + '/lib/yuicompressor-2.4.6.jar fabric.js -o fabric.min.js';
}
else if (minifier === 'closure') {
mininfierCmd = 'java -jar ' + rootPath + '/lib/google_closure_compiler.jar --js fabric.js --js_output_file fabric.min.js' + sourceMapFlags;
}
else if (minifier === 'uglifyjs') {
mininfierCmd = 'uglifyjs ' + amdUglifyFlags + ' --compress --mangle --output fabric.min.js fabric.js' + sourceMapFlags;
}
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' : '') +
(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 (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 ifSpecifiedAMDInclude(amdLib) {
var supportedLibraries = ['requirejs'];
if (supportedLibraries.indexOf(amdLib) > -1) {
return 'src/amd/' + amdLib + '.js';
}
return '';
}
var filesToInclude = [
'HEADER.js',
ifSpecifiedInclude('global', 'src/globalFabric.js'),
ifSpecifiedInclude('gestures', 'lib/event.js'),
'src/mixins/observable.mixin.js',
'src/mixins/collection.mixin.js',
'src/mixins/shared_methods.mixin.js',
'src/util/misc.js',
ifSpecifiedInclude('accessors', 'src/util/named_accessors.mixin.js'),
'src/util/arc.js',
'src/util/lang_array.js',
'src/util/lang_object.js',
'src/util/lang_string.js',
'src/util/lang_class.js',
ifSpecifiedInclude('interaction', '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'),
ifSpecifiedInclude('animation', 'src/util/animate_color.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/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/group.class.js',
ifSpecifiedInclude('interaction', 'src/shapes/active_selection.class.js'),
'src/shapes/image.class.js',
ifSpecifiedInclude('object_straightening', 'src/mixins/object_straightening.mixin.js'),
ifSpecifiedInclude('image_filters', 'src/filters/webgl_backend.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/2d_backend.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/base_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/colormatrix_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/grayscale_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/invert_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/removecolor_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/filter_generator.js'),
ifSpecifiedInclude('image_filters', 'src/filters/blendcolor_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/blendimage_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/resize_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/contrast_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/saturate_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/blur_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/gamma_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/composed_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/hue_rotation.class.js'),
ifSpecifiedInclude('text', 'src/shapes/text.class.js'),
ifSpecifiedInclude('text', 'src/mixins/text_style.mixin.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('textbox', 'src/shapes/textbox.class.js'),
ifSpecifiedInclude('textbox', 'src/mixins/textbox_behavior.mixin.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 {
// change the current working directory
process.chdir(distributionPath);
appendFileContents(filesToInclude, function() {
fs.writeFile('fabric.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 fabric.js ' + amdUglifyFlags + ' -b --output fabric.js');
}
if (amdLib !== false) {
console.log('Built distribution to ' + distributionPath + 'fabric.js (' + amdLib + '-compatible)');
} else {
console.log('Built distribution to ' + distributionPath + 'fabric.js');
}
exec(mininfierCmd, function (error, output) {
if (error) {
console.error('Minification failed using', minifier, 'with', mininfierCmd);
console.error('Minifier error output:\n' + error);
process.exit(1);
}
console.log('Minified using', minifier, 'to ' + distributionPath + 'fabric.min.js');
if (sourceMapFlags) {
console.log('Built sourceMap to ' + distributionPath + 'fabric.min.js.map');
}
exec('gzip -c fabric.min.js > fabric.min.js.gz', function (error, output) {
console.log('Gzipped to ' + distributionPath + 'fabric.min.js.gz');
});
});
// Always build requirejs AMD module in fabric.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('fabric.require.js', distFileContents, function (err) {
if (err) {
console.log(err);
throw err;
}
exec('uglifyjs fabric.require.js ' + amdUglifyFlags + ' -b --output fabric.require.js');
console.log('Built distribution to ' + distributionPath + 'fabric.require.js (requirejs-compatible)');
});
});
});
});
}