@@ -466,17 +466,22 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
466
466
inlineModuleIndex ++
467
467
if ( url && ! isExcludedUrl ( url ) && ! isPublicFile ) {
468
468
setModuleSideEffectPromises . push (
469
- this . resolve ( url , id )
470
- . then ( ( resolved ) => {
471
- if ( ! resolved ) {
472
- return Promise . reject ( )
473
- }
474
- return this . load ( resolved )
475
- } )
476
- . then ( ( mod ) => {
477
- // set this to keep the module even if `treeshake.moduleSideEffects=false` is set
478
- mod . moduleSideEffects = true
479
- } ) ,
469
+ this . resolve ( url , id ) . then ( ( resolved ) => {
470
+ if ( ! resolved ) {
471
+ return Promise . reject (
472
+ new Error ( `Failed to resolve ${ url } from ${ id } ` ) ,
473
+ )
474
+ }
475
+ // set moduleSideEffects to keep the module even if `treeshake.moduleSideEffects=false` is set
476
+ const moduleInfo = this . getModuleInfo ( resolved . id )
477
+ if ( moduleInfo ) {
478
+ moduleInfo . moduleSideEffects = true
479
+ } else if ( ! resolved . external ) {
480
+ return this . load ( resolved ) . then ( ( mod ) => {
481
+ mod . moduleSideEffects = true
482
+ } )
483
+ }
484
+ } ) ,
480
485
)
481
486
// <script type="module" src="..."/>
482
487
// add it as an import
@@ -715,23 +720,28 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
715
720
const getImportedChunks = (
716
721
chunk : OutputChunk ,
717
722
seen : Set < string > = new Set ( ) ,
718
- ) : OutputChunk [ ] => {
719
- const chunks : OutputChunk [ ] = [ ]
723
+ ) : ( OutputChunk | string ) [ ] => {
724
+ const chunks : ( OutputChunk | string ) [ ] = [ ]
720
725
chunk . imports . forEach ( ( file ) => {
721
726
const importee = bundle [ file ]
722
- if ( importee ?. type === 'chunk' && ! seen . has ( file ) ) {
723
- seen . add ( file )
727
+ if ( importee ) {
728
+ if ( importee . type === 'chunk' && ! seen . has ( file ) ) {
729
+ seen . add ( file )
724
730
725
- // post-order traversal
726
- chunks . push ( ...getImportedChunks ( importee , seen ) )
727
- chunks . push ( importee )
731
+ // post-order traversal
732
+ chunks . push ( ...getImportedChunks ( importee , seen ) )
733
+ chunks . push ( importee )
734
+ }
735
+ } else {
736
+ // external imports
737
+ chunks . push ( file )
728
738
}
729
739
} )
730
740
return chunks
731
741
}
732
742
733
743
const toScriptTag = (
734
- chunk : OutputChunk ,
744
+ chunkOrUrl : OutputChunk | string ,
735
745
toOutputPath : ( filename : string ) => string ,
736
746
isAsync : boolean ,
737
747
) : HtmlTagDescriptor => ( {
@@ -746,7 +756,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
746
756
// https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules:~:text=For%20%3Cscript%3E,of%20other%20modules.
747
757
// Now `<script type="module">` uses `same origin`: https://github.com/whatwg/html/pull/3656#:~:text=Module%20scripts%20are%20always%20fetched%20with%20credentials%20mode%20%22same%2Dorigin%22%20by%20default%20and%20can%20no%20longer%0Ause%20%22omit%22
748
758
crossorigin : true ,
749
- src : toOutputPath ( chunk . fileName ) ,
759
+ src :
760
+ typeof chunkOrUrl === 'string'
761
+ ? chunkOrUrl
762
+ : toOutputPath ( chunkOrUrl . fileName ) ,
750
763
} ,
751
764
} )
752
765
@@ -863,7 +876,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
863
876
const resolveDependencies =
864
877
typeof modulePreload === 'object' &&
865
878
modulePreload . resolveDependencies
866
- const importsFileNames = imports . map ( ( chunk ) => chunk . fileName )
879
+ const importsFileNames = imports
880
+ . filter ( ( chunkOrUrl ) => typeof chunkOrUrl !== 'string' )
881
+ . map ( ( chunk ) => chunk . fileName )
867
882
const resolvedDeps = resolveDependencies
868
883
? resolveDependencies ( chunk . fileName , importsFileNames , {
869
884
hostId : relativeUrlPath ,
0 commit comments