@@ -96,6 +96,25 @@ class Main {
9696 }
9797 } ,
9898 copy : {
99+ helpers : {
100+ addJsonImportAssertions ( dir ) {
101+ fs . readdirSync ( dir ) . forEach ( file => {
102+ const fullPath = path . join ( dir , file ) ;
103+ if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
104+ addJsonImportAssertions ( fullPath ) ;
105+ } else if ( file . endsWith ( '.js' ) || file . endsWith ( '.mjs' ) ) {
106+ let content = fs . readFileSync ( fullPath , 'utf8' ) ;
107+ content = content . replace ( / i m p o r t \s + ( .* ?) \s + f r o m \s + ( [ ' " ] ) ( .* ?) \. j s o n \2\s * ; / g, ( match , imports , quote , modulePath ) => {
108+ if ( ! imports . includes ( 'assert' ) ) {
109+ return `import ${ imports } from ${ quote } ${ modulePath } .json${ quote } assert { type: "json" };` ;
110+ }
111+ return match ;
112+ } ) ;
113+ fs . writeFileSync ( fullPath , content , 'utf8' ) ;
114+ }
115+ } ) ;
116+ }
117+ } ,
99118 exclude : [ '.ts' ] ,
100119 folderContent : ( from , to , exclude ) => {
101120 if ( ! fs . existsSync ( to ) ) { fs . mkdirSync ( to , { recursive : true } ) }
@@ -125,6 +144,7 @@ class Main {
125144 if ( this . #_config. esmDir ) {
126145 this . #_helpers. print ( 'Copying files to ESM...' ) ;
127146 this . #_helpers. copy . directory ( from , path . join ( constants . ROOT , constants . ESM_REL_PATH ) , exclude ) ;
147+ this . #_helpers. copy . helpers . addJsonImportAssertions ( this . #_config. esmDir ) ;
128148 }
129149
130150 if ( this . #_config. cjsDir ) {
0 commit comments