1
1
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
2
// See LICENSE in the project root for license information.
3
3
4
- import type { WebpackPluginInstance , Compiler } from 'webpack' ;
4
+ import type { WebpackPluginInstance , Compiler , ResolveOptions } from 'webpack' ;
5
5
6
6
import type { WorkspaceLayoutCache } from './WorkspaceLayoutCache' ;
7
7
import { KnownDescriptionFilePlugin } from './KnownDescriptionFilePlugin' ;
@@ -17,6 +17,12 @@ export interface IWorkspaceResolvePluginOptions {
17
17
* The cache of workspace layout information.
18
18
*/
19
19
cache : WorkspaceLayoutCache ;
20
+
21
+ /**
22
+ * Which webpack resolvers to apply the plugin to.
23
+ * @defaultValue ['normal', 'context', 'loader']
24
+ */
25
+ resolverNames ?: Iterable < string > ;
20
26
}
21
27
22
28
/**
@@ -26,48 +32,53 @@ export interface IWorkspaceResolvePluginOptions {
26
32
*/
27
33
export class WorkspaceResolvePlugin implements WebpackPluginInstance {
28
34
private readonly _cache : WorkspaceLayoutCache ;
35
+ private readonly _resolverNames : Set < string > ;
29
36
30
- public constructor ( cache : WorkspaceLayoutCache ) {
31
- this . _cache = cache ;
37
+ public constructor ( options : IWorkspaceResolvePluginOptions ) {
38
+ this . _cache = options . cache ;
39
+ this . _resolverNames = new Set ( options . resolverNames ?? [ 'normal' , 'context' , 'loader' ] ) ;
32
40
}
33
41
34
42
public apply ( compiler : Compiler ) : void {
35
- compiler . resolverFactory . hooks . resolveOptions
36
- . for ( 'normal' )
37
- . tap ( WorkspaceResolvePlugin . name , ( resolveOptions ) => {
38
- // Omit default `node_modules`
39
- if ( resolveOptions . modules ) {
40
- resolveOptions . modules = resolveOptions . modules . filter ( ( modulePath : string ) => {
41
- return modulePath !== 'node_modules' ;
42
- } ) ;
43
- } else {
44
- resolveOptions . modules = [ ] ;
45
- }
43
+ const cache : WorkspaceLayoutCache = this . _cache ;
46
44
47
- const cache : WorkspaceLayoutCache = this . _cache ;
45
+ function handler ( resolveOptions : ResolveOptions ) : ResolveOptions {
46
+ // Omit default `node_modules`
47
+ if ( resolveOptions . modules ) {
48
+ resolveOptions . modules = resolveOptions . modules . filter ( ( modulePath : string ) => {
49
+ return modulePath !== 'node_modules' ;
50
+ } ) ;
51
+ } else {
52
+ resolveOptions . modules = [ ] ;
53
+ }
48
54
49
- resolveOptions . plugins ??= [ ] ;
50
- resolveOptions . plugins . push (
51
- // Optimize identifying the package.json file for the issuer
52
- new KnownDescriptionFilePlugin ( cache , 'before-parsed-resolve' , 'described-resolve' ) ,
53
- // Optimize locating the installed dependencies of the current package
54
- new KnownPackageDependenciesPlugin ( cache , 'before-raw-module' , 'resolve-as-module' ) ,
55
- // Optimize loading the package.json file for the destination package (bare specifier)
56
- new KnownDescriptionFilePlugin ( cache , 'before-resolve-as-module' , 'resolve-in-package' ) ,
57
- // Optimize loading the package.json file for the destination package (relative path)
58
- new KnownDescriptionFilePlugin ( cache , 'before-relative' , 'described-relative' ) ,
59
- // Optimize locating and loading nested package.json for a directory
60
- new KnownDescriptionFilePlugin (
61
- cache ,
62
- 'before-undescribed-existing-directory' ,
63
- 'existing-directory' ,
64
- true
65
- ) ,
66
- // Optimize locating and loading nested package.json for a file
67
- new KnownDescriptionFilePlugin ( cache , 'before-undescribed-raw-file' , 'raw-file' )
68
- ) ;
55
+ resolveOptions . plugins ??= [ ] ;
56
+ resolveOptions . plugins . push (
57
+ // Optimize identifying the package.json file for the issuer
58
+ new KnownDescriptionFilePlugin ( cache , 'before-parsed-resolve' , 'described-resolve' ) ,
59
+ // Optimize locating the installed dependencies of the current package
60
+ new KnownPackageDependenciesPlugin ( cache , 'before-raw-module' , 'resolve-as-module' ) ,
61
+ // Optimize loading the package.json file for the destination package (bare specifier)
62
+ new KnownDescriptionFilePlugin ( cache , 'before-resolve-as-module' , 'resolve-in-package' ) ,
63
+ // Optimize loading the package.json file for the destination package (relative path)
64
+ new KnownDescriptionFilePlugin ( cache , 'before-relative' , 'described-relative' ) ,
65
+ // Optimize locating and loading nested package.json for a directory
66
+ new KnownDescriptionFilePlugin (
67
+ cache ,
68
+ 'before-undescribed-existing-directory' ,
69
+ 'existing-directory' ,
70
+ true
71
+ ) ,
72
+ // Optimize locating and loading nested package.json for a file
73
+ new KnownDescriptionFilePlugin ( cache , 'before-undescribed-raw-file' , 'raw-file' )
74
+ ) ;
69
75
70
- return resolveOptions ;
71
- } ) ;
76
+ return resolveOptions ;
77
+ }
78
+ for ( const resolverName of this . _resolverNames ) {
79
+ compiler . resolverFactory . hooks . resolveOptions
80
+ . for ( resolverName )
81
+ . tap ( WorkspaceResolvePlugin . name , handler ) ;
82
+ }
72
83
}
73
84
}
0 commit comments