@@ -22,12 +22,12 @@ export interface optimizerOptions {
2222 *
2323 * - `false` Vite's default Pre-Bundling will be used.
2424 */
25- resolve ?: ( args : import ( 'esbuild' ) . OnResolveArgs ) => 'commonjs' | 'module' | false | null | undefined | Promise < 'commonjs' | 'module' | false | null | undefined >
25+ resolve ?: ( args : import ( 'esbuild' ) . OnResolveArgs ) => { type : 'commonjs' | 'module' } | false | void | Promise < { type : 'commonjs' | 'module' } | false | void >
2626}
2727
2828export default function optimizer ( options : optimizerOptions , nodeIntegration : boolean ) : VitePlugin {
2929 return {
30- name : 'vite-plugin-electron-renderer:pre-bundle ' ,
30+ name : 'vite-plugin-electron-renderer:optimizer ' ,
3131 config ( config ) {
3232 node_modules_path = find_node_modules ( config . root ? path . resolve ( config . root ) : process . cwd ( ) ) [ 0 ]
3333 cache_dir = path . join ( node_modules_path , CACHE_DIR )
@@ -109,18 +109,41 @@ export function esbuildPlugin(options: optimizerOptions): EsbuildPlugin {
109109 let moduleType : 'commonjs' | 'module' | undefined
110110 const packageJson = path . join ( node_modules_path , id , 'package.json' )
111111 if ( fs . existsSync ( packageJson ) ) {
112- moduleType = cjs_require ( packageJson ) . type === 'module' ? 'module' : 'commonjs'
112+ const pkg = cjs_require ( packageJson )
113+ if ( pkg . type ) {
114+ // { "type": "module" }
115+ moduleType = pkg . type === 'module' ? 'module' : 'commonjs'
116+ } else if ( pkg . module ) {
117+ // { "module": "main.mjs" }
118+ moduleType = 'module'
119+ } else if ( pkg . exports ) {
120+ if ( pkg . exports . import ) {
121+ // { "exports": { "import": "main.mjs" } }
122+ moduleType = 'module'
123+ } else {
124+ for ( const _export of Object . values < Record < string , string > > ( pkg . exports ) ) {
125+ if ( _export . import ) {
126+ // { "exports": { ".": { "import": "main.mjs" } } }
127+ moduleType = 'module'
128+ break
129+ }
130+ }
131+ }
132+ }
113133 }
114134
115135 const userType = await resolve ?.( args )
116136 if ( userType === false ) {
117137 // Use Vite's default Pre-Bundling
118138 return
119139 }
120- if ( userType === 'commonjs' || userType === 'module ' ) {
121- moduleType = userType
140+ if ( userType && typeof userType === 'object ' ) {
141+ moduleType = userType . type
122142 }
123143
144+ // Assign default value
145+ moduleType ??= 'commonjs'
146+
124147 // Only `cjs` modules, especially C/C++ npm-pkg, `es` modules will be use Vite's default Pre-Bundling
125148 if ( moduleType === 'commonjs' ) {
126149 return {
0 commit comments