/
bundle.js
367 lines (301 loc) · 11.8 KB
/
bundle.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
const path = require('path');
const os = require('os');
const Convert = require('./convert-source-map');
const fs = require('../file-system');
const SourceInclusion = require('./source-inclusion').SourceInclusion;
const DependencyInclusion = require('./dependency-inclusion').DependencyInclusion;
const Configuration = require('../configuration').Configuration;
const Utils = require('./utils');
exports.Bundle = class {
constructor(bundler, config) {
this.Minimatch = require('minimatch').Minimatch; //nested dep of vinyl-fs
this.bundler = bundler;
this.config = config;
this.dependencies = [];
this.prepend = (config.prepend || []).filter(x => bundler.itemIncludedInBuild(x));
this.append = (config.append || []).filter(x => bundler.itemIncludedInBuild(x));
this.moduleId = config.name.replace(path.extname(config.name), '');
this.hash = '';
this.includes = (config.source || []);
this.excludes = [];
if (this.includes instanceof Array) {
this.includes = this.includes.map(x => new SourceInclusion(this, x));
} else {
this.excludes = (this.includes.exclude || []).map(x => this.createMatcher(x));
this.includes = this.includes.include.map(x => new SourceInclusion(this, x));
}
this.buildOptions = new Configuration(config.options, bundler.buildOptions.getAllOptions());
this.requiresBuild = true;
this.fileCache = {};
}
static create(bundler, config) {
let bundle = new exports.Bundle(bundler, config);
let dependencies = config.dependencies || [];
let dependenciesToBuild = dependencies
.filter(x => bundler.itemIncludedInBuild(x));
return Utils.runSequentially(
dependenciesToBuild,
dep => bundler.configureDependency(dep))
.then(descriptions => {
return Utils.runSequentially(
descriptions,
description => bundle.addDependency(description)
);
})
.then(() => bundle);
}
createMatcher(pattern) {
return new this.Minimatch(pattern);
}
addDependency(description) {
this.dependencies.push(description);
let inclusion = new DependencyInclusion(this, description);
this.includes.push(inclusion);
return inclusion.traceResources();
}
trySubsume(item) {
let includes = this.includes;
for (let i = 0, ii = includes.length; i < ii; ++i) {
if (includes[i].trySubsume(item)) {
this.requiresBuild = true;
return true;
}
}
return false;
}
transform() {
if (this.requiresBuild) {
let index = -1;
let items = this.includes;
function doTransform() {
index++;
if (index < items.length) {
return items[index].transform().then(doTransform);
}
return Promise.resolve();
}
return doTransform();
}
return Promise.resolve();
}
getDependencyLocations() {
return this.includes.filter(inclusion => inclusion.description && inclusion.description.location)
.map(inclusion => {
let normalizedLocation = path.posix.normalize(inclusion.description.location).replace(/\\/g, '\/');
return {
location: normalizedLocation,
inclusion: inclusion
};
});
}
getBundledModuleIds() {
return unique(this.includes.reduce((a, b) => a.concat(b.getAllModuleIds()), []));
}
getBundledFiles() {
return this.includes.reduce((a, b) => a.concat(b.getAllFiles()), []);
}
write(platform) {
if (!this.requiresBuild) {
return Promise.resolve();
}
let work = Promise.resolve();
let loaderOptions = this.bundler.loaderOptions;
let buildOptions = this.buildOptions;
let files = [];
if (this.prepend.length) {
work = work.then(() => addFilesInOrder(this, this.prepend, files));
}
if (loaderOptions.configTarget === this.config.name) {
work = work.then(() => {
files.push({ contents: '_aureliaConfigureModuleLoader();'});
});
}
work = work.then(() => files = files.concat(this.getBundledFiles()));
if (this.append.length) {
work = work.then(() => addFilesInOrder(this, this.append, files));
}
return work.then(() => {
const Concat = require('./concat-with-sourcemaps');
let concat = new Concat(true, this.config.name, os.EOL);
const generateHashedPath = require('./utils').generateHashedPath;
const generateHash = require('./utils').generateHash;
let needsSourceMap = false;
for (let i = 0; i < files.length; ++i) {
let currentFile = files[i];
let sourceMap = buildOptions.isApplicable('sourcemaps') ? currentFile.sourceMap : undefined;
function fileIsDependency(file) {
return file
&& file.path
&& file.path.indexOf('node_modules') >= 0;
}
function acquireSourceMapForDependency(file) {
if (!file || !file.path) {
return;
}
let parsedPath = path.parse(file.path);
try {
let base64SourceMap = Convert.fromSource(file.contents.toString());
if (base64SourceMap) {
return null;
}
} catch (error) {
// we don't want the build to fail when a sourcemap file can't be parsed
return null;
}
let converter;
try {
converter = Convert.fromMapFileSource(file.contents.toString(), parsedPath.dir);
} catch (e) {
console.log(e);
return null;
}
sourceMap = converter
? converter.sourcemap
: null;
if (sourceMap !== null) {
// path.posix.relative(from, to) does not work here
sourceMap.sourceRoot = parsedPath.dir.substring(process.cwd().length);
}
return sourceMap;
}
if (fileIsDependency(currentFile)) {
sourceMap = acquireSourceMapForDependency(currentFile);
}
if (sourceMap) {
needsSourceMap = true;
}
concat.add(currentFile.path, currentFile.contents, sourceMap ? JSON.stringify(sourceMap) : undefined);
}
let mapContents;
let contents = concat.content;
let bundleFileName = this.config.name;
if (loaderOptions.configTarget === this.config.name) {
//Add to the config bundle the loader config. Can't change index.html yet because we haven't generated hashes for all the files
concat.add(undefined, this.writeLoaderCode(platform));
contents = concat.content;
if (buildOptions.isApplicable('rev')) {
//Generate a unique hash based off of the bundle contents
//Must generate hash after we write the loader config so that any other bundle changes (hash changes) can cause a new hash for the vendor file
this.hash = generateHash(concat.content);
bundleFileName = generateHashedPath(this.config.name, this.hash);
}
//Again, in the config setup, we're at the last bundle, and we can modify the index.html correctly now
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
if (platform.index) {
this.setIndexFileConfigTarget(platform, path.posix.join(outputDir, bundleFileName));
}
} else if (buildOptions.isApplicable('rev')) {
//Generate a unique hash based off of the bundle contents
//Must generate hash after we write the loader config so that any other bundle changes (hash changes) can cause a new hash for the vendor file
this.hash = generateHash(concat.content);
bundleFileName = generateHashedPath(this.config.name, this.hash);
}
let mapFileName = bundleFileName + '.map';
let mapSourceRoot = path.posix.relative(
path.posix.join(process.cwd(), platform.output),
process.cwd()
);
console.log(`Writing ${bundleFileName}...`);
if (buildOptions.isApplicable('minify')) {
let minificationOptions = Object.assign({ fromString: true }, buildOptions.getValue('minify'));
if (needsSourceMap) {
minificationOptions.inSourceMap = Convert.fromJSON(concat.sourceMap).toObject();
minificationOptions.outSourceMap = mapFileName;
minificationOptions.sourceRoot = mapSourceRoot;
}
const UglifyJS = require('uglify-js'); //added to user project devDependencies
let minificationResult = UglifyJS.minify(String(contents), minificationOptions);
contents = minificationResult.code;
mapContents = needsSourceMap ? Convert.fromJSON(minificationResult.map).toJSON() : undefined;
} else if (needsSourceMap) {
mapContents = Convert.fromJSON(concat.sourceMap)
.setProperty('sourceRoot', mapSourceRoot)
.toJSON();
contents += os.EOL + '//# sourceMappingURL=' + mapFileName;
}
return fs.writeFile(path.posix.join(platform.output, bundleFileName), contents).then(() => {
this.requiresBuild = false;
if (mapContents) {
return fs.writeFile(path.posix.join(platform.output, mapFileName), mapContents)
.catch(e => {
console.log(`Unable to write the sourcemap to ${path.posix.join(platform.output, mapFileName)}`);
});
}
})
.catch(e => {
console.log(`Unable to write the bundle to ${path.posix.join(platform.output, bundleFileName)}`);
console.log(e);
throw e;
});
})
.catch(e => {
console.log('Failed to write the bundle');
console.log(e);
throw e;
});
}
getFileFromCacheOrLoad(x) {
let found = this.fileCache[x];
if (found) {
return Promise.resolve(found);
}
return fs.readFile(x).then(data => {
found = { contents: data.toString() };
this.fileCache[x] = found;
return found;
});
}
writeLoaderCode(platform) {
const createLoaderCode = require('./loader').createLoaderCode;
let config = createLoaderCode(platform, this.bundler);
return 'function _aureliaConfigureModuleLoader(){' + config + '}';
}
setIndexFileConfigTarget(platform, location) {
//Replace the reference to the vendor bundle in the "index.html" file to use the correct build revision file (or remove the build revision hash);
const createSrcFileRegex = require('./utils').createSrcFileRegex;
let indexLocation = platform.index;
try {
let indexFile = fs.readFileSync(indexLocation);
let outputDir = platform.baseUrl || platform.output; //If we have a baseUrl, then the files are served from there, else it's the output
let configMatcher = createSrcFileRegex(outputDir, this.moduleId);
if (configMatcher.test(indexFile)) {
indexFile = indexFile.replace(configMatcher, 'src="$2' + location + '"');//Replace the old reference to the file with whatever the new one is (preserving any unknown path parts before)
fs.writeFile(indexLocation, indexFile);
} else {
console.log('Error: Unable to update ' + this.moduleId + ' path to ' + location + ', could not find existing reference to replace');
}
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Error: No index file found at "' + indexLocation + '"');
} else {
console.log(err);
}
}
}
};
function addFilesInOrder(bundle, paths, files) {
let index = -1;
function addFile() {
index++;
if (index < paths.length) {
return bundle.getFileFromCacheOrLoad(paths[index])
.then(file => files.push(file))
.then(addFile);
}
return Promise.resolve();
}
return addFile();
}
function unique(collection) {
let u = {};
let a = [];
for (let i = 0, l = collection.length; i < l; ++i) {
if (u.hasOwnProperty(collection[i])) {
continue;
}
a.push(collection[i]);
u[collection[i]] = 1;
}
return a;
}