@@ -4,6 +4,8 @@ import gulp from 'gulp';
44import { memoizeTask } from './memoize-task.js' ;
55import { EMPTY as ObservableEmpty , forkJoin as ObservableForkJoin } from 'rxjs' ;
66import gulpJsonTransform from 'gulp-json-transform' ;
7+ import { sync as globSync } from 'glob' ;
8+ import path from 'node:path' ;
79
810export const packageTask = ( ( cache ) => memoizeTask ( cache , function bundle ( target , format ) {
911 if ( target === `src` ) return ObservableEmpty ( ) ;
@@ -20,6 +22,29 @@ export const packageTask = ((cache) => memoizeTask(cache, function bundle(target
2022
2123export default packageTask ;
2224
25+ function createSideEffectsList ( ) {
26+ const patterns = [ 'add/**/*.ts' , 'asynciterable/todomstream.ts' ] ;
27+
28+ const tsFilePaths = globSync ( patterns . map ( ( p ) => `./src/${ p } ` ) ) ;
29+
30+ // Generate .mjs and .js paths for each .ts file
31+ const sideEffectPaths = [ ] ;
32+ for ( const filePath of tsFilePaths ) {
33+ // Get the relative path from srcDir to the file
34+ const relativePath = path . relative ( './src' , filePath ) ;
35+ // Normalize path separators to forward slashes
36+ const normalizedPath = relativePath . replace ( / \\ / g, '/' ) ;
37+ // Remove the .ts extension and add .mjs and .js
38+ const basePath = normalizedPath . replace ( / \. t s $ / , '' ) ;
39+ sideEffectPaths . push ( `./${ basePath } .mjs` ) ;
40+ sideEffectPaths . push ( `./${ basePath } .js` ) ;
41+ }
42+
43+ return sideEffectPaths ;
44+ }
45+
46+ const sideEffects = createSideEffectsList ( ) ;
47+
2348const createMainPackageJson = ( target , format ) => ( orig ) => ( {
2449 ...createTypeScriptPackageJson ( target , format ) ( orig ) ,
2550 bin : orig . bin ,
@@ -48,7 +73,7 @@ const createMainPackageJson = (target, format) => (orig) => ({
4873 } ,
4974 './*' : createDualExport ( '*' ) ,
5075 } ,
51- sideEffects : false ,
76+ sideEffects,
5277 esm : { mode : `all` , sourceMap : true }
5378} ) ;
5479
@@ -60,7 +85,7 @@ const createTypeScriptPackageJson = (target, format) => (orig) => ({
6085 types : `node.ts` ,
6186 browser : `dom.ts` ,
6287 type : 'module' ,
63- sideEffects : false ,
88+ sideEffects,
6489 esm : { mode : `auto` , sourceMap : true } ,
6590 dependencies : {
6691 '@types/node' : '*' ,
@@ -88,7 +113,7 @@ const createScopedPackageJSON = (target, format) => (({ name, ...orig }) =>
88113 // set "module" if building scoped ESM target
89114 module : format === 'esm' || format === 'cls' ? `node.js` : undefined ,
90115 // set "sideEffects" to false as a hint to Webpack that it's safe to tree-shake the ESM target
91- sideEffects : format === 'esm' || format === 'cls' ? false : undefined ,
116+ sideEffects : format === 'esm' || format === 'cls' ? sideEffects : undefined ,
92117 // include "esm" settings for https://www.npmjs.com/package/esm if building scoped ESM target
93118 esm : format === `esm` ? { mode : `auto` , sourceMap : true } : undefined ,
94119 // set "types" to "dom" if building scoped UMD target, otherwise "node"
0 commit comments