Skip to content

Commit 96022c6

Browse files
committed
fix(@angular-devkit/core): fix bootstrap-local for custom resolve
1 parent 70c2dc3 commit 96022c6

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

lib/bootstrap-local.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ require.extensions['.ejs'] = function (m, filename) {
9494
};
9595

9696

97+
const packages = require('./packages').packages;
9798
// If we're running locally, meaning npm linked. This is basically "developer mode".
9899
if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
99-
const packages = require('./packages').packages;
100100

101101
// We mock the module loader so that we can fake our packages when running locally.
102102
const Module = require('module');
@@ -107,7 +107,7 @@ if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
107107
if (request in packages) {
108108
return packages[request].main;
109109
} else {
110-
let match = Object.keys(packages).find(pkgName => request.startsWith(pkgName + '/'));
110+
const match = Object.keys(packages).find(pkgName => request.startsWith(pkgName + '/'));
111111
if (match) {
112112
const p = path.join(packages[match].root, request.substr(match.length));
113113
return oldResolve.call(this, p, parent);
@@ -117,3 +117,29 @@ if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
117117
}
118118
};
119119
}
120+
121+
122+
// Set the resolve hook to allow resolution of packages from a local dev environment.
123+
require('@angular-devkit/core/node/resolve').setResolveHook(function(request, options) {
124+
try {
125+
if (request in packages) {
126+
if (options.resolvePackageJson) {
127+
return path.join(packages[request].root, 'package.json');
128+
} else {
129+
return packages[request].main;
130+
}
131+
} else {
132+
const match = Object.keys(packages).find(function(pkgName) {
133+
return request.startsWith(pkgName + '/');
134+
});
135+
136+
if (match) {
137+
return path.join(packages[match].root, request.substr(match[0].length));
138+
} else {
139+
return null;
140+
}
141+
}
142+
} catch (_) {
143+
return null;
144+
}
145+
});

packages/angular_devkit/core/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
import * as fs from './fs';
99

1010
export * from './cli-logger';
11-
export * from './resolve';
11+
export { ModuleNotFoundException, ResolveOptions, resolve } from './resolve';
1212

1313
export { fs };

packages/angular_devkit/core/node/resolve.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ export interface ResolveOptions {
108108
resolvePackageJson?: boolean;
109109
}
110110

111+
112+
let _resolveHook: ((x: string, options: ResolveOptions) => string | null) | null = null;
113+
export function setResolveHook(
114+
hook: ((x: string, options: ResolveOptions) => string | null) | null,
115+
) {
116+
_resolveHook = hook;
117+
}
118+
119+
111120
/**
112121
* Resolve a package using a logic similar to npm require.resolve, but with more options.
113122
* @param x The package name to resolve.
@@ -117,6 +126,13 @@ export interface ResolveOptions {
117126
* @throws {ModuleNotFoundException} If no module with that name was found anywhere.
118127
*/
119128
export function resolve(x: string, options: ResolveOptions): string {
129+
if (_resolveHook) {
130+
const maybe = _resolveHook(x, options);
131+
if (maybe) {
132+
return maybe;
133+
}
134+
}
135+
120136
const readFileSync = fs.readFileSync;
121137

122138
const extensions: string[] = options.extensions || Object.keys(require.extensions);

0 commit comments

Comments
 (0)