-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
angular-patch.js
executable file
Β·101 lines (89 loc) Β· 2.71 KB
/
angular-patch.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
#!/usr/bin/env node
const path = require('path');
const pnpapi = require('pnpapi');
const {setResolveHook, ModuleNotFoundException} = require('@angular-devkit/core/node/resolve');
Error.stackTraceLimit = Infinity;
/**
* Returns a list of all the callers from the resolve() call.
* @returns {string[]}
*/
function _caller() {
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
/** @type {{ prepareStackTrace: (x: {}, stack: {}) => {} }} */
const error = Error;
const origPrepareStackTrace = error.prepareStackTrace;
error.prepareStackTrace = (_, stack) => stack;
/** @type {{ getFileName(): string }[] | undefined} */
const stack = (new Error()).stack;
error.prepareStackTrace = origPrepareStackTrace;
return stack ? stack.map(x => x.getFileName()).filter(x => !!x) : [];
}
setResolveHook(
/**
*
* @param {string} request
* @param {{paths?: string[]; checkLocal?: boolean; basedir: string}} options
*/
function resolveHook(request, options) {
const basePath = options.basedir;
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(request)) {
let res = path.resolve(basePath, request);
if (request === '..' || request.endsWith('/')) {
res += '/';
}
const m = resolve(res, options);
if (m) {
return m;
}
} else {
const n = resolve(request, options);
if (n) {
return n;
}
}
if (options.checkLocal) {
const callers = _caller();
for (const caller of callers) {
const localDir = path.dirname(caller);
if (localDir !== options.basedir) {
try {
return resolveHook(request, {
...options,
checkLocal: false,
basedir: localDir,
});
} catch (e) {
// Just swap the basePath with the original call one.
if (!(e instanceof ModuleNotFoundException)) {
throw e;
}
}
}
}
}
throw new ModuleNotFoundException(request, basePath);
function resolve(request, options) {
if (options.resolvePackageJson) {
try {
return pnpapi.resolveRequest(path.join(request, 'package.json'), options.basedir);
} catch (_) {
// ignore
}
}
if (request.endsWith('/')) {
try {
return pnpapi.resolveRequest(path.join(request, 'index'), options.basedir);
} catch (_) {
// ignore
}
} else {
try {
return pnpapi.resolveRequest(request, options.basedir);
} catch (_) {
// ignore
}
}
}
});
const {ng} = require('@angular/cli/package.json').bin;
require(require('path').join('@angular/cli', ng));