File tree 1 file changed +29
-2
lines changed
1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -900,13 +900,40 @@ async function bundleConfigFile(
900
900
{
901
901
name : 'externalize-deps' ,
902
902
setup ( build ) {
903
- build . onResolve ( { filter : / .* / } , ( args ) => {
904
- const id = args . path
903
+ build . onResolve ( { filter : / .* / } , ( { path : id , importer } ) => {
904
+ // externalize bare imports
905
905
if ( id [ 0 ] !== '.' && ! path . isAbsolute ( id ) ) {
906
906
return {
907
907
external : true
908
908
}
909
909
}
910
+ // bundle the rest and make sure that the we can also access
911
+ // it's third-party dependencies. externalize if not.
912
+ // monorepo/
913
+ // ├─ package.json
914
+ // ├─ utils.js -----------> bundle (share same node_modules)
915
+ // ├─ vite-project/
916
+ // │ ├─ vite.config.js --> entry
917
+ // │ ├─ package.json
918
+ // ├─ foo-project/
919
+ // │ ├─ utils.js --------> external (has own node_modules)
920
+ // │ ├─ package.json
921
+ const idFsPath = path . resolve ( path . dirname ( importer ) , id )
922
+ const idPkgPath = lookupFile ( idFsPath , [ `package.json` ] , {
923
+ pathOnly : true
924
+ } )
925
+ if ( idPkgPath ) {
926
+ const idPkgDir = path . dirname ( idPkgPath )
927
+ // if this file needs to go up one or more directory to reach the vite config,
928
+ // that means it has it's own node_modules (e.g. foo-project)
929
+ if ( path . relative ( idPkgDir , fileName ) . startsWith ( '..' ) ) {
930
+ return {
931
+ // normalize actual import after bundled as a single vite config
932
+ path : idFsPath ,
933
+ external : true
934
+ }
935
+ }
936
+ }
910
937
} )
911
938
}
912
939
} ,
You can’t perform that action at this time.
0 commit comments