-
Notifications
You must be signed in to change notification settings - Fork 6
/
npm-extension.js
450 lines (394 loc) · 13 KB
/
npm-extension.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"format cjs";
var utils = require("./npm-utils");
exports.includeInBuild = true;
var isNode = typeof process === "object" && {}.toString.call(process) ===
"[object process]";
var isWorker = typeof WorkerGlobalScope !== "undefined" && (self instanceof WorkerGlobalScope);
var isBrowser = typeof window !== "undefined" && !isNode && !isWorker;
exports.addExtension = function(System){
if (System._extensions) {
System._extensions.push(exports.addExtension);
}
/**
* Normalize has to deal with a "tricky" situation. There are module names like
* "css" -> "css" normalize like normal
* "./qunit" //-> "qunit" ... could go to steal-qunit#qunit, but then everything would?
*
* isRoot?
* "can-slider" //-> "path/to/main"
*
* else
*
* "can-slider" //-> "can-slider#path/to/main"
*/
var oldNormalize = System.normalize;
System.normalize = function(name, parentName, parentAddress, pluginNormalize){
if(parentName && this.npmParentMap && this.npmParentMap[parentName]) {
parentName = this.npmParentMap[parentName];
}
var hasNoParent = !parentName;
var nameIsRelative = utils.path.isRelative(name);
var parentIsNpmModule = utils.moduleName.isNpm(parentName);
var identifierEndsWithSlash = utils.path.endsWithSlash(name);
// If this is a relative module name and the parent is not an npm module
// we can skip all of this logic.
if(parentName && nameIsRelative && !parentIsNpmModule) {
return oldNormalize.call(this, name, parentName, parentAddress,
pluginNormalize);
}
// don't normalize name module yet if it includes any conditionals
if (utils.moduleName.isConditional(name)) {
return oldNormalize.call(this, name, parentName, parentAddress,
pluginNormalize);
}
// Check against contextual maps that would not be converted.
var hasContextualMap = typeof this.map[parentName] === "object" &&
this.map[parentName][name];
if(hasContextualMap) {
return oldNormalize.call(this, name, parentName, parentAddress,
pluginNormalize);
}
// Get the current package
var refPkg = utils.pkg.findByModuleNameOrAddress(this, parentName,
parentAddress);
// this isn't in a package, so ignore
if(!refPkg) {
return oldNormalize.call(this, name, parentName, parentAddress,
pluginNormalize);
}
// If name is ./ or ../
var isPointingAtParentFolder = name === "../" || name === "./";
if(parentIsNpmModule && isPointingAtParentFolder) {
var parsedParentModuleName = utils.moduleName.parse(parentName);
var parentModulePath = parsedParentModuleName.modulePath || "";
var relativePath = utils.path.relativeTo(parentModulePath, name);
var isInRoot = utils.path.isPackageRootDir(relativePath);
if(isInRoot) {
name = refPkg.name + "#" + utils.path.removeJS(refPkg.main);
} else {
name = name + "index";
}
}
// Using the current package, get info about what it is probably asking for
var parsedModuleName = utils.moduleName.parseFromPackage(this, refPkg,
name,
parentName);
var isRoot = utils.pkg.isRoot(this, refPkg);
var parsedPackageNameIsReferringPackage =
parsedModuleName.packageName === refPkg.name;
// Are we normalizing a module that is relative to another npm module?
var isRelativeToParentNpmModule =
parentIsNpmModule &&
nameIsRelative &&
parsedPackageNameIsReferringPackage;
// Look for the dependency package specified by the current package
var depPkg, wantedPkg;
// If we are within the same package then refPkg is the package we care
// about
if(isRelativeToParentNpmModule) {
depPkg = refPkg;
}
var context = this.npmContext;
var crawl = context && context.crawl;
var isDev = !!crawl;
if(!depPkg) {
// Development mode
if(crawl) {
var parentPkg = nameIsRelative ? null :
crawl.matchedVersion(context, refPkg.name,
refPkg.version);
if(parentPkg) {
wantedPkg = crawl.getDependencyMap(this, parentPkg, isRoot)[parsedModuleName.packageName];
if(wantedPkg) {
var foundPkg = crawl.matchedVersion(this.npmContext,
wantedPkg.name,
wantedPkg.version);
if(foundPkg) {
depPkg = utils.pkg.findByUrl(this, foundPkg.fileUrl);
}
}
}
} else {
if(isRoot) {
depPkg = utils.pkg.findDepWalking(this, refPkg,
parsedModuleName.packageName);
} else {
depPkg = utils.pkg.findDep(this, refPkg,
parsedModuleName.packageName);
}
}
}
// If the parent package is loading itself by name, look up by version
if(parsedPackageNameIsReferringPackage) {
depPkg = utils.pkg.findByNameAndVersion(this,
parsedModuleName.packageName,
refPkg.version);
}
// This really shouldn't happen, but lets find a package.
var lookupByName = parsedModuleName.isGlobal || hasNoParent;
if (!depPkg) {
depPkg = utils.pkg.findByName(this, parsedModuleName.packageName);
}
var isThePackageWeWant = !isDev || !depPkg ||
(wantedPkg ? crawl.pkgSatisfies(depPkg, wantedPkg.version) : true);
if(!isThePackageWeWant) {
depPkg = undefined;
} else if(isDev && depPkg) {
utils.pkg.saveResolution(context, refPkg, depPkg);
}
// It could be something like `fs` so check in globals
if(!depPkg) {
var browserPackageName = this.globalBrowser[parsedModuleName.packageName];
if(browserPackageName) {
parsedModuleName.packageName = browserPackageName.moduleName;
depPkg = utils.pkg.findByName(this, parsedModuleName.packageName);
}
}
// It could be the root main.
if(!depPkg && isRoot && name === refPkg.main &&
utils.pkg.hasDirectoriesLib(refPkg)) {
parsedModuleName.version = refPkg.version;
parsedModuleName.packageName = refPkg.name;
parsedModuleName.modulePath = utils.pkg.main(refPkg);
return oldNormalize.call(this,
utils.moduleName.create(parsedModuleName),
parentName, parentAddress, pluginNormalize);
}
// TODO Here is where we should progressively load the package.json files.
var loader = this;
if(!depPkg) {
if(crawl) {
var parentPkg = crawl.matchedVersion(this.npmContext, refPkg.name,
refPkg.version);
if(parentPkg) {
depPkg = crawl.getDependencyMap(this, parentPkg, isRoot)[parsedModuleName.packageName];
}
}
if(!depPkg) {
if(refPkg.browser && refPkg.browser[name]) {
return oldNormalize.call(this, refPkg.browser[name], parentName,
parentAddress, pluginNormalize);
}
return oldNormalize.call(this, name, parentName, parentAddress,
pluginNormalize);
}
return crawl.dep(this.npmContext, parentPkg, refPkg, depPkg, isRoot)
.then(createModuleNameAndNormalize);
} else {
return createModuleNameAndNormalize(depPkg);
}
function createModuleNameAndNormalize(depPkg){
parsedModuleName.version = depPkg.version;
// add the main path
if(!parsedModuleName.modulePath) {
parsedModuleName.modulePath = utils.pkg.main(depPkg);
}
var moduleName = utils.moduleName.create(parsedModuleName);
// Apply mappings, if they exist in the refPkg
var steal = utils.pkg.config(refPkg);
if(steal && steal.map &&
typeof steal.map[moduleName] === "string") {
moduleName = steal.map[moduleName];
}
var p = oldNormalize.call(loader, moduleName, parentName,
parentAddress, pluginNormalize);
// For identifiers like ./lib/ save this info as we might
// get a 404 and need to retry with lib/index.js
if(identifierEndsWithSlash) {
p.then(function(name){
if(context && context.forwardSlashMap) {
context.forwardSlashMap[name] = true;
}
});
}
return p;
}
};
var oldLocate = System.locate;
System.locate = function(load){
var parsedModuleName = utils.moduleName.parse(load.name),
loader = this;
// @ is not the first character
if(parsedModuleName.version && this.npm && !loader.paths[load.name]) {
var pkg = this.npm[utils.moduleName.nameAndVersion(parsedModuleName)];
if(pkg) {
return oldLocate.call(this, load).then(function(address){
var expectedAddress = utils.path.joinURIs(
System.baseURL, load.name
);
if(isBrowser) {
expectedAddress = expectedAddress.replace(/#/g, "%23");
}
// If locate didn't do the expected thing then we're going
// to guess that we shouldn't perform NPM lookup on this
// module as there might be a wildcard path.
if(address !== expectedAddress + ".js" &&
address !== expectedAddress) {
return address;
}
var root = utils.pkg.rootDir(pkg, utils.pkg.isRoot(loader, pkg));
if(parsedModuleName.modulePath) {
var npmAddress = utils.path.joinURIs(
utils.path.addEndingSlash(root),
parsedModuleName.plugin ?
parsedModuleName.modulePath :
utils.path.addJS(parsedModuleName.modulePath)
);
address = typeof steal !== "undefined" ?
utils.path.joinURIs(loader.baseURL, npmAddress) :
npmAddress;
}
return address;
});
}
}
return oldLocate.call(this, load);
};
var oldFetch = System.fetch;
System.fetch = function(load){
if(load.metadata.dryRun) {
return oldFetch.apply(this, arguments);
}
var loader = this;
var context = loader.npmContext;
var fetchPromise = Promise.resolve(oldFetch.apply(this, arguments));
if(utils.moduleName.isNpm(load.name)) {
fetchPromise = fetchPromise.then(null, function(err){
if(err.statusCode !== 404) {
return Promise.reject(err);
}
// Begin attempting retries. `retryTypes` defines different
// types of retries to do, currently retrying on the
// /index and /package.json conventions.
var types = [].slice.call(retryTypes);
return retryAll(types, err);
function retryAll(types, err){
if(!types.length) {
throw err;
}
var type = types.shift();
if(!type.test(load)) {
throw err;
}
return Promise.resolve(retryFetch.call(loader, load, type))
.then(null, function(err){
return retryAll(types, err);
});
}
});
}
return fetchPromise;
};
// Given a moduleName convert it into a npm-style moduleName if it belongs
// to a package.
var convertName = function(loader, name){
var pkg = utils.pkg.findByName(loader, name.split("/")[0]);
if(pkg) {
var parsed = utils.moduleName.parse(name, pkg.name);
parsed.version = pkg.version;
if(!parsed.modulePath) {
parsed.modulePath = utils.pkg.main(pkg);
}
return utils.moduleName.create(parsed);
}
return name;
};
var configSpecial = {
map: function(map){
var newMap = {}, val;
for(var name in map) {
val = map[name];
newMap[convertName(this, name)] = typeof val === "object"
? configSpecial.map(val)
: convertName(this, val);
}
return newMap;
},
meta: function(map){
var newMap = {};
for(var name in map){
newMap[convertName(this, name)] = map[name];
}
return newMap;
},
paths: function(paths){
var newPaths = {};
for(var name in paths) {
newPaths[convertName(this, name)] = paths[name];
}
return newPaths;
}
};
var oldConfig = System.config;
System.config = function(cfg){
var loader = this;
// Use npm-convert if it is available as it is better
// and has the ability to push mappings into a waiting queue.
if(loader.npmContext) {
var context = loader.npmContext;
var pkg = context.versions.__default;
context.convert.steal(context, pkg, cfg, true, false, false);
oldConfig.apply(loader, arguments);
return;
}
for(var name in cfg) {
if(configSpecial[name]) {
cfg[name] = configSpecial[name].call(loader, cfg[name]);
}
}
oldConfig.apply(loader, arguments);
};
function retryFetch(load, type) {
var loader = this;
// Get the new moduleName to test against
var moduleName = typeof type.name === "function" ?
type.name(loader, load) :
load.name + type.name;
var local = utils.extend({}, load);
local.name = moduleName;
local.metadata = { dryRun: true };
return Promise.resolve(loader.locate(local))
.then(function(address){
local.address = address;
return loader.fetch(local);
})
.then(function(source){
load.metadata.address = local.address;
loader.npmParentMap[load.name] = local.name;
var npmLoad = loader.npmContext &&
loader.npmContext.npmLoad;
if(npmLoad) {
npmLoad.saveLoadIfNeeded(loader.npmContext);
if(!isNode) {
utils.warnOnce("Some 404s were encountered " +
"while loading. Don't panic! " +
"These will only happen in dev " +
"and are harmless.");
}
}
return source;
});
}
// These define ways to retry a fetch when it fails (404)
var retryTypes = [
{
name: function(loader, load){
var context = loader.npmContext;
if(context.forwardSlashMap[load.name]) {
var parts = load.name.split("/");
parts.pop();
return parts.concat(["index"]).join("/");
}
return load.name + "/index";
},
test: function() { return true; }
},
{
name: ".json",
test: function(load){
return utils.moduleName.isNpm(load.name) &&
utils.path.basename(load.address) === "package.js";
}
}
];
};