1+ import {
2+ ensureExternalScriptTag ,
3+ readExternalScriptAttributes ,
4+ type ExternalScriptAttributes ,
5+ } from "./externalScripts" ;
16import { markFlattenedInnerRoot } from "../runtime/flattenedRoot" ;
27export { FLATTENED_INNER_ROOT_STRIP_ATTRS } from "../runtime/flattenedRoot" ;
38import { parseHostVariableValues , warnUnknownEnumValues } from "../runtime/getVariables" ;
@@ -722,31 +727,55 @@ export interface BundleOptions {
722727 * - Inlines small textual assets as data URLs
723728 */
724729
725- function ensureExternalScriptTag ( doc : Document , src : string ) : void {
726- if ( queryByAttr ( doc , "src" , src , "script" ) ) return ;
727- const el = doc . createElement ( "script" ) ;
728- el . setAttribute ( "src" , src ) ;
729- doc . body . appendChild ( el ) ;
730+ type DeferredScriptChunk = string | ( ( ) => string ) ;
731+
732+ function preserveLocalScriptIntegrity (
733+ doc : Document ,
734+ src : string ,
735+ resolvePath : ( src : string ) => string | null ,
736+ ) : boolean {
737+ const path = resolvePath ( src ) ;
738+ if ( ! path ) return false ;
739+ const pinned = [ ...doc . querySelectorAll ( "script[src][integrity]" ) ] . filter ( ( el ) => {
740+ const candidate = el . getAttribute ( "src" ) || "" ;
741+ return (
742+ isRelativeUrl ( candidate ) &&
743+ resolvePath ( candidate ) === path &&
744+ el . getAttribute ( "integrity" ) ?. trim ( )
745+ ) ;
746+ } ) ;
747+ for ( const el of pinned ) ensureExternalScriptTag ( doc , src , readExternalScriptAttributes ( el ) ) ;
748+ return pinned . length > 0 ;
730749}
731750
732751function hoistExternalScript (
733752 src : string ,
734753 projectDir : string ,
735754 doc : Document ,
736755 seenSrcs : Set < string > ,
737- chunks : string [ ] ,
756+ chunks : DeferredScriptChunk [ ] ,
757+ attributes : ExternalScriptAttributes ,
738758) : void {
759+ if ( attributes . integrity ?. trim ( ) ) {
760+ ensureExternalScriptTag ( doc , src , attributes ) ;
761+ seenSrcs . add ( src ) ;
762+ return ;
763+ }
739764 if ( seenSrcs . has ( src ) ) return ;
740765 seenSrcs . add ( src ) ;
741766 if ( ! isNonRelativeUrl ( src ) && ! isAbsolute ( src ) ) {
742767 const jsPath = resolveWithinProject ( projectDir , src ) ;
743768 const js = jsPath ? safeReadFile ( jsPath ) : null ;
744769 if ( js != null ) {
745- chunks . push ( js ) ;
770+ chunks . push ( ( ) =>
771+ preserveLocalScriptIntegrity ( doc , src , ( value ) => resolveWithinProject ( projectDir , value ) )
772+ ? ""
773+ : js ,
774+ ) ;
746775 return ;
747776 }
748777 }
749- ensureExternalScriptTag ( doc , src ) ;
778+ ensureExternalScriptTag ( doc , src , attributes ) ;
750779}
751780
752781function hoistCompositionScripts (
@@ -759,7 +788,7 @@ function hoistCompositionScripts(
759788 runtimeCompId : string | undefined ;
760789 authoredRootId : string | undefined ;
761790 seenCompScriptSrcs : Set < string > ;
762- compScriptChunks : string [ ] ;
791+ compScriptChunks : DeferredScriptChunk [ ] ;
763792 } ,
764793) : void {
765794 for ( const scriptEl of [ ...container . querySelectorAll ( "script" ) ] ) {
@@ -771,6 +800,7 @@ function hoistCompositionScripts(
771800 opts . document ,
772801 opts . seenCompScriptSrcs ,
773802 opts . compScriptChunks ,
803+ readExternalScriptAttributes ( scriptEl ) ,
774804 ) ;
775805 } else {
776806 opts . compScriptChunks . push (
@@ -857,42 +887,6 @@ export async function bundleToSingleHtml(
857887 }
858888 }
859889
860- // Inline local JS
861- const localJsChunks : string [ ] = [ ] ;
862- let jsAnchorPlaced = false ;
863- for ( const el of [ ...document . querySelectorAll ( "script[src]" ) ] ) {
864- const src = el . getAttribute ( "src" ) ;
865- if ( ! src || ! isRelativeUrl ( src ) ) continue ;
866- // Module scripts can contain static imports whose resolution is relative
867- // to the script URL. Folding their source into a classic inline script
868- // both drops module semantics and changes the import base URL.
869- if ( ( el . getAttribute ( "type" ) || "" ) . trim ( ) . toLowerCase ( ) === "module" ) continue ;
870- const jsPath = resolveEntryPath ( src ) ;
871- const js = jsPath ? safeReadFile ( jsPath ) : null ;
872- if ( js == null ) continue ;
873- localJsChunks . push ( js ) ;
874- if ( ! jsAnchorPlaced ) {
875- const anchor = document . createElement ( "script" ) ;
876- anchor . setAttribute ( "data-hf-bundled-local-js" , "1" ) ;
877- el . replaceWith ( anchor ) ;
878- jsAnchorPlaced = true ;
879- } else {
880- el . remove ( ) ;
881- }
882- }
883- if ( localJsChunks . length > 0 ) {
884- const anchor = document . querySelector ( 'script[data-hf-bundled-local-js="1"]' ) ;
885- const joinedJs = joinJsChunks ( localJsChunks ) ;
886- if ( anchor ) {
887- anchor . removeAttribute ( "data-hf-bundled-local-js" ) ;
888- anchor . textContent = joinedJs ;
889- } else {
890- const script = document . createElement ( "script" ) ;
891- script . textContent = joinedJs ;
892- document . body . appendChild ( script ) ;
893- }
894- }
895-
896890 // Inline sub-compositions (via shared function)
897891 const trackedCompositionHosts = getBundledTrackedCompositionHosts ( document ) ;
898892 const hostIdentityByElement = assignBundledRuntimeCompositionIds ( trackedCompositionHosts ) ;
@@ -927,7 +921,7 @@ export async function bundleToSingleHtml(
927921 } ,
928922 } ) ;
929923 const compStyleChunks : string [ ] = [ ...subCompResult . styles ] ;
930- const compScriptChunks : string [ ] = [ ] ;
924+ const compScriptChunks : DeferredScriptChunk [ ] = [ ] ;
931925 const compExternalLinks = [ ...subCompResult . externalLinks ] ;
932926 const compVariablesByComp : Record < string , Record < string , unknown > > = {
933927 ...subCompResult . variablesByComp ,
@@ -939,21 +933,24 @@ export async function bundleToSingleHtml(
939933 continue ;
940934 }
941935 const extSrc = scriptItem . src ;
936+ if ( scriptItem . integrity ?. trim ( ) ) {
937+ ensureExternalScriptTag ( document , extSrc , scriptItem ) ;
938+ seenCompScriptSrcs . add ( extSrc ) ;
939+ continue ;
940+ }
942941 if ( seenCompScriptSrcs . has ( extSrc ) ) continue ;
943942 seenCompScriptSrcs . add ( extSrc ) ;
944943 if ( isRelativeUrl ( extSrc ) ) {
945944 const jsPath = resolveEntryPath ( extSrc ) ;
946945 const js = jsPath ? safeReadFile ( jsPath ) : null ;
947946 if ( js != null ) {
948- compScriptChunks . push ( js ) ;
947+ compScriptChunks . push ( ( ) =>
948+ preserveLocalScriptIntegrity ( document , extSrc , resolveEntryPath ) ? "" : js ,
949+ ) ;
949950 continue ;
950951 }
951952 }
952- if ( ! queryByAttr ( document , "src" , extSrc , "script" ) ) {
953- const extScript = document . createElement ( "script" ) ;
954- extScript . setAttribute ( "src" , extSrc ) ;
955- document . body . appendChild ( extScript ) ;
956- }
953+ ensureExternalScriptTag ( document , extSrc , scriptItem ) ;
957954 }
958955
959956 // Inline template compositions: inject <template id="X-template"> content into
@@ -1068,6 +1065,43 @@ export async function bundleToSingleHtml(
10681065 templateEl . remove ( ) ;
10691066 }
10701067
1068+ // Inline local JS
1069+ const localJsChunks : string [ ] = [ ] ;
1070+ let jsAnchorPlaced = false ;
1071+ for ( const el of [ ...document . querySelectorAll ( "script[src]" ) ] ) {
1072+ const src = el . getAttribute ( "src" ) ;
1073+ if ( ! src || ! isRelativeUrl ( src ) ) continue ;
1074+ if ( preserveLocalScriptIntegrity ( document , src , resolveEntryPath ) ) continue ;
1075+ // Module scripts can contain static imports whose resolution is relative
1076+ // to the script URL. Folding their source into a classic inline script
1077+ // both drops module semantics and changes the import base URL.
1078+ if ( ( el . getAttribute ( "type" ) || "" ) . trim ( ) . toLowerCase ( ) === "module" ) continue ;
1079+ const jsPath = resolveEntryPath ( src ) ;
1080+ const js = jsPath ? safeReadFile ( jsPath ) : null ;
1081+ if ( js == null ) continue ;
1082+ localJsChunks . push ( js ) ;
1083+ if ( ! jsAnchorPlaced ) {
1084+ const anchor = document . createElement ( "script" ) ;
1085+ anchor . setAttribute ( "data-hf-bundled-local-js" , "1" ) ;
1086+ el . replaceWith ( anchor ) ;
1087+ jsAnchorPlaced = true ;
1088+ } else {
1089+ el . remove ( ) ;
1090+ }
1091+ }
1092+ if ( localJsChunks . length > 0 ) {
1093+ const anchor = document . querySelector ( 'script[data-hf-bundled-local-js="1"]' ) ;
1094+ const joinedJs = joinJsChunks ( localJsChunks ) ;
1095+ if ( anchor ) {
1096+ anchor . removeAttribute ( "data-hf-bundled-local-js" ) ;
1097+ anchor . textContent = joinedJs ;
1098+ } else {
1099+ const script = document . createElement ( "script" ) ;
1100+ script . textContent = joinedJs ;
1101+ document . body . appendChild ( script ) ;
1102+ }
1103+ }
1104+
10711105 // Inject external scripts from sub-compositions (e.g., Lottie CDN)
10721106 // that aren't already present in the main document.
10731107 for ( const link of compExternalLinks ) {
@@ -1092,7 +1126,9 @@ export async function bundleToSingleHtml(
10921126 }
10931127 if ( compScriptChunks . length ) {
10941128 const compScript = document . createElement ( "script" ) ;
1095- compScript . textContent = joinJsChunks ( compScriptChunks ) ;
1129+ compScript . textContent = joinJsChunks (
1130+ compScriptChunks . map ( ( chunk ) => ( typeof chunk === "string" ? chunk : chunk ( ) ) ) ,
1131+ ) ;
10961132 document . body . appendChild ( compScript ) ;
10971133 }
10981134
0 commit comments