@@ -61,6 +61,10 @@ module.exports = (env, argv) => {
6161 const workspaceProxy = workspaceYoRcFile . devServerProxy || { } ;
6262 const workspaceRepos = isSingleRepoMode ? [ './' ] : workspaceYoRcFile . frontendRepos || [ ] ;
6363 const workspaceMaxChunkSize = workspaceYoRcFile . maxChunkSize || 5000000 ;
64+ const resolveAliases = Object . fromEntries ( Object . entries ( workspaceYoRcFile . resolveAliases || { } ) . map ( ( [ key , p ] ) => [ key , path . join ( workspacePath , p ) ] ) ) ;
65+ // Use a regex with capturing group as explained in https://github.com/webpack/webpack/pull/14509#issuecomment-1237348087.
66+ const customResolveAliasRegex = Object . entries ( resolveAliases ) . length > 0 ? new RegExp ( `/^(.+?[\\/]node_modules[\\/](?!(${ Object . keys ( resolveAliases ) . join ( '|' ) } ))(@.+?[\\/])?.+?)[\\/]/` ) : null ;
67+ Object . entries ( resolveAliases ) . forEach ( ( [ key , p ] ) => console . log ( `Using custom resolve alias: ${ key } -> ${ p } ` ) ) ;
6468
6569 const workspaceRepoToName = Object . fromEntries ( workspaceRepos . map ( ( r ) => [ r , require ( path . join ( workspacePath , r , 'package.json' ) ) . name ] ) ) ;
6670
@@ -363,14 +367,24 @@ module.exports = (env, argv) => {
363367 // new CssMinimizerPlugin(),
364368 ] ,
365369 } ,
370+ snapshot : {
371+ managedPaths : customResolveAliasRegex ? [
372+ customResolveAliasRegex ,
373+ ] : undefined ,
374+ } ,
366375 resolve : {
367376 extensions : [ '.tsx' , '.ts' , '.js' ] ,
377+ // By default, always search for modules in the relative node_modules. However,
378+ // if the package can not be found, fall back to the workspace node_modules. This is
379+ // useful when using the resolveAliases to resolve a package to somewhere else.
380+ modules : [ 'node_modules' , path . join ( workspacePath , 'node_modules' ) ] ,
368381 alias : Object . assign (
369382 {
370383 // Alias to jsx-runtime required as only React@18 has this export, otherwise it fails with "The request 'react/jsx-runtime' failed to resolve only because it was resolved as fully specified".
371384 // See https://github.com/facebook/react/issues/20235 for details.
372385 'react/jsx-runtime' : 'react/jsx-runtime.js' ,
373386 'react/jsx-dev-runtime' : 'react/jsx-dev-runtime.js' ,
387+ ...resolveAliases ,
374388 } ,
375389 // Add aliases for all the workspace repos
376390 ...( ! isSingleRepoMode
@@ -399,7 +413,7 @@ module.exports = (env, argv) => {
399413 // Handle node_modules packages that contain sourcemaps
400414 shouldUseSourceMap && {
401415 enforce : 'pre' ,
402- exclude : / @ b a b e l (?: \/ | \\ { 1 , 2 } ) r u n t i m e / ,
416+ exclude : [ / @ b a b e l (?: \/ | \\ { 1 , 2 } ) r u n t i m e / , customResolveAliasRegex ] . filter ( Boolean ) ,
403417 test : / \. ( j s | m j s | j s x | t s | t s x | c s s ) $ / ,
404418 loader : require . resolve ( 'source-map-loader' ) ,
405419 } ,
@@ -445,7 +459,7 @@ module.exports = (env, argv) => {
445459 // The preset includes JSX, Flow, TypeScript, and some ESnext features.
446460 {
447461 test : / \. ( j s | m j s | j s x | t s | t s x ) $ / ,
448- exclude : / n o d e _ m o d u l e s / ,
462+ exclude : [ / n o d e _ m o d u l e s / , customResolveAliasRegex ] . filter ( Boolean ) ,
449463 loader : 'babel-loader' ,
450464 options : {
451465 presets : [ [ '@babel/preset-env' , { targets : { browsers : 'last 2 Chrome versions' } } ] , '@babel/preset-typescript' , '@babel/preset-react' ] ,
@@ -689,6 +703,7 @@ module.exports = (env, argv) => {
689703 incremental : true ,
690704 paths : Object . assign (
691705 { } ,
706+ resolveAliases ,
692707 ...( ! isSingleRepoMode
693708 ? workspaceRepos . map ( ( r ) => ( {
694709 [ `${ workspaceRepoToName [ r ] } /dist` ] : [ path . join ( workspacePath , r , 'src/*' ) ] ,
0 commit comments