@@ -10,7 +10,7 @@ import _esbuild from 'rollup-plugin-esbuild'
10
10
import dts from 'rollup-plugin-dts'
11
11
import replace from '@rollup/plugin-replace'
12
12
import { relative , resolve , dirname } from 'pathe'
13
- import { resolvePath } from 'mlly'
13
+ import { resolvePath , resolveModuleExportNames } from 'mlly'
14
14
import { getpkg , tryResolve , warn } from '../utils'
15
15
import type { BuildContext } from '../types'
16
16
import { JSONPlugin } from './plugins/json'
@@ -33,12 +33,19 @@ export async function rollupBuild (ctx: BuildContext) {
33
33
const shebang = getShebang ( code )
34
34
35
35
await mkdir ( dirname ( output ) , { recursive : true } )
36
+
37
+ // CJS Stub
36
38
if ( ctx . options . rollup . emitCJS ) {
37
- await writeFile ( output + '.cjs' , `${ shebang } module.exports = require(${ JSON . stringify ( jitiPath ) } )(null, { interopDefault: true, esmResolve: true })('${ entry . input } ')` )
39
+ await writeFile ( output + '.cjs' , `${ shebang } module.exports = require(${ JSON . stringify ( jitiPath ) } )(null, { interopDefault: true, esmResolve: true })('${ resolvedEntry } ')` )
38
40
}
39
- // Use file:// protocol for windows compatibility
40
- await writeFile ( output + '.mjs' , `${ shebang } import jiti from ${ JSON . stringify ( pathToFileURL ( jitiPath ) . href ) } ;\nexport default jiti(null, { interopDefault: true, esmResolve: true })('${ entry . input } ');` )
41
- await writeFile ( output + '.d.ts' , `export * from '${ entry . input } ';\nexport { default } from '${ entry . input } ';` )
41
+
42
+ // MJS Stub
43
+ // Try to analyze exports
44
+ const namedExports = await resolveModuleExportNames ( resolvedEntry )
45
+ await writeFile ( output + '.mjs' , `${ shebang } import jiti from ${ JSON . stringify ( pathToFileURL ( jitiPath ) . href ) } ;\nconst _module = jiti(null, { interopDefault: true, esmResolve: true })('${ resolvedEntry } ');\n\nexport default _module;\n\n${ namedExports . map ( name => `export const ${ name } = _module.${ name } ;` ) . join ( '\n' ) } ` )
46
+
47
+ // DTS Stub
48
+ await writeFile ( output + '.d.ts' , `export * from '${ entry . input } ';\nexport { default } from '${ resolvedEntry } ';` )
42
49
43
50
if ( shebang ) {
44
51
await makeExecutable ( output + '.cjs' )
0 commit comments