@@ -96,25 +96,6 @@ 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- this . #_helpers. copy . helpers . 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- } ,
11899 exclude : [ '.ts' ] ,
119100 folderContent : ( from , to , exclude ) => {
120101 if ( ! fs . existsSync ( to ) ) { fs . mkdirSync ( to , { recursive : true } ) }
@@ -144,7 +125,8 @@ class Main {
144125 if ( this . #_config. esmDir ) {
145126 this . #_helpers. print ( 'Copying files to ESM...' ) ;
146127 this . #_helpers. copy . directory ( from , path . join ( constants . ROOT , constants . ESM_REL_PATH ) , exclude ) ;
147- this . #_helpers. copy . helpers . addJsonImportAssertions ( this . #_config. esmDir ) ;
128+ this . #_helpers. addJsonImportAssertions ( this . #_config. esmDir ) ;
129+ this . #_helpers. convertDirToImport ( this . #_config. esmDir ) ;
148130 }
149131
150132 if ( this . #_config. cjsDir ) {
@@ -247,19 +229,53 @@ class Main {
247229 throw new Error ( `The ${ constants . PACKAGE_NAME } 's config file is not a valid JSON file.` )
248230 }
249231 }
232+ } ,
233+ addJsonImportAssertions : ( dir ) => {
234+ this . #_helpers. print ( 'Add JSON import assertion' ) ;
235+ fs . readdirSync ( dir ) . forEach ( file => {
236+ const fullPath = path . join ( dir , file ) ;
237+ if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
238+ this . #_helpers. addJsonImportAssertions ( fullPath ) ;
239+ } else if ( file . endsWith ( '.js' ) || file . endsWith ( '.mjs' ) ) {
240+ let content = fs . readFileSync ( fullPath , 'utf8' ) ;
241+ 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 ) => {
242+ if ( ! imports . includes ( 'assert' ) ) {
243+ return `import ${ imports } from ${ quote } ${ modulePath } .json${ quote } assert { type: "json" };` ;
244+ }
245+ return match ;
246+ } ) ;
247+ fs . writeFileSync ( fullPath , content , 'utf8' ) ;
248+ }
249+ } ) ;
250+ } ,
251+ /**This method converts `_dirname = __dirname` to `_dirname = import.meta.dirname` */
252+ convertDirToImport : ( dir ) => {
253+ fs . readdirSync ( dir ) . forEach ( file => {
254+ const fullPath = path . join ( dir , file ) ;
255+ if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
256+ this . #_helpers. convertDirToImport ( fullPath ) ;
257+ } else if ( file . endsWith ( '.js' ) ) {
258+ let content = fs . readFileSync ( fullPath , 'utf8' ) ;
259+
260+ // Replace __dirname with import.meta.dirname
261+ content = content . replace ( / _ _ d i r n a m e / g, 'import.meta.dirname' ) ;
262+
263+ fs . writeFileSync ( fullPath , content , 'utf8' ) ;
264+ }
265+ } ) ;
250266 }
251267 }
252268
253269 run ( ) {
254- const st = performance . now ( ) ;
270+ const st = Date . now ( ) ;
255271
256272 this . #_helpers. config . check ( ) ;
257273 this . #_helpers. read ( ) ;
258274 this . #_helpers. create . packages ( ) ;
259275 this . #_helpers. copy . run ( ) ;
260276 this . #_helpers. extensions . run ( ) ;
261277
262- const et = performance . now ( ) ;
278+ const et = Date . now ( ) ;
263279 const duration = et - st ;
264280 this . #_helpers. print ( `PostBuild finishes in ${ duration } milliseconds` ) ;
265281 }
0 commit comments