Skip to content

Commit 19209fe

Browse files
authored
Merge pull request #52 from electron-vite/v0.13.11
V0.13.11
2 parents b6951d2 + fc0dc8f commit 19209fe

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.13.11 (2023-03-27)
2+
3+
- b0312e9 fix: use `__cjs_require` avoid esbuild parse `require`
4+
- dde27b7 fix: remove esbuild plugin #46
5+
16
## 0.13.10 (2023-03-26)
27

38
- 8d2d914 fix: use `__cjs_require` avoid esbuild parse

install.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export const builtins_dir = path.join(__dirname, 'builtins')
1111
export const electron = `
1212
const electron = typeof require !== 'undefined'
1313
// All exports module see https://www.electronjs.org -> API -> Renderer Process Modules
14-
? require("electron")
14+
? (function requireElectron() {
15+
const __cjs_require = require;
16+
return __cjs_require("electron");
17+
}())
1518
: (function nodeIntegrationWarn() {
1619
console.error(\`If you need to use "electron" in the Renderer process, make sure that "nodeIntegration" is enabled in the Main process.\`);
1720
return {
@@ -84,7 +87,9 @@ export async function generateBuiltins() {
8487
// ❯ node:trace_events:25:9
8588

8689
const { exports } = libEsm({ exports: Object.keys(await import(module)) })
87-
fs.writeFileSync(filename, `const _M_ = require("${module}");\n${exports}`)
90+
// Use "__cjs_require" avoid esbuild parse "require"
91+
// TODO: better implements
92+
fs.writeFileSync(filename, `const __cjs_require = require; const _M_ = __cjs_require("${module}");\n${exports}`)
8893
}
8994

9095
// Electron

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-electron-renderer",
3-
"version": "0.13.10",
3+
"version": "0.13.11",
44
"description": "Support use Node.js API in Electron-Renderer",
55
"main": "index.js",
66
"types": "types",

src/index.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import type {
99
UserConfig,
1010
} from 'vite'
1111
import type { RollupOptions } from 'rollup'
12-
import type { Plugin as EsbuildPlugin } from 'esbuild'
1312
import libEsm from 'lib-esm'
1413
import cjsShim from './cjs-shim'
1514

1615
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1716
const builtins = builtinModules.filter(m => !m.startsWith('_')); builtins.push(...builtins.map(m => `node:${m}`))
1817
const electronBuiltins = ['electron', ...builtins]
18+
const BUILTIN_PATH = 'vite-plugin-electron-renderer/builtins'
19+
const RESOLVE_PATH = 'vite-plugin-electron-renderer/.resolve'
1920

2021
export 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-
205176
function ensureDir(dirname: string) {
206177
if (!fs.existsSync(dirname)) {
207178
fs.mkdirSync(dirname, { recursive: true })

0 commit comments

Comments
 (0)