@@ -70,7 +70,7 @@ function getNPMBundles(): BuildOptions[] {
7070 {
7171 ...getNPMConfig ( { dev : false } ) ,
7272 entryPoints : getBrowserInputs ( ) ,
73- plugins,
73+ plugins : [ ... plugins , sideEffects ( ) ] ,
7474 } ,
7575 // server
7676 {
@@ -324,6 +324,50 @@ function copyAssets(): Plugin {
324324 } ;
325325}
326326
327+ function sideEffects ( ) : Plugin {
328+ return {
329+ name : 'side-effects' ,
330+ async setup ( build ) {
331+ build . onEnd ( async ( ) => {
332+ const { outdir } = build . initialOptions ;
333+ if ( ! outdir ) return ;
334+
335+ const dir = outdir . replace ( / ^ .* ?\/ / , '' ) ,
336+ files = await fs . readdir ( `${ outdir } /define` ) ,
337+ sideEffects = new Set < string > ( ) ;
338+
339+ await Promise . all (
340+ files . map ( async ( file ) => {
341+ const filePath = `${ outdir } /define/${ file } ` ,
342+ isDirectory = ( await fs . stat ( filePath ) ) . isDirectory ( ) ;
343+
344+ if ( isDirectory ) return ;
345+
346+ const code = await fs . readFile ( filePath , 'utf-8' ) ;
347+
348+ // If there is a side effect chunk created by esbuild, it'll generally be the first import.
349+ const end = code . indexOf ( ';' ) ,
350+ chunk = code . slice ( 'import"../chunks/' . length , end - 1 ) ;
351+
352+ if ( chunk . startsWith ( 'vidstack' ) && chunk . endsWith ( '.js' ) ) {
353+ sideEffects . add ( `./${ dir } /chunks/${ chunk } ` ) ;
354+ }
355+ } ) ,
356+ ) ;
357+
358+ const pkgPath = 'dist-npm/package.json' ,
359+ pkg = JSON . parse ( await fs . readFile ( pkgPath , 'utf-8' ) ) ;
360+
361+ for ( const chunk of sideEffects ) {
362+ pkg . sideEffects . push ( chunk ) ;
363+ }
364+
365+ await fs . writeFile ( pkgPath , JSON . stringify ( pkg , null , 2 ) , 'utf-8' ) ;
366+ } ) ;
367+ } ,
368+ } ;
369+ }
370+
327371// This plugin rewrites chunk paths so our URL rewrites to jsDelivr work.
328372function rewriteCDNChunks ( ) : Plugin {
329373 return {
0 commit comments