2020 */
2121
2222import Path from 'path' ;
23- import Fs from 'fs' ;
2423
2524import webpack from 'webpack' ;
2625
@@ -29,20 +28,6 @@ import { BundleRefModule } from './bundle_ref_module';
2928
3029const RESOLVE_EXTENSIONS = [ '.js' , '.ts' , '.tsx' ] ;
3130
32- function safeStat ( path : string ) : Promise < Fs . Stats | undefined > {
33- return new Promise ( ( resolve , reject ) => {
34- Fs . stat ( path , ( error , stat ) => {
35- if ( error ?. code === 'ENOENT' ) {
36- resolve ( undefined ) ;
37- } else if ( error ) {
38- reject ( error ) ;
39- } else {
40- resolve ( stat ) ;
41- }
42- } ) ;
43- } ) ;
44- }
45-
4631interface RequestData {
4732 context : string ;
4833 dependencies : Array < { request : string } > ;
@@ -80,7 +65,7 @@ export class BundleRefsPlugin {
8065 const context = data . context ;
8166 const dep = data . dependencies [ 0 ] ;
8267
83- this . maybeReplaceImport ( context , dep . request ) . then (
68+ this . maybeReplaceImport ( context , dep . request , compiler ) . then (
8469 ( module ) => {
8570 if ( ! module ) {
8671 wrappedFactory ( data , callback ) ;
@@ -134,64 +119,17 @@ export class BundleRefsPlugin {
134119 } ) ;
135120 }
136121
137- private cachedResolveRefEntry ( ref : BundleRef ) {
138- const cached = this . resolvedRefEntryCache . get ( ref ) ;
139-
140- if ( cached ) {
141- return cached ;
142- }
143-
144- const absoluteRequest = Path . resolve ( ref . contextDir , ref . entry ) ;
145- const promise = this . cachedResolveRequest ( absoluteRequest ) . then ( ( resolved ) => {
146- if ( ! resolved ) {
147- throw new Error ( `Unable to resolve request [${ ref . entry } ] relative to [${ ref . contextDir } ]` ) ;
148- }
149-
150- return resolved ;
151- } ) ;
152- this . resolvedRefEntryCache . set ( ref , promise ) ;
153- return promise ;
154- }
155-
156- private cachedResolveRequest ( absoluteRequest : string ) {
157- const cached = this . resolvedRequestCache . get ( absoluteRequest ) ;
158-
159- if ( cached ) {
160- return cached ;
161- }
162-
163- const promise = this . resolveRequest ( absoluteRequest ) ;
164- this . resolvedRequestCache . set ( absoluteRequest , promise ) ;
165- return promise ;
166- }
167-
168- private async resolveRequest ( absoluteRequest : string ) {
169- const stats = await safeStat ( absoluteRequest ) ;
170- if ( stats && stats . isFile ( ) ) {
171- return absoluteRequest ;
172- }
173-
174- // look for an index file in directories
175- if ( stats ?. isDirectory ( ) ) {
176- for ( const ext of RESOLVE_EXTENSIONS ) {
177- const indexPath = Path . resolve ( absoluteRequest , `index${ ext } ` ) ;
178- const indexStats = await safeStat ( indexPath ) ;
179- if ( indexStats ?. isFile ( ) ) {
180- return indexPath ;
122+ private async resolve ( request : string , startPath : string , compiler : webpack . Compiler ) {
123+ const resolver = compiler . resolverFactory . get ( 'normal' ) ;
124+ return new Promise < string | undefined > ( ( resolve , reject ) => {
125+ resolver . resolve ( { } , startPath , request , { } , ( err : unknown | null , resolvedPath : string ) => {
126+ if ( err ) {
127+ reject ( err ) ;
128+ } else {
129+ resolve ( resolvedPath ) ;
181130 }
182- }
183- }
184-
185- // look for a file with one of the supported extensions
186- for ( const ext of RESOLVE_EXTENSIONS ) {
187- const filePath = `${ absoluteRequest } ${ ext } ` ;
188- const fileStats = await safeStat ( filePath ) ;
189- if ( fileStats ?. isFile ( ) ) {
190- return filePath ;
191- }
192- }
193-
194- return ;
131+ } ) ;
132+ } ) ;
195133 }
196134
197135 /**
@@ -200,9 +138,12 @@ export class BundleRefsPlugin {
200138 * then an error is thrown. If the request does not resolve to a bundleRef then
201139 * undefined is returned. Otherwise it returns the referenced bundleRef.
202140 */
203- private async maybeReplaceImport ( context : string , request : string ) {
204- // ignore imports that have loaders defined or are not relative seeming
205- if ( request . includes ( '!' ) || ! request . startsWith ( '.' ) ) {
141+ private async maybeReplaceImport ( context : string , request : string , compiler : webpack . Compiler ) {
142+ const alias = Object . keys ( compiler . options . resolve ?. alias ?? { } ) ;
143+ const isAliasRequest = alias . some ( ( a ) => request . startsWith ( a ) ) ;
144+
145+ // For non-alias import path, ignore imports that have loaders defined or are not relative seeming
146+ if ( ! isAliasRequest && ( request . includes ( '!' ) || ! request . startsWith ( '.' ) ) ) {
206147 return ;
207148 }
208149
@@ -211,13 +152,12 @@ export class BundleRefsPlugin {
211152 return ;
212153 }
213154
214- const absoluteRequest = Path . resolve ( context , request ) ;
215- if ( absoluteRequest . startsWith ( this . ignorePrefix ) ) {
155+ const resolved = await this . resolve ( request , context , compiler ) ;
156+ if ( ! resolved ) {
216157 return ;
217158 }
218159
219- const resolved = await this . cachedResolveRequest ( absoluteRequest ) ;
220- if ( ! resolved ) {
160+ if ( resolved . startsWith ( this . ignorePrefix ) ) {
221161 return ;
222162 }
223163
@@ -228,7 +168,7 @@ export class BundleRefsPlugin {
228168 }
229169
230170 for ( const ref of possibleRefs ) {
231- const resolvedEntry = await this . cachedResolveRefEntry ( ref ) ;
171+ const resolvedEntry = await this . resolve ( `./ ${ ref . entry } ` , ref . contextDir , compiler ) ;
232172 if ( resolved !== resolvedEntry ) {
233173 continue ;
234174 }
0 commit comments