Skip to content

Commit 8a9eb70

Browse files
authored
Include basePath in resolver cache file, instead of being external (microsoft#4906)
* [webpack-resolve] Expect basePath in resolver cache file * [rush-resolve] Include basePath in resolver cache file --------- Co-authored-by: David Michon <dmichon-msft@users.noreply.github.com>
1 parent 1d42386 commit 8a9eb70

10 files changed

Lines changed: 3697 additions & 3664 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "In rush-resolver-cache-plugin, include the base path in the resolver cache file.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/webpack-workspace-resolve-plugin",
5+
"comment": "Expect the base path to be part of the resolver cache file.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/webpack-workspace-resolve-plugin"
10+
}

common/reviews/api/webpack-workspace-resolve-plugin.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface IResolveContext {
2020

2121
// @beta
2222
export interface IResolverCacheFile {
23+
basePath: string;
2324
contexts: ISerializedResolveContext[];
2425
}
2526

@@ -35,7 +36,6 @@ export interface ISerializedResolveContext {
3536
export interface IWorkspaceLayoutCacheOptions {
3637
cacheData: IResolverCacheFile;
3738
resolverPathSeparator?: '/' | '\\';
38-
workspaceRoot: string;
3939
}
4040

4141
// @beta

rush-plugins/rush-resolver-cache-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ A Rush plugin that runs after successful dependency installation and generates a
44

55
When this plugin is installed, it will produce a file called `resolver-cache.json` in the temp directory of the default subspace, e.g. `<repo>/common/temp/default/resolver-cache.json`
66

7-
To use this file, load it, call JSON.parse, and pass the result as the `cacheData` property to the `WorkspaceLayoutCache` constructor from `@rushstack/webpack-workspace-resolve-plugin`. The `workspaceRoot` property should be set to the path `<repo>`.
7+
To use this file, load it, call JSON.parse, and pass the result as the `cacheData` property to the `WorkspaceLayoutCache` constructor from `@rushstack/webpack-workspace-resolve-plugin`.

rush-plugins/rush-resolver-cache-plugin/src/computeResolverCacheFromLockfileAsync.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export async function computeResolverCacheFromLockfileAsync(
254254
);
255255

256256
const cacheFile: IResolverCacheFile = {
257+
basePath: commonPrefixToTrim,
257258
contexts: serializedContexts
258259
};
259260

rush-plugins/rush-resolver-cache-plugin/src/test/__snapshots__/computeResolverCacheFromLockfileAsync.test.ts.snap

Lines changed: 3654 additions & 3651 deletions
Large diffs are not rendered by default.

rush-plugins/rush-resolver-cache-plugin/src/test/computeResolverCacheFromLockfileAsync.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ interface ITestCase {
2424
const TEST_CASES: readonly ITestCase[] = [
2525
{
2626
workspaceRoot: '/$root/common/temp/build-tests',
27-
commonPrefixToTrim: '/$root',
27+
commonPrefixToTrim: '/$root/',
2828
lockfileName: 'build-tests-subspace.yaml'
2929
},
3030
{
3131
workspaceRoot: '/$root/common/temp/default',
32-
commonPrefixToTrim: '/$root',
32+
commonPrefixToTrim: '/$root/',
3333
lockfileName: 'default-subspace.yaml'
3434
},
3535
{
3636
workspaceRoot: '/$root/common/temp/bundled-dependencies',
37-
commonPrefixToTrim: '/$root',
37+
commonPrefixToTrim: '/$root/',
3838
lockfileName: 'bundled-dependencies.yaml',
3939
afterExternalPackagesAsync: async (contexts: Map<string, IResolverContext>) => {
4040
for (const context of contexts.values()) {
@@ -105,7 +105,7 @@ describe(computeResolverCacheFromLockfileAsync.name, () => {
105105
for (const importerPath of lockfile.importers.keys()) {
106106
const remainder: string = importerPath.slice(importerPath.lastIndexOf('../') + 3);
107107
projectByImporterPath.setItem(importerPath, {
108-
projectFolder: `${commonPrefixToTrim}/${remainder}`,
108+
projectFolder: `${commonPrefixToTrim}${remainder}`,
109109
packageJson: {
110110
name: `@local/${remainder.replace(/\//g, '+')}`
111111
}

webpack/webpack-workspace-resolve-plugin/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ When using this plugin, the following options should be configured for your reso
1717
- `symlinks: false` - Since the cache knows the symlinks for package dependencies, you can avoid the cost of testing for other symlinks unless you are using additional symlinks.
1818
- `modules: []` - The cache should contain all information necessary to locate available dependencies for any arbitrary folder. If you need to allow resolution in other roots, you can add those, but omit `'node_modules'`.
1919

20+
## Impact
21+
22+
This plugin should eliminate file system calls associated with the following operations of NodeJS module resolution in webpack:
23+
- Find the nearest `package.json` to the calling module
24+
- Locate a named package from a calling module
25+
- Identify a `package.json` in a resolved directory
26+
- Find the nearest `package.json` to a resolved file path
27+
2028
## Limitations
2129

2230
This plugin depends on the presence of a cache file in the workspace to function. Data in this cache file is assumed not to change while the webpack process is running.

webpack/webpack-workspace-resolve-plugin/src/WorkspaceLayoutCache.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface ISerializedResolveContext {
3737
* @beta
3838
*/
3939
export interface IResolverCacheFile {
40+
/**
41+
* The base path. All paths in context entries are prefixed by this path.
42+
*/
43+
basePath: string;
4044
/**
4145
* The ordered list of all contexts in the cache
4246
*/
@@ -64,10 +68,6 @@ export interface IResolveContext {
6468
* @beta
6569
*/
6670
export interface IWorkspaceLayoutCacheOptions {
67-
/**
68-
* The root folder of the workspace. All paths in the cache file are assumed to be relative to this folder.
69-
*/
70-
workspaceRoot: string;
7171
/**
7272
* The parsed cache data. File reading is left as an exercise for the caller.
7373
*/
@@ -114,12 +114,13 @@ export class WorkspaceLayoutCache {
114114
public readonly normalizeToPlatform: IPathNormalizationFunction;
115115

116116
public constructor(options: IWorkspaceLayoutCacheOptions) {
117-
const { workspaceRoot, cacheData, resolverPathSeparator = directorySeparator } = options;
117+
const { cacheData, resolverPathSeparator = directorySeparator } = options;
118118

119119
if (resolverPathSeparator !== '/' && resolverPathSeparator !== '\\') {
120120
throw new Error(`Unsupported directory separator: ${resolverPathSeparator}`);
121121
}
122122

123+
const { basePath } = cacheData;
123124
const resolveContexts: ResolveContext[] = [];
124125
const contextLookup: LookupByPath<IResolveContext> = new LookupByPath(undefined, resolverPathSeparator);
125126

@@ -149,7 +150,7 @@ export class WorkspaceLayoutCache {
149150

150151
public get descriptionFileRoot(): string {
151152
if (!this._descriptionFileRoot) {
152-
this._descriptionFileRoot = `${workspaceRoot}${resolverPathSeparator}${
153+
this._descriptionFileRoot = `${basePath}${
153154
normalizeToPlatform?.(this._serialized.root) ?? this._serialized.root
154155
}`;
155156
}

webpack/webpack-workspace-resolve-plugin/src/test/createResolveForTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export function createResolveForTests(
3030
const fileSystem: Volume = new Volume();
3131

3232
const cache: WorkspaceLayoutCache = new WorkspaceLayoutCache({
33-
workspaceRoot: `${separator}workspace`,
3433
cacheData: {
34+
basePath: `${separator}workspace${separator}`,
3535
contexts: [
3636
{
3737
root: 'a',

0 commit comments

Comments
 (0)