-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
bundled-source.js
364 lines (304 loc) · 10.8 KB
/
bundled-source.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
const path = require('path');
const findDeps = require('./find-deps').findDeps;
const cjsTransform = require('./amodro-trace/read/cjs');
const esTransform = require('./amodro-trace/read/es');
const allWriteTransforms = require('./amodro-trace/write/all');
const Utils = require('./utils');
const logger = require('aurelia-logging').getLogger('BundledSource');
const { getAliases, toDotDot } = require('./module-id-processor');
exports.BundledSource = class {
constructor(bundler, file) {
this.bundler = bundler;
this.file = file;
this.includedIn = null;
this.includedBy = null;
this._contents = null;
this.requiresTransform = true;
}
get sourceMap() {
return this.file.sourceMap;
}
get path() {
return this.file.path;
}
set contents(value) {
this._contents = value;
}
get contents() {
return this._contents === null
? (this._contents = this.file.contents.toString())
: this._contents;
}
get dependencyInclusion() {
// source-inclusion could be created by dependency-inclusion
if (this.includedBy) {
return this.includedBy.includedBy;
}
}
_getProjectRoot() {
return this.bundler.project.paths.root;
}
_getLoaderPlugins() {
return this.bundler.loaderOptions.plugins;
}
_getLoaderType() {
return this.bundler.loaderOptions.type;
}
_getLoaderConfig() {
return this.bundler.loaderConfig;
}
_getUseCache() {
return this.bundler.buildOptions.isApplicable('cache');
}
get moduleId() {
if (this._moduleId) return this._moduleId;
let dependencyInclusion = this.dependencyInclusion;
let projectRoot = this._getProjectRoot();
let moduleId;
if (dependencyInclusion) {
let loaderConfig = dependencyInclusion.description.loaderConfig;
let root = path.resolve(projectRoot, loaderConfig.path);
moduleId = path.join(loaderConfig.name, this.path.replace(root, ''));
} else {
let modulePath = path.relative(projectRoot, this.path);
moduleId = path.normalize(modulePath);
}
moduleId = moduleId.replace(/\\/g, '/');
if (moduleId.toLowerCase().endsWith('.js')) {
moduleId = moduleId.slice(0, -3);
}
this._moduleId = toDotDot(moduleId);
return this._moduleId;
}
update(file) {
this.file = file;
this._contents = null;
this.requiresTransform = true;
if (this.includedIn) {
this.includedIn.requiresBuild = true;
} else {
logger.warn(this.path + ' is not captured by any bundle file. You might need to adjust the bundles source matcher in aurelia.json.');
}
}
transform() {
if (!this.requiresTransform) {
return;
}
let dependencyInclusion = this.dependencyInclusion;
let browserReplacement = dependencyInclusion &&
dependencyInclusion.description.browserReplacement();
let loaderPlugins = this._getLoaderPlugins();
let loaderType = this._getLoaderType();
let loaderConfig = this._getLoaderConfig();
let moduleId = this.moduleId;
let modulePath = this.path;
getAliases(moduleId, loaderConfig.paths).forEach(alias => {
this.bundler.configTargetBundle.addAlias(alias.fromId, alias.toId);
});
logger.debug(`Tracing ${moduleId}`);
let deps;
let matchingPlugin = loaderPlugins.find(p => p.matches(modulePath));
if (path.extname(modulePath).toLowerCase() === '.json') {
// support text! prefix
let contents = `define('${Utils.moduleIdWithPlugin(moduleId, 'text', loaderType)}',[],function(){return ${JSON.stringify(this.contents)};});\n`;
// support Node.js's json module
contents += `define('${moduleId}',['${Utils.moduleIdWithPlugin(moduleId, 'text', loaderType)}'],function(m){return JSON.parse(m);});\n`;
// be nice to requirejs json plugin users, add json! prefix
contents += `define('${Utils.moduleIdWithPlugin(moduleId, 'json', loaderType)}',['${moduleId}'],function(m){return m;});\n`;
this.contents = contents;
} else if (matchingPlugin) {
deps = findDeps(modulePath, this.contents, loaderType);
this.contents = matchingPlugin.transform(moduleId, modulePath, this.contents);
} else {
deps = [];
let context = {pkgsMainMap: {}, config: {shim: {}}};
let desc = dependencyInclusion && dependencyInclusion.description;
if (desc && desc.mainId === moduleId) {
// main file of node package
context.pkgsMainMap[moduleId] = desc.name;
}
let wrapShim = false;
let replacement = {};
if (dependencyInclusion) {
let description = dependencyInclusion.description;
if (description.loaderConfig.deps || description.loaderConfig.exports) {
context.config.shim[description.name] = {
deps: description.loaderConfig.deps,
'exports': description.loaderConfig.exports
};
}
if (description.loaderConfig.deps) {
// force deps for shimed package
deps.push.apply(deps, description.loaderConfig.deps);
}
if (description.loaderConfig.wrapShim) {
wrapShim = true;
}
if (browserReplacement) {
for (let i = 0, keys = Object.keys(browserReplacement); i < keys.length; i++) {
let key = keys[i];
let target = browserReplacement[key];
const baseId = description.name + '/index';
const sourceModule = key.startsWith('.') ?
relativeModuleId(moduleId, absoluteModuleId(baseId, key)) :
key;
let targetModule;
if (target) {
targetModule = relativeModuleId(moduleId, absoluteModuleId(baseId, target));
} else {
// {"module-a": false}
// replace with special placeholder __ignore__
targetModule = '__ignore__';
}
replacement[sourceModule] = targetModule;
}
}
}
const opts = {
stubModules: loaderConfig.stubModules,
wrapShim: wrapShim || loaderConfig.wrapShim,
replacement: replacement
};
// Use cache for js files to avoid expensive parsing and transform.
let cache;
let hash;
const useCache = this._getUseCache();
if (useCache) {
// Only hash on moduleId, opts and contents.
// This ensures cache on npm packages can be shared
// among different apps.
const key = [
moduleId,
loaderType,
JSON.stringify(context),
JSON.stringify(opts),
this.contents // contents here is after gulp transpile task
].join('|');
hash = Utils.generateHash(key);
cache = Utils.getCache(hash);
}
if (cache) {
this.contents = cache.contents;
deps = cache.deps;
} else {
let contents;
// forceCjsWrap bypasses a r.js parse bug.
// See lib/amodro-trace/read/cjs.js for more info.
let forceCjsWrap = !!modulePath.match(/(\/|\\)(cjs|commonjs)(\/|\\)/i) ||
// core-js uses "var define = ..." everywhere, we need to force cjs
// before we can switch to dumberjs bundler
(desc && desc.name === 'core-js');
try {
contents = cjsTransform(modulePath, this.contents, forceCjsWrap);
} catch {
// file is not in amd/cjs format, try native es module
try {
contents = esTransform(modulePath, this.contents);
} catch (e) {
logger.error('Could not convert to AMD module, skipping ' + modulePath);
logger.error('Error was: ' + e);
contents = this.contents;
}
}
const writeTransform = allWriteTransforms(opts);
contents = writeTransform(context, moduleId, modulePath, contents);
const tracedDeps = findDeps(modulePath, contents, loaderType);
if (tracedDeps && tracedDeps.length) {
deps.push.apply(deps, tracedDeps);
}
if (deps) {
let needsCssInjection = false;
(new Set(deps)).forEach(dep => {
// ignore module with plugin prefix/subfix
if (dep.indexOf('!') !== -1) return;
// only check css file
if (path.extname(dep).toLowerCase() !== '.css') return;
needsCssInjection = true;
dep = absoluteModuleId(moduleId, dep);
// inject css to document head
contents += `\ndefine('${dep}',['__inject_css__','${Utils.moduleIdWithPlugin(dep, 'text', loaderType)}'],function(i,c){i(c,'_au_css:${dep}');});\n`;
});
if (needsCssInjection) deps.push('__inject_css__');
}
this.contents = contents;
// write cache
if (useCache && hash) {
Utils.setCache(hash, {
deps: deps,
contents: this.contents
});
}
}
}
this.requiresTransform = false;
if (!deps || deps.length === 0) return;
let needed = new Set();
Array.from(new Set(deps)) // unique
.map(d => {
const loc = d.indexOf('!');
if (loc < 1 || loc === d.length - 1) return d;
let pluginName = d.slice(0, loc);
let dep = d.slice(loc + 1);
if (loaderType === 'system') {
[pluginName, dep] = [dep, pluginName];
}
if (pluginName !== 'text' && pluginName !== 'json') {
needed.add(pluginName);
}
return dep;
})
.filter(d => {
// any dep requested by a npm package file
if (this.dependencyInclusion) return true;
// For local src, pick up all absolute dep.
if (d[0] !== '.') return true;
// For relative dep, as we bundled all of local js/html/css files,
// only pick up unknown ext that might be missed by gulp tasks.
return Utils.couldMissGulpPreprocess(d);
})
.map(d => absoluteModuleId(moduleId, d))
.forEach(d => {
// ignore false replacment
if (browserReplacement && browserReplacement.hasOwnProperty(d)) {
if (browserReplacement[d] === false) {
return;
}
}
needed.add(d);
});
return Array.from(needed);
}
};
function absoluteModuleId(baseId, moduleId) {
if (moduleId[0] !== '.') return moduleId;
let parts = baseId.split('/');
parts.pop();
moduleId.split('/').forEach(p => {
if (p === '.') return;
if (p === '..') {
parts.pop();
return;
}
parts.push(p);
});
return parts.join('/');
}
function relativeModuleId(baseId, moduleId) {
if (moduleId[0] === '.') return moduleId;
let baseParts = baseId.split('/');
baseParts.pop();
let parts = moduleId.split('/');
while (parts.length && baseParts.length && baseParts[0] === parts[0]) {
baseParts.shift();
parts.shift();
}
let left = baseParts.length;
if (left === 0) {
parts.unshift('.');
} else {
for (let i = 0; i < left; i ++) {
parts.unshift('..');
}
}
return parts.join('/');
}