@rushstack/webpack-workspace-resolve-plugin 0.5.13 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +35 -0
- package/CHANGELOG.md +13 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib-esm/KnownDescriptionFilePlugin.js +100 -0
- package/lib-esm/KnownDescriptionFilePlugin.js.map +1 -0
- package/lib-esm/KnownPackageDependenciesPlugin.js +83 -0
- package/lib-esm/KnownPackageDependenciesPlugin.js.map +1 -0
- package/lib-esm/WorkspaceLayoutCache.js +85 -0
- package/lib-esm/WorkspaceLayoutCache.js.map +1 -0
- package/lib-esm/WorkspaceResolvePlugin.js +50 -0
- package/lib-esm/WorkspaceResolvePlugin.js.map +1 -0
- package/lib-esm/index.js +5 -0
- package/lib-esm/index.js.map +1 -0
- package/package.json +27 -6
- /package/{lib → lib-commonjs}/KnownDescriptionFilePlugin.js +0 -0
- /package/{lib → lib-commonjs}/KnownDescriptionFilePlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/KnownPackageDependenciesPlugin.js +0 -0
- /package/{lib → lib-commonjs}/KnownPackageDependenciesPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/WorkspaceLayoutCache.js +0 -0
- /package/{lib → lib-commonjs}/WorkspaceLayoutCache.js.map +0 -0
- /package/{lib → lib-commonjs}/WorkspaceResolvePlugin.js +0 -0
- /package/{lib → lib-commonjs}/WorkspaceResolvePlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/index.js +0 -0
- /package/{lib → lib-commonjs}/index.js.map +0 -0
- /package/{lib → lib-dts}/KnownDescriptionFilePlugin.d.ts +0 -0
- /package/{lib → lib-dts}/KnownDescriptionFilePlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/KnownPackageDependenciesPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/KnownPackageDependenciesPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/WorkspaceLayoutCache.d.ts +0 -0
- /package/{lib → lib-dts}/WorkspaceLayoutCache.d.ts.map +0 -0
- /package/{lib → lib-dts}/WorkspaceResolvePlugin.d.ts +0 -0
- /package/{lib → lib-dts}/WorkspaceResolvePlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/index.d.ts +0 -0
- /package/{lib → lib-dts}/index.d.ts.map +0 -0
package/CHANGELOG.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/webpack-workspace-resolve-plugin",
|
|
3
3
|
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"version": "0.6.0",
|
|
6
|
+
"tag": "@rushstack/webpack-workspace-resolve-plugin_v0.6.0",
|
|
7
|
+
"date": "Thu, 19 Feb 2026 00:04:53 GMT",
|
|
8
|
+
"comments": {
|
|
9
|
+
"minor": [
|
|
10
|
+
{
|
|
11
|
+
"comment": "Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `\"exports\"` field in `package.json`."
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"dependency": [
|
|
15
|
+
{
|
|
16
|
+
"comment": "Updating dependency \"@rushstack/lookup-by-path\" to `0.9.0`"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"comment": "Updating dependency \"@rushstack/heft\" to `1.2.0`"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"version": "0.5.14",
|
|
26
|
+
"tag": "@rushstack/webpack-workspace-resolve-plugin_v0.5.14",
|
|
27
|
+
"date": "Sat, 07 Feb 2026 01:13:26 GMT",
|
|
28
|
+
"comments": {
|
|
29
|
+
"dependency": [
|
|
30
|
+
{
|
|
31
|
+
"comment": "Updating dependency \"@rushstack/lookup-by-path\" to `0.8.16`"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"comment": "Updating dependency \"@rushstack/heft\" to `1.1.14`"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
},
|
|
4
39
|
{
|
|
5
40
|
"version": "0.5.13",
|
|
6
41
|
"tag": "@rushstack/webpack-workspace-resolve-plugin_v0.5.13",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
# Change Log - @rushstack/webpack-workspace-resolve-plugin
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Thu, 19 Feb 2026 00:04:53 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 0.6.0
|
|
6
|
+
Thu, 19 Feb 2026 00:04:53 GMT
|
|
7
|
+
|
|
8
|
+
### Minor changes
|
|
9
|
+
|
|
10
|
+
- Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `"exports"` field in `package.json`.
|
|
11
|
+
|
|
12
|
+
## 0.5.14
|
|
13
|
+
Sat, 07 Feb 2026 01:13:26 GMT
|
|
14
|
+
|
|
15
|
+
_Version update only_
|
|
4
16
|
|
|
5
17
|
## 0.5.13
|
|
6
18
|
Wed, 04 Feb 2026 20:42:47 GMT
|
package/dist/tsdoc-metadata.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
/**
|
|
4
|
+
* A resolver plugin that optimizes locating the package.json file for a module.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export class KnownDescriptionFilePlugin {
|
|
9
|
+
/**
|
|
10
|
+
* Constructs a new instance of `KnownDescriptionFilePlugin`.
|
|
11
|
+
* @param cache - The workspace layout cache
|
|
12
|
+
* @param source - The resolve step to hook into
|
|
13
|
+
* @param target - The resolve step to delegate to
|
|
14
|
+
* @param skipForContext - If true, don't apply this plugin if the resolver is configured to resolve to a context
|
|
15
|
+
*/
|
|
16
|
+
constructor(cache, source, target, skipForContext) {
|
|
17
|
+
this.source = source;
|
|
18
|
+
this.target = target;
|
|
19
|
+
this._cache = cache;
|
|
20
|
+
this._skipForContext = !!skipForContext;
|
|
21
|
+
}
|
|
22
|
+
apply(resolver) {
|
|
23
|
+
if (this._skipForContext && resolver.options.resolveToContext) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const target = resolver.ensureHook(this.target);
|
|
27
|
+
const { fileSystem } = resolver;
|
|
28
|
+
function readDescriptionFileWithParse(descriptionFilePath, callback) {
|
|
29
|
+
fileSystem.readFile(descriptionFilePath, (err, data) => {
|
|
30
|
+
if (!data?.length) {
|
|
31
|
+
return callback(err);
|
|
32
|
+
}
|
|
33
|
+
callback(null, JSON.parse(data.toString()));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const readDescriptionFile = fileSystem.readJson?.bind(fileSystem) ?? readDescriptionFileWithParse;
|
|
37
|
+
resolver
|
|
38
|
+
.getHook(this.source)
|
|
39
|
+
.tapAsync(KnownDescriptionFilePlugin.name, (request, resolveContext, callback) => {
|
|
40
|
+
const { path } = request;
|
|
41
|
+
if (!path) {
|
|
42
|
+
// No request, nothing to do.
|
|
43
|
+
return callback();
|
|
44
|
+
}
|
|
45
|
+
const cache = this._cache;
|
|
46
|
+
const match = cache.contextLookup.findLongestPrefixMatch(path);
|
|
47
|
+
if (!match) {
|
|
48
|
+
// No description file available, proceed without.
|
|
49
|
+
return callback();
|
|
50
|
+
}
|
|
51
|
+
const remainingPath = path.slice(match.index);
|
|
52
|
+
const relativePath = `.${cache.normalizeToSlash?.(remainingPath) ?? remainingPath}`;
|
|
53
|
+
const descriptionFileRoot = `${path.slice(0, match.index)}`;
|
|
54
|
+
const descriptionFilePath = `${descriptionFileRoot}${cache.resolverPathSeparator}package.json`;
|
|
55
|
+
const { contextForPackage } = cache;
|
|
56
|
+
readDescriptionFile(descriptionFilePath, (err, descriptionFileData) => {
|
|
57
|
+
if (!descriptionFileData) {
|
|
58
|
+
resolveContext.missingDependencies?.add(descriptionFilePath);
|
|
59
|
+
return callback(err);
|
|
60
|
+
}
|
|
61
|
+
resolveContext.fileDependencies?.add(descriptionFilePath);
|
|
62
|
+
// Store the resolver context since a WeakMap lookup is cheaper than walking the tree again
|
|
63
|
+
contextForPackage.set(descriptionFileData, match);
|
|
64
|
+
// Using the object literal is an order of magnitude faster, at least on node 18.19.1
|
|
65
|
+
const obj = {
|
|
66
|
+
path: request.path,
|
|
67
|
+
context: request.context,
|
|
68
|
+
descriptionFilePath,
|
|
69
|
+
descriptionFileRoot,
|
|
70
|
+
descriptionFileData,
|
|
71
|
+
relativePath,
|
|
72
|
+
ignoreSymlinks: request.ignoreSymlinks,
|
|
73
|
+
fullySpecified: request.fullySpecified,
|
|
74
|
+
__innerRequest: request.__innerRequest,
|
|
75
|
+
__innerRequest_request: request.__innerRequest_request,
|
|
76
|
+
__innerRequest_relativePath: request.__innerRequest_relativePath,
|
|
77
|
+
request: request.request,
|
|
78
|
+
query: request.query,
|
|
79
|
+
fragment: request.fragment,
|
|
80
|
+
module: request.module,
|
|
81
|
+
directory: request.directory,
|
|
82
|
+
file: request.file,
|
|
83
|
+
internal: request.internal
|
|
84
|
+
};
|
|
85
|
+
// Delegate to the resolver step at `target`.
|
|
86
|
+
resolver.doResolve(target, obj, 'using description file: ' + descriptionFilePath + ' (relative path: ' + relativePath + ')', resolveContext, (e, result) => {
|
|
87
|
+
if (e) {
|
|
88
|
+
return callback(e);
|
|
89
|
+
}
|
|
90
|
+
// Don't allow other processing
|
|
91
|
+
if (result === undefined) {
|
|
92
|
+
return callback(null, null);
|
|
93
|
+
}
|
|
94
|
+
callback(null, result);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=KnownDescriptionFilePlugin.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,CAAC,CAAC;IACP,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { InputFileSystem, Resolver } from 'webpack';\n\nimport type { IPrefixMatch } from '@rushstack/lookup-by-path';\n\nimport type { IResolveContext, WorkspaceLayoutCache } from './WorkspaceLayoutCache';\n\ntype ResolveRequest = Parameters<Resolver['hooks']['resolveStep']['call']>[1];\n\n/**\n * A resolver plugin that optimizes locating the package.json file for a module.\n *\n * @internal\n */\nexport class KnownDescriptionFilePlugin {\n public readonly source: string;\n public readonly target: string;\n\n private readonly _skipForContext: boolean;\n private readonly _cache: WorkspaceLayoutCache;\n\n /**\n * Constructs a new instance of `KnownDescriptionFilePlugin`.\n * @param cache - The workspace layout cache\n * @param source - The resolve step to hook into\n * @param target - The resolve step to delegate to\n * @param skipForContext - If true, don't apply this plugin if the resolver is configured to resolve to a context\n */\n public constructor(cache: WorkspaceLayoutCache, source: string, target: string, skipForContext?: boolean) {\n this.source = source;\n this.target = target;\n this._cache = cache;\n this._skipForContext = !!skipForContext;\n }\n\n public apply(resolver: Resolver): void {\n if (this._skipForContext && resolver.options.resolveToContext) {\n return;\n }\n\n const target: ReturnType<Resolver['ensureHook']> = resolver.ensureHook(this.target);\n const { fileSystem } = resolver;\n\n type JsonObjectTypes = ReturnType<NonNullable<InputFileSystem['readJsonSync']>>;\n\n function readDescriptionFileWithParse(\n descriptionFilePath: string,\n callback: (err: Error | null | undefined, data?: JsonObjectTypes) => void\n ): void {\n fileSystem.readFile(descriptionFilePath, (err: Error | null | undefined, data?: string | Buffer) => {\n if (!data?.length) {\n return callback(err);\n }\n callback(null, JSON.parse(data.toString()));\n });\n }\n\n const readDescriptionFile: (\n descriptionFilePath: string,\n cb: (err: Error | null | undefined, data?: JsonObjectTypes) => void\n ) => void = fileSystem.readJson?.bind(fileSystem) ?? readDescriptionFileWithParse;\n\n resolver\n .getHook(this.source)\n .tapAsync(KnownDescriptionFilePlugin.name, (request, resolveContext, callback) => {\n const { path } = request;\n if (!path) {\n // No request, nothing to do.\n return callback();\n }\n\n const cache: WorkspaceLayoutCache = this._cache;\n\n const match: IPrefixMatch<IResolveContext> | undefined =\n cache.contextLookup.findLongestPrefixMatch(path);\n if (!match) {\n // No description file available, proceed without.\n return callback();\n }\n\n const remainingPath: string = path.slice(match.index);\n const relativePath: string = `.${cache.normalizeToSlash?.(remainingPath) ?? remainingPath}`;\n const descriptionFileRoot: string = `${path.slice(0, match.index)}`;\n const descriptionFilePath: string = `${descriptionFileRoot}${cache.resolverPathSeparator}package.json`;\n\n const { contextForPackage } = cache;\n\n readDescriptionFile(descriptionFilePath, (err, descriptionFileData) => {\n if (!descriptionFileData) {\n resolveContext.missingDependencies?.add(descriptionFilePath);\n return callback(err);\n }\n\n resolveContext.fileDependencies?.add(descriptionFilePath);\n // Store the resolver context since a WeakMap lookup is cheaper than walking the tree again\n contextForPackage.set(descriptionFileData, match);\n\n // Using the object literal is an order of magnitude faster, at least on node 18.19.1\n const obj: ResolveRequest = {\n path: request.path,\n context: request.context,\n descriptionFilePath,\n descriptionFileRoot,\n descriptionFileData,\n relativePath,\n ignoreSymlinks: request.ignoreSymlinks,\n fullySpecified: request.fullySpecified,\n __innerRequest: request.__innerRequest,\n __innerRequest_request: request.__innerRequest_request,\n __innerRequest_relativePath: request.__innerRequest_relativePath,\n\n request: request.request,\n query: request.query,\n fragment: request.fragment,\n module: request.module,\n directory: request.directory,\n file: request.file,\n internal: request.internal\n };\n\n // Delegate to the resolver step at `target`.\n resolver.doResolve(\n target,\n obj,\n 'using description file: ' + descriptionFilePath + ' (relative path: ' + relativePath + ')',\n resolveContext,\n (e: Error | null | undefined, result: ResolveRequest | undefined) => {\n if (e) {\n return callback(e);\n }\n\n // Don't allow other processing\n if (result === undefined) {\n return callback(null, null);\n }\n callback(null, result);\n }\n );\n });\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
/**
|
|
4
|
+
* A resolver plugin that optimizes resolving installed dependencies for the current package.
|
|
5
|
+
* Enforces strict resolution.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class KnownPackageDependenciesPlugin {
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a new instance of `KnownPackageDependenciesPlugin`.
|
|
12
|
+
* @param cache - The workspace layout cache
|
|
13
|
+
* @param source - The resolve step to hook into
|
|
14
|
+
* @param target - The resolve step to delegate to
|
|
15
|
+
*/
|
|
16
|
+
constructor(cache, source, target) {
|
|
17
|
+
this.source = source;
|
|
18
|
+
this.target = target;
|
|
19
|
+
this._cache = cache;
|
|
20
|
+
}
|
|
21
|
+
apply(resolver) {
|
|
22
|
+
const target = resolver.ensureHook(this.target);
|
|
23
|
+
resolver
|
|
24
|
+
.getHook(this.source)
|
|
25
|
+
.tapAsync(KnownPackageDependenciesPlugin.name, (request, resolveContext, callback) => {
|
|
26
|
+
const { path, request: rawRequest } = request;
|
|
27
|
+
if (!path) {
|
|
28
|
+
return callback();
|
|
29
|
+
}
|
|
30
|
+
if (!rawRequest) {
|
|
31
|
+
return callback();
|
|
32
|
+
}
|
|
33
|
+
const { descriptionFileData } = request;
|
|
34
|
+
if (!descriptionFileData) {
|
|
35
|
+
return callback(new Error(`Expected descriptionFileData for ${path}`));
|
|
36
|
+
}
|
|
37
|
+
const cache = this._cache;
|
|
38
|
+
let scope = cache.contextForPackage.get(descriptionFileData);
|
|
39
|
+
if (!scope) {
|
|
40
|
+
scope = cache.contextLookup.findLongestPrefixMatch(path);
|
|
41
|
+
if (!scope) {
|
|
42
|
+
return callback(new Error(`Expected context for ${request.descriptionFileRoot}`));
|
|
43
|
+
}
|
|
44
|
+
cache.contextForPackage.set(descriptionFileData, scope);
|
|
45
|
+
}
|
|
46
|
+
let dependency;
|
|
47
|
+
while (scope && !dependency) {
|
|
48
|
+
dependency = scope.value.findDependency(rawRequest);
|
|
49
|
+
scope = scope.lastMatch;
|
|
50
|
+
}
|
|
51
|
+
if (!dependency) {
|
|
52
|
+
return callback();
|
|
53
|
+
}
|
|
54
|
+
const isPackageRoot = dependency.index === rawRequest.length;
|
|
55
|
+
const fullySpecified = isPackageRoot ? false : request.fullySpecified;
|
|
56
|
+
const remainingPath = isPackageRoot ? '.' : `.${rawRequest.slice(dependency.index)}`;
|
|
57
|
+
const relativePath = (remainingPath.length > 1 && cache.normalizeToSlash?.(remainingPath)) || remainingPath;
|
|
58
|
+
const { descriptionFileRoot } = dependency.value;
|
|
59
|
+
const obj = {
|
|
60
|
+
path: descriptionFileRoot,
|
|
61
|
+
context: request.context,
|
|
62
|
+
descriptionFilePath: `${descriptionFileRoot}${cache.resolverPathSeparator}package.json`,
|
|
63
|
+
descriptionFileRoot,
|
|
64
|
+
descriptionFileData: undefined,
|
|
65
|
+
relativePath,
|
|
66
|
+
ignoreSymlinks: request.ignoreSymlinks,
|
|
67
|
+
fullySpecified,
|
|
68
|
+
__innerRequest: request.__innerRequest,
|
|
69
|
+
__innerRequest_request: request.__innerRequest_request,
|
|
70
|
+
__innerRequest_relativePath: request.__innerRequest_relativePath,
|
|
71
|
+
request: relativePath,
|
|
72
|
+
query: request.query,
|
|
73
|
+
fragment: request.fragment,
|
|
74
|
+
module: false,
|
|
75
|
+
directory: request.directory,
|
|
76
|
+
file: request.file,
|
|
77
|
+
internal: request.internal
|
|
78
|
+
};
|
|
79
|
+
resolver.doResolve(target, obj, null, resolveContext, callback);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=KnownPackageDependenciesPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KnownPackageDependenciesPlugin.js","sourceRoot":"","sources":["../src/KnownPackageDependenciesPlugin.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DA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,cAAc,EAAE,OAAO,CAAC,cAAc;gBACtC,sBAAsB,EAAE,OAAO,CAAC,sBAAsB;gBACtD,2BAA2B,EAAE,OAAO,CAAC,2BAA2B;gBAEhE,OAAO,EAAE,YAAY;gBACrB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,QAAQ,EAAE,OAAO,CAAC,QAAQ;gBAC1B,MAAM,EAAE,KAAK;gBACb,SAAS,EAAE,OAAO,CAAC,SAAS;gBAC5B,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;aAC3B,CAAC;YACF,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,cAAc,EAAE,QAAQ,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;IACP,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { Resolver } from 'webpack';\n\nimport type { IPrefixMatch } from '@rushstack/lookup-by-path';\n\nimport type { IResolveContext, WorkspaceLayoutCache } from './WorkspaceLayoutCache';\n\ntype ResolveRequest = Parameters<Resolver['hooks']['resolveStep']['call']>[1];\n\n/**\n * A resolver plugin that optimizes resolving installed dependencies for the current package.\n * Enforces strict resolution.\n *\n * @internal\n */\nexport class KnownPackageDependenciesPlugin {\n public readonly source: string;\n public readonly target: string;\n\n private readonly _cache: WorkspaceLayoutCache;\n\n /**\n * Constructs a new instance of `KnownPackageDependenciesPlugin`.\n * @param cache - The workspace layout cache\n * @param source - The resolve step to hook into\n * @param target - The resolve step to delegate to\n */\n public constructor(cache: WorkspaceLayoutCache, source: string, target: string) {\n this.source = source;\n this.target = target;\n this._cache = cache;\n }\n\n public apply(resolver: Resolver): void {\n const target: ReturnType<Resolver['ensureHook']> = resolver.ensureHook(this.target);\n\n resolver\n .getHook(this.source)\n .tapAsync(KnownPackageDependenciesPlugin.name, (request, resolveContext, callback) => {\n const { path, request: rawRequest } = request;\n if (!path) {\n return callback();\n }\n\n if (!rawRequest) {\n return callback();\n }\n\n const { descriptionFileData } = request;\n if (!descriptionFileData) {\n return callback(new Error(`Expected descriptionFileData for ${path}`));\n }\n\n const cache: WorkspaceLayoutCache = this._cache;\n\n let scope: IPrefixMatch<IResolveContext> | undefined =\n cache.contextForPackage.get(descriptionFileData);\n if (!scope) {\n scope = cache.contextLookup.findLongestPrefixMatch(path);\n if (!scope) {\n return callback(new Error(`Expected context for ${request.descriptionFileRoot}`));\n }\n cache.contextForPackage.set(descriptionFileData, scope);\n }\n\n let dependency: IPrefixMatch<IResolveContext> | undefined;\n while (scope && !dependency) {\n dependency = scope.value.findDependency(rawRequest);\n scope = scope.lastMatch;\n }\n\n if (!dependency) {\n return callback();\n }\n\n const isPackageRoot: boolean = dependency.index === rawRequest.length;\n const fullySpecified: boolean | undefined = isPackageRoot ? false : request.fullySpecified;\n const remainingPath: string = isPackageRoot ? '.' : `.${rawRequest.slice(dependency.index)}`;\n const relativePath: string =\n (remainingPath.length > 1 && cache.normalizeToSlash?.(remainingPath)) || remainingPath;\n const { descriptionFileRoot } = dependency.value;\n const obj: ResolveRequest = {\n path: descriptionFileRoot,\n context: request.context,\n descriptionFilePath: `${descriptionFileRoot}${cache.resolverPathSeparator}package.json`,\n descriptionFileRoot,\n descriptionFileData: undefined,\n relativePath,\n ignoreSymlinks: request.ignoreSymlinks,\n fullySpecified,\n __innerRequest: request.__innerRequest,\n __innerRequest_request: request.__innerRequest_request,\n __innerRequest_relativePath: request.__innerRequest_relativePath,\n\n request: relativePath,\n query: request.query,\n fragment: request.fragment,\n module: false,\n directory: request.directory,\n file: request.file,\n internal: request.internal\n };\n resolver.doResolve(target, obj, null, resolveContext, callback);\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { sep as directorySeparator } from 'node:path';
|
|
4
|
+
import { LookupByPath } from '@rushstack/lookup-by-path';
|
|
5
|
+
function backslashToSlash(path) {
|
|
6
|
+
return path.replace(/\\/g, '/');
|
|
7
|
+
}
|
|
8
|
+
function slashToBackslash(path) {
|
|
9
|
+
return path.replace(/\//g, '\\');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A cache of workspace layout information.
|
|
13
|
+
* @beta
|
|
14
|
+
*/
|
|
15
|
+
export class WorkspaceLayoutCache {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
const { cacheData, resolverPathSeparator = directorySeparator } = options;
|
|
18
|
+
if (resolverPathSeparator !== '/' && resolverPathSeparator !== '\\') {
|
|
19
|
+
throw new Error(`Unsupported directory separator: ${resolverPathSeparator}`);
|
|
20
|
+
}
|
|
21
|
+
const { basePath } = cacheData;
|
|
22
|
+
const resolveContexts = [];
|
|
23
|
+
const contextLookup = new LookupByPath(undefined, resolverPathSeparator);
|
|
24
|
+
this.contextLookup = contextLookup;
|
|
25
|
+
this.contextForPackage = new WeakMap();
|
|
26
|
+
const normalizeToSlash = resolverPathSeparator === '\\' ? backslashToSlash : undefined;
|
|
27
|
+
const normalizeToPlatform = resolverPathSeparator === '\\' ? slashToBackslash : undefined;
|
|
28
|
+
this.resolverPathSeparator = resolverPathSeparator;
|
|
29
|
+
this.normalizeToSlash = normalizeToSlash;
|
|
30
|
+
this.normalizeToPlatform = normalizeToPlatform;
|
|
31
|
+
// Internal class due to coupling to `resolveContexts`
|
|
32
|
+
class ResolveContext {
|
|
33
|
+
constructor(serialized) {
|
|
34
|
+
this._serialized = serialized;
|
|
35
|
+
this._descriptionFileRoot = undefined;
|
|
36
|
+
this._dependencies = undefined;
|
|
37
|
+
}
|
|
38
|
+
get descriptionFileRoot() {
|
|
39
|
+
if (!this._descriptionFileRoot) {
|
|
40
|
+
const merged = `${basePath}${this._serialized.root}`;
|
|
41
|
+
this._descriptionFileRoot = normalizeToPlatform?.(merged) ?? merged;
|
|
42
|
+
}
|
|
43
|
+
return this._descriptionFileRoot;
|
|
44
|
+
}
|
|
45
|
+
findDependency(request) {
|
|
46
|
+
if (!this._dependencies) {
|
|
47
|
+
// Lazy initialize this object since most packages won't be requested.
|
|
48
|
+
const dependencies = new LookupByPath(undefined, '/');
|
|
49
|
+
const { name, deps } = this._serialized;
|
|
50
|
+
// Handle the self-reference scenario
|
|
51
|
+
dependencies.setItem(name, this);
|
|
52
|
+
if (deps) {
|
|
53
|
+
for (const [key, ordinal] of Object.entries(deps)) {
|
|
54
|
+
// This calls into the array of instances that is owned by WorkpaceLayoutCache
|
|
55
|
+
dependencies.setItem(key, resolveContexts[ordinal]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this._dependencies = dependencies;
|
|
59
|
+
}
|
|
60
|
+
return this._dependencies.findLongestPrefixMatch(request);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const serialized of cacheData.contexts) {
|
|
64
|
+
const resolveContext = new ResolveContext(serialized);
|
|
65
|
+
resolveContexts.push(resolveContext);
|
|
66
|
+
contextLookup.setItemFromSegments(concat(
|
|
67
|
+
// All paths in the cache file are platform-agnostic
|
|
68
|
+
LookupByPath.iteratePathSegments(basePath, '/'), LookupByPath.iteratePathSegments(serialized.root, '/')), resolveContext);
|
|
69
|
+
// Handle nested package.json files. These may modify some properties, but the dependency resolution
|
|
70
|
+
// will match the original package root. Typically these are used to set the `type` field to `module`.
|
|
71
|
+
if (serialized.dirInfoFiles) {
|
|
72
|
+
for (const file of serialized.dirInfoFiles) {
|
|
73
|
+
contextLookup.setItemFromSegments(concat(
|
|
74
|
+
// All paths in the cache file are platform-agnostic
|
|
75
|
+
concat(LookupByPath.iteratePathSegments(basePath, '/'), LookupByPath.iteratePathSegments(serialized.root, '/')), LookupByPath.iteratePathSegments(file, '/')), resolveContext);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function* concat(a, b) {
|
|
82
|
+
yield* a;
|
|
83
|
+
yield* b;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=WorkspaceLayoutCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WorkspaceLayoutCache.js","sourceRoot":"","sources":["../src/WorkspaceLayoutCache.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,GAAG,IAAI,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAqB,MAAM,2BAA2B,CAAC;AAoF5E,SAAS,gBAAgB,CAAC,IAAY;IACpC,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAY;IACpC,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAoB;IAc/B,YAAmB,OAAqC;QACtD,MAAM,EAAE,SAAS,EAAE,qBAAqB,GAAG,kBAAkB,EAAE,GAAG,OAAO,CAAC;QAE1E,IAAI,qBAAqB,KAAK,GAAG,IAAI,qBAAqB,KAAK,IAAI,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CAAC,oCAAoC,qBAAqB,EAAE,CAAC,CAAC;QAC/E,CAAC;QAED,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC;QAC/B,MAAM,eAAe,GAAqB,EAAE,CAAC;QAC7C,MAAM,aAAa,GAAkC,IAAI,YAAY,CAAC,SAAS,EAAE,qBAAqB,CAAC,CAAC;QAExG,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,iBAAiB,GAAG,IAAI,OAAO,EAAyC,CAAC;QAE9E,MAAM,gBAAgB,GACpB,qBAAqB,KAAK,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,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ACX,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { sep as directorySeparator } from 'node:path';\n\nimport { LookupByPath, type IPrefixMatch } from '@rushstack/lookup-by-path';\n\n/**\n * Information about a local or installed npm package.\n * @beta\n */\nexport interface ISerializedResolveContext {\n /**\n * The path to the root folder of this context.\n * This path is normalized to use `/` as the separator and should not end with a trailing `/`.\n */\n root: string;\n /**\n * The name of this package. Used to inject a self-reference into the dependency map.\n */\n name: string;\n /**\n * Map of declared dependencies (if any) to the ordinal of the corresponding context.\n */\n deps?: Record<string, number>;\n /**\n * Set of relative paths to nested `package.json` files within this context.\n * These paths are normalized to use `/` as the separator and should not begin with a leading `./`.\n */\n dirInfoFiles?: string[];\n}\n\n/**\n * The serialized form of the cache file. This file is expected to be generated by a separate tool from\n * information known to the package manager. Namely, the dependency relationships between packages, and\n * all the `package.json` files in the workspace (installed or local).\n * @beta\n */\nexport interface IResolverCacheFile {\n /**\n * The base path. All paths in context entries are prefixed by this path.\n */\n basePath: string;\n /**\n * The ordered list of all contexts in the cache\n */\n contexts: ISerializedResolveContext[];\n}\n\n/**\n * A context for resolving dependencies in a workspace.\n * @beta\n */\nexport interface IResolveContext {\n /**\n * The absolute path to the root folder of this context\n */\n descriptionFileRoot: string;\n /**\n * Find the context that corresponds to a module specifier, when requested in the current context.\n * @param request - The module specifier to resolve\n */\n findDependency(request: string): IPrefixMatch<IResolveContext> | undefined;\n}\n\n/**\n * Options for creating a `WorkspaceLayoutCache`.\n * @beta\n */\nexport interface IWorkspaceLayoutCacheOptions {\n /**\n * The parsed cache data. File reading is left as an exercise for the caller.\n */\n cacheData: IResolverCacheFile;\n /**\n * The directory separator used in the `path` field of the resolver inputs.\n * Will usually be `path.sep`.\n */\n resolverPathSeparator?: '/' | '\\\\';\n}\n\n/**\n * A function that normalizes a path to a platform-specific format (if needed).\n * Will be undefined if the platform uses `/` as the path separator.\n *\n * @beta\n */\nexport type IPathNormalizationFunction = ((input: string) => string) | undefined;\n\nfunction backslashToSlash(path: string): string {\n return path.replace(/\\\\/g, '/');\n}\n\nfunction slashToBackslash(path: string): string {\n return path.replace(/\\//g, '\\\\');\n}\n\n/**\n * A cache of workspace layout information.\n * @beta\n */\nexport class WorkspaceLayoutCache {\n /**\n * A lookup of context roots to their corresponding context objects\n */\n public readonly contextLookup: LookupByPath<IResolveContext>;\n /**\n * A weak map of package JSON contents to their corresponding context objects\n */\n public readonly contextForPackage: WeakMap<object, IPrefixMatch<IResolveContext>>;\n\n public readonly resolverPathSeparator: string;\n public readonly normalizeToSlash: IPathNormalizationFunction;\n public readonly normalizeToPlatform: IPathNormalizationFunction;\n\n public constructor(options: IWorkspaceLayoutCacheOptions) {\n const { cacheData, resolverPathSeparator = directorySeparator } = options;\n\n if (resolverPathSeparator !== '/' && resolverPathSeparator !== '\\\\') {\n throw new Error(`Unsupported directory separator: ${resolverPathSeparator}`);\n }\n\n const { basePath } = cacheData;\n const resolveContexts: ResolveContext[] = [];\n const contextLookup: LookupByPath<IResolveContext> = new LookupByPath(undefined, resolverPathSeparator);\n\n this.contextLookup = contextLookup;\n this.contextForPackage = new WeakMap<object, IPrefixMatch<IResolveContext>>();\n\n const normalizeToSlash: IPathNormalizationFunction =\n resolverPathSeparator === '\\\\' ? backslashToSlash : undefined;\n const normalizeToPlatform: IPathNormalizationFunction =\n resolverPathSeparator === '\\\\' ? slashToBackslash : undefined;\n\n this.resolverPathSeparator = resolverPathSeparator;\n this.normalizeToSlash = normalizeToSlash;\n this.normalizeToPlatform = normalizeToPlatform;\n\n // Internal class due to coupling to `resolveContexts`\n class ResolveContext implements IResolveContext {\n private readonly _serialized: ISerializedResolveContext;\n private _descriptionFileRoot: string | undefined;\n private _dependencies: LookupByPath<IResolveContext> | undefined;\n\n public constructor(serialized: ISerializedResolveContext) {\n this._serialized = serialized;\n this._descriptionFileRoot = undefined;\n this._dependencies = undefined;\n }\n\n public get descriptionFileRoot(): string {\n if (!this._descriptionFileRoot) {\n const merged: string = `${basePath}${this._serialized.root}`;\n this._descriptionFileRoot = normalizeToPlatform?.(merged) ?? merged;\n }\n return this._descriptionFileRoot;\n }\n\n public findDependency(request: string): IPrefixMatch<IResolveContext> | undefined {\n if (!this._dependencies) {\n // Lazy initialize this object since most packages won't be requested.\n const dependencies: LookupByPath<IResolveContext> = new LookupByPath(undefined, '/');\n\n const { name, deps } = this._serialized;\n\n // Handle the self-reference scenario\n dependencies.setItem(name, this);\n if (deps) {\n for (const [key, ordinal] of Object.entries(deps)) {\n // This calls into the array of instances that is owned by WorkpaceLayoutCache\n dependencies.setItem(key, resolveContexts[ordinal]);\n }\n }\n this._dependencies = dependencies;\n }\n\n return this._dependencies.findLongestPrefixMatch(request);\n }\n }\n\n for (const serialized of cacheData.contexts) {\n const resolveContext: ResolveContext = new ResolveContext(serialized);\n resolveContexts.push(resolveContext);\n\n contextLookup.setItemFromSegments(\n concat<string>(\n // All paths in the cache file are platform-agnostic\n LookupByPath.iteratePathSegments(basePath, '/'),\n LookupByPath.iteratePathSegments(serialized.root, '/')\n ),\n resolveContext\n );\n\n // Handle nested package.json files. These may modify some properties, but the dependency resolution\n // will match the original package root. Typically these are used to set the `type` field to `module`.\n if (serialized.dirInfoFiles) {\n for (const file of serialized.dirInfoFiles) {\n contextLookup.setItemFromSegments(\n concat<string>(\n // All paths in the cache file are platform-agnostic\n concat<string>(\n LookupByPath.iteratePathSegments(basePath, '/'),\n LookupByPath.iteratePathSegments(serialized.root, '/')\n ),\n LookupByPath.iteratePathSegments(file, '/')\n ),\n resolveContext\n );\n }\n }\n }\n }\n}\n\nfunction* concat<T>(a: Iterable<T>, b: Iterable<T>): IterableIterator<T> {\n yield* a;\n yield* b;\n}\n"]}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { KnownDescriptionFilePlugin } from './KnownDescriptionFilePlugin';
|
|
4
|
+
import { KnownPackageDependenciesPlugin } from './KnownPackageDependenciesPlugin';
|
|
5
|
+
/**
|
|
6
|
+
* A Webpack plugin that optimizes package.json lookups and resolution of bare specifiers in a monorepo.
|
|
7
|
+
*
|
|
8
|
+
* @beta
|
|
9
|
+
*/
|
|
10
|
+
export class WorkspaceResolvePlugin {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this._cache = options.cache;
|
|
13
|
+
this._resolverNames = new Set(options.resolverNames ?? ['normal', 'context', 'loader']);
|
|
14
|
+
}
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
const cache = this._cache;
|
|
17
|
+
function handler(resolveOptions) {
|
|
18
|
+
// Omit default `node_modules`
|
|
19
|
+
if (resolveOptions.modules) {
|
|
20
|
+
resolveOptions.modules = resolveOptions.modules.filter((modulePath) => {
|
|
21
|
+
return modulePath !== 'node_modules';
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
resolveOptions.modules = [];
|
|
26
|
+
}
|
|
27
|
+
resolveOptions.plugins ?? (resolveOptions.plugins = []);
|
|
28
|
+
resolveOptions.plugins.push(
|
|
29
|
+
// Optimize identifying the package.json file for the issuer
|
|
30
|
+
new KnownDescriptionFilePlugin(cache, 'before-parsed-resolve', 'described-resolve'),
|
|
31
|
+
// Optimize locating the installed dependencies of the current package
|
|
32
|
+
new KnownPackageDependenciesPlugin(cache, 'before-raw-module', 'resolve-as-module'),
|
|
33
|
+
// Optimize loading the package.json file for the destination package (bare specifier)
|
|
34
|
+
new KnownDescriptionFilePlugin(cache, 'before-resolve-as-module', 'resolve-in-package'),
|
|
35
|
+
// Optimize loading the package.json file for the destination package (relative path)
|
|
36
|
+
new KnownDescriptionFilePlugin(cache, 'before-relative', 'described-relative'),
|
|
37
|
+
// Optimize locating and loading nested package.json for a directory
|
|
38
|
+
new KnownDescriptionFilePlugin(cache, 'before-undescribed-existing-directory', 'existing-directory', true),
|
|
39
|
+
// Optimize locating and loading nested package.json for a file
|
|
40
|
+
new KnownDescriptionFilePlugin(cache, 'before-undescribed-raw-file', 'raw-file'));
|
|
41
|
+
return resolveOptions;
|
|
42
|
+
}
|
|
43
|
+
for (const resolverName of this._resolverNames) {
|
|
44
|
+
compiler.resolverFactory.hooks.resolveOptions
|
|
45
|
+
.for(resolverName)
|
|
46
|
+
.tap(WorkspaceResolvePlugin.name, handler);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=WorkspaceResolvePlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WorkspaceResolvePlugin.js","sourceRoot":"","sources":["../src/WorkspaceResolvePlugin.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAK3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAoBlF;;;;GAIG;AACH,MAAM,OAAO,sBAAsB;IAIjC,YAAmB,OAAuC;QACxD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5B,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,aAAa,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC1F,CAAC;IAEM,KAAK,CAAC,QAAkB;QAC7B,MAAM,KAAK,GAAyB,IAAI,CAAC,MAAM,CAAC;QAEhD,SAAS,OAAO,CAAC,cAA8B;YAC7C,8BAA8B;YAC9B,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;gBAC3B,cAAc,CAAC,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,UAAkB,EAAE,EAAE;oBAC5E,OAAO,UAAU,KAAK,cAAc,CAAC;gBACvC,CAAC,CAAC,CAAC;YACL,CAAC;iBAAM,CAAC;gBACN,cAAc,CAAC,OAAO,GAAG,EAAE,CAAC;YAC9B,CAAC;YAED,cAAc,CAAC,OAAO,KAAtB,cAAc,CAAC,OAAO,GAAK,EAAE,EAAC;YAC9B,cAAc,CAAC,OAAO,CAAC,IAAI;YACzB,4DAA4D;YAC5D,IAAI,0BAA0B,CAAC,KAAK,EAAE,uBAAuB,EAAE,mBAAmB,CAAC;YACnF,sEAAsE;YACtE,IAAI,8BAA8B,CAAC,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,CAAC;YACnF,sFAAsF;YACtF,IAAI,0BAA0B,CAAC,KAAK,EAAE,0BAA0B,EAAE,oBAAoB,CAAC;YACvF,qFAAqF;YACrF,IAAI,0BAA0B,CAAC,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,CAAC;YAC9E,oEAAoE;YACpE,IAAI,0BAA0B,CAC5B,KAAK,EACL,uCAAuC,EACvC,oBAAoB,EACpB,IAAI,CACL;YACD,+DAA+D;YAC/D,IAAI,0BAA0B,CAAC,KAAK,EAAE,6BAA6B,EAAE,UAAU,CAAC,CACjF,CAAC;YAEF,OAAO,cAAc,CAAC;QACxB,CAAC;QACD,KAAK,MAAM,YAAY,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YAC/C,QAAQ,CAAC,eAAe,CAAC,KAAK,CAAC,cAAc;iBAC1C,GAAG,CAAC,YAAY,CAAC;iBACjB,GAAG,CAAC,sBAAsB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { WebpackPluginInstance, Compiler, ResolveOptions } from 'webpack';\n\nimport type { WorkspaceLayoutCache } from './WorkspaceLayoutCache';\nimport { KnownDescriptionFilePlugin } from './KnownDescriptionFilePlugin';\nimport { KnownPackageDependenciesPlugin } from './KnownPackageDependenciesPlugin';\n\n/**\n * Options for constructing a `WorkspaceResolvePlugin`.\n *\n * @beta\n */\nexport interface IWorkspaceResolvePluginOptions {\n /**\n * The cache of workspace layout information.\n */\n cache: WorkspaceLayoutCache;\n\n /**\n * Which webpack resolvers to apply the plugin to.\n * @defaultValue ['normal', 'context', 'loader']\n */\n resolverNames?: Iterable<string>;\n}\n\n/**\n * A Webpack plugin that optimizes package.json lookups and resolution of bare specifiers in a monorepo.\n *\n * @beta\n */\nexport class WorkspaceResolvePlugin implements WebpackPluginInstance {\n private readonly _cache: WorkspaceLayoutCache;\n private readonly _resolverNames: Set<string>;\n\n public constructor(options: IWorkspaceResolvePluginOptions) {\n this._cache = options.cache;\n this._resolverNames = new Set(options.resolverNames ?? ['normal', 'context', 'loader']);\n }\n\n public apply(compiler: Compiler): void {\n const cache: WorkspaceLayoutCache = this._cache;\n\n function handler(resolveOptions: ResolveOptions): ResolveOptions {\n // Omit default `node_modules`\n if (resolveOptions.modules) {\n resolveOptions.modules = resolveOptions.modules.filter((modulePath: string) => {\n return modulePath !== 'node_modules';\n });\n } else {\n resolveOptions.modules = [];\n }\n\n resolveOptions.plugins ??= [];\n resolveOptions.plugins.push(\n // Optimize identifying the package.json file for the issuer\n new KnownDescriptionFilePlugin(cache, 'before-parsed-resolve', 'described-resolve'),\n // Optimize locating the installed dependencies of the current package\n new KnownPackageDependenciesPlugin(cache, 'before-raw-module', 'resolve-as-module'),\n // Optimize loading the package.json file for the destination package (bare specifier)\n new KnownDescriptionFilePlugin(cache, 'before-resolve-as-module', 'resolve-in-package'),\n // Optimize loading the package.json file for the destination package (relative path)\n new KnownDescriptionFilePlugin(cache, 'before-relative', 'described-relative'),\n // Optimize locating and loading nested package.json for a directory\n new KnownDescriptionFilePlugin(\n cache,\n 'before-undescribed-existing-directory',\n 'existing-directory',\n true\n ),\n // Optimize locating and loading nested package.json for a file\n new KnownDescriptionFilePlugin(cache, 'before-undescribed-raw-file', 'raw-file')\n );\n\n return resolveOptions;\n }\n for (const resolverName of this._resolverNames) {\n compiler.resolverFactory.hooks.resolveOptions\n .for(resolverName)\n .tap(WorkspaceResolvePlugin.name, handler);\n }\n }\n}\n"]}
|
package/lib-esm/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
export { WorkspaceResolvePlugin } from './WorkspaceResolvePlugin';
|
|
4
|
+
export { WorkspaceLayoutCache } from './WorkspaceLayoutCache';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,sBAAsB,EAAuC,MAAM,0BAA0B,CAAC;AACvG,OAAO,EACL,oBAAoB,EAMrB,MAAM,wBAAwB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nexport { WorkspaceResolvePlugin, type IWorkspaceResolvePluginOptions } from './WorkspaceResolvePlugin';\nexport {\n WorkspaceLayoutCache,\n type IPathNormalizationFunction,\n type IWorkspaceLayoutCacheOptions,\n type IResolveContext,\n type ISerializedResolveContext,\n type IResolverCacheFile\n} from './WorkspaceLayoutCache';\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/webpack-workspace-resolve-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "This plugin leverages workspace-level metadata to greatly accelerate module resolution.",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "./lib-commonjs/index.js",
|
|
6
|
+
"module": "./lib-esm/index.js",
|
|
7
|
+
"types": "./dist/webpack-workspace-resolve-plugin.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/webpack-workspace-resolve-plugin.d.ts",
|
|
11
|
+
"import": "./lib-esm/index.js",
|
|
12
|
+
"require": "./lib-commonjs/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./lib/*": {
|
|
15
|
+
"types": "./lib-dts/*.d.ts",
|
|
16
|
+
"import": "./lib-esm/*.js",
|
|
17
|
+
"require": "./lib-commonjs/*.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
"*": {
|
|
23
|
+
"lib/*": [
|
|
24
|
+
"lib-dts/*"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
7
28
|
"license": "MIT",
|
|
8
29
|
"repository": {
|
|
9
30
|
"type": "git",
|
|
@@ -18,14 +39,14 @@
|
|
|
18
39
|
"@types/node": "*"
|
|
19
40
|
},
|
|
20
41
|
"dependencies": {
|
|
21
|
-
"@rushstack/lookup-by-path": "0.
|
|
42
|
+
"@rushstack/lookup-by-path": "0.9.0"
|
|
22
43
|
},
|
|
23
44
|
"devDependencies": {
|
|
24
45
|
"@types/node": "20.17.19",
|
|
25
46
|
"eslint": "~9.37.0",
|
|
26
47
|
"memfs": "4.12.0",
|
|
27
|
-
"webpack": "~5.
|
|
28
|
-
"@rushstack/heft": "1.
|
|
48
|
+
"webpack": "~5.105.2",
|
|
49
|
+
"@rushstack/heft": "1.2.0",
|
|
29
50
|
"local-node-rig": "1.0.0"
|
|
30
51
|
},
|
|
31
52
|
"sideEffects": false,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|