@@ -9,13 +9,14 @@ import type {
99 UserConfig ,
1010} from 'vite'
1111import type { RollupOptions } from 'rollup'
12- import type { Plugin as EsbuildPlugin } from 'esbuild'
1312import libEsm from 'lib-esm'
1413import cjsShim from './cjs-shim'
1514
1615const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
1716const builtins = builtinModules . filter ( m => ! m . startsWith ( '_' ) ) ; builtins . push ( ...builtins . map ( m => `node:${ m } ` ) )
1817const electronBuiltins = [ 'electron' , ...builtins ]
18+ const BUILTIN_PATH = 'vite-plugin-electron-renderer/builtins'
19+ const RESOLVE_PATH = 'vite-plugin-electron-renderer/.resolve'
1920
2021export interface RendererOptions {
2122 /**
@@ -70,22 +71,13 @@ export default function renderer(options: RendererOptions = {}): VitePlugin[] {
7071 // see - https://github.com/rollup/plugins/blob/commonjs-v24.0.0/packages/commonjs/src/helpers.js#L38
7172 withIgnore ( config . build )
7273
73- // -------------------------------------------------
74-
75- config . optimizeDeps ??= { }
76- config . optimizeDeps . esbuildOptions ??= { }
77- config . optimizeDeps . esbuildOptions . plugins ??= [ ]
78- config . optimizeDeps . esbuildOptions . plugins . push ( esbuildPlugin ( ) )
79-
80- // -------------------------------------------------
81-
8274 const resolveAliases = await buildResolve ( options )
8375 const builtinAliases : Alias [ ] = electronBuiltins
8476 . filter ( m => ! m . startsWith ( 'node:' ) )
8577 . map < Alias > ( m => ( {
8678 find : new RegExp ( `^(node:)?${ m } $` ) ,
8779 // Vite's pre-bundle only recognizes bare-import
88- replacement : `vite-plugin-electron-renderer/builtins /${ m } ` ,
80+ replacement : `${ BUILTIN_PATH } /${ m } ` ,
8981 // TODO: must be use absolute path for `pnnpm` monorepo - `shamefully-hoist=true` 🤔
9082 } ) )
9183
@@ -174,34 +166,13 @@ ${exports}
174166
175167 aliases . push ( {
176168 find : name ,
177- replacement : `vite-plugin-electron-renderer/.resolve /${ name } ` ,
169+ replacement : `${ RESOLVE_PATH } /${ name } ` ,
178170 } )
179171 }
180172
181173 return aliases
182174}
183175
184- function esbuildPlugin ( ) : EsbuildPlugin {
185- return {
186- name : 'vite-plugin-target:optimizer:esbuild' ,
187- setup ( build ) {
188- // https://github.com/vitejs/vite/blob/v4.2.0/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L277-L279
189- const escape = ( text : string ) =>
190- `^${ text . replace ( / [ - / \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) } $`
191- const filter = new RegExp ( electronBuiltins . map ( escape ) . join ( '|' ) )
192-
193- // Avoid Vite internal esbuild plugin
194- // https://github.com/vitejs/vite/blob/v4.2.0/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L288
195- build . onResolve ( { filter } , args => {
196- return {
197- path : args . path ,
198- external : true ,
199- }
200- } )
201- } ,
202- }
203- }
204-
205176function ensureDir ( dirname : string ) {
206177 if ( ! fs . existsSync ( dirname ) ) {
207178 fs . mkdirSync ( dirname , { recursive : true } )
0 commit comments