1
- import { resolve , dirname } from 'path' ;
1
+ import { resolve , extname , dirname } from 'path' ;
2
2
import { fileURLToPath } from 'url'
3
3
4
4
import pkg from './package.json' with { type : 'json' }
@@ -15,6 +15,25 @@ import { babel } from '@rollup/plugin-babel'
15
15
16
16
const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
17
17
18
+ function renameJsToCjs ( ) {
19
+ return {
20
+ name : 'rename-js-to-cjs' ,
21
+ generateBundle ( options , bundle ) {
22
+ for ( const [ fileName , file ] of Object . entries ( bundle ) ) {
23
+ if ( file . type === 'chunk' && extname ( fileName ) === '.js' ) {
24
+ const newFileName = fileName . replace ( / \. j s $ / , '.cjs' ) ;
25
+
26
+ // Emit new file with updated name and same code
27
+ this . emitFile ( { type : 'asset' , fileName : newFileName , source : file . code } ) ;
28
+
29
+ // Remove old .js entry
30
+ delete bundle [ fileName ] ;
31
+ }
32
+ }
33
+ } ,
34
+ } ;
35
+ }
36
+
18
37
const baseConfig = {
19
38
input : 'dist/esm-raw/index.js' ,
20
39
output : {
@@ -67,7 +86,8 @@ const cjsConfig = {
67
86
preventAssignment : true ,
68
87
'process.env.NODE_ENV' : JSON . stringify ( process . env . NODE_ENV ) ,
69
88
__VERSION__ : JSON . stringify ( pkg . version ) ,
70
- } )
89
+ } ) ,
90
+ renameJsToCjs ( )
71
91
] ,
72
92
}
73
93
0 commit comments