Skip to content

Commit 6a9b037

Browse files
authored
Merge pull request #62 from electron-vite/v0.14.2
V0.14.2
2 parents 4609d13 + 454ec77 commit 6a9b037

File tree

7 files changed

+31
-26
lines changed

7 files changed

+31
-26
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 0.14.2 (2023-05-05)
2+
3+
- 6930c08 v0.14.2
4+
- d99bec7 feat(build): target `node14`
5+
- af15ae0 fix: correct lookup path for `require()` #63 | closes [#63]https://github.com/electron-vite/vite-plugin-electron-renderer/issues/63
6+
- 69a6a0b chore: cleanup
7+
- ef1f57e docs: update
8+
- dd3e052 chore: cleanup
9+
110
## 0.14.1 (2023-04-15)
211

312
- ffef5f2 chore: bump vite-plugin-utils to 0.4.1

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,27 @@ const { ipcRenderer } = require('electron')
140140
```
141141
-->
142142

143-
<!--
144143
## Dependency Pre-Bundling
145144

146-
**In general**. Vite will pre-bundle all third-party modules in a Web-based usage format, but it can not adapt to Electron Renderer process especially C/C++ modules. So we must be make a little changes for this.
147-
When a module detected as a `cjs` module. it will be pre-bundle like the following.
145+
**In general**. Vite will pre-bundle all third-party modules in a Web-based usage format, but it can not adapt to Electron Renderer process especially C/C++ modules. So we must be make a little changes for this.
146+
147+
<!-- When a module detected as a `cjs` module. it will be pre-bundle like the following. -->
148148

149149
```js
150-
const lib = require("cjs-module");
150+
// 👉 https://github.com/electron-vite/vite-plugin-electron-renderer/blob/v0.13.0/src/optimizer.ts#L139-L142
151+
const _M_ = require("serialport");
151152

152-
export const member = lib.member;
153-
export default (lib.default || lib);
153+
export default (_M_.default || _M_);
154+
export const SerialPort = _M_.SerialPort;
155+
// export other members ...
154156
```
155157

156-
[See source code](https://github.com/electron-vite/vite-plugin-electron-renderer/blob/v0.13.0/src/optimizer.ts#L139-L142)
157-
158+
<!--
158159
**By the way**. If an npm package is a pure ESM format package, and the packages it depends on are also in ESM format, then put it in `optimizeDeps.exclude` and it will work normally.
159160
[See the explanation](https://github.com/electron-vite/vite-plugin-electron-renderer/blob/v0.10.3/examples/quick-start/vite.config.ts#L33-L36)
160161
-->
161162

162-
## `dependencies` vs `devDependencies`
163+
## dependencies vs devDependencies
163164

164165
<table>
165166
<thead>

README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default (lib.default || lib);
6464
[这里解释了它](https://github.com/electron-vite/vite-plugin-electron/blob/14684ba108beec305edf4c9d8865527f6508f987/examples/nodeIntegration/vite.config.ts#L36-L39)
6565
-->
6666

67-
## `dependencies``devDependencies`
67+
## dependencies 与 devDependencies
6868

6969
<table>
7070
<thead>

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-electron-renderer",
3-
"version": "0.14.1",
3+
"version": "0.14.2",
44
"description": "Support use Node.js API in Electron-Renderer",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -37,8 +37,7 @@
3737
"vitest": "^0.29.3"
3838
},
3939
"files": [
40-
"dist",
41-
"install.js"
40+
"dist"
4241
],
4342
"keywords": [
4443
"vite",

src/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,22 @@ export interface RendererOptions {
115115
export default function renderer(options: RendererOptions = {}): VitePlugin {
116116
let root: string
117117
let cacheDir: string
118-
const cwd = process.cwd()
119118
const resolveKeys: string[] = []
120119
const moduleCache = new Map<string, string>()
121120

122121
return {
123122
name: 'vite-plugin-electron-renderer',
124123
async config(config, { command }) {
125124
// https://github.com/vitejs/vite/blob/v4.2.1/packages/vite/src/node/config.ts#L469-L472
126-
root = normalizePath(config.root ? path.resolve(config.root) : cwd)
125+
root = normalizePath(config.root ? path.resolve(config.root) : process.cwd())
127126

128-
cacheDir = path.join(find_node_modules(root)[0] ?? cwd, CACHE_DIR)
127+
cacheDir = path.join(find_node_modules(root)[0] ?? process.cwd(), CACHE_DIR)
129128

130129
for (const [key, option] of Object.entries(options.resolve ?? {})) {
131130
if (command === 'build' && option.type === 'esm') {
132131
// A `esm` module can be build correctly during the `vite build`
133-
continue
132+
// Because the current C/C++ modules are imported through `cjs` format, so exclude `esm`
133+
continue // (🚧-① only `type:cjs`)
134134
}
135135
resolveKeys.push(key)
136136
}
@@ -159,7 +159,7 @@ export default function renderer(options: RendererOptions = {}): VitePlugin {
159159
},
160160
}]
161161

162-
// options.resolve (only `cjs`)
162+
// options.resolve (🚧-① only `type:cjs`)
163163
aliases.push({
164164
find: new RegExp(`^(${resolveKeys.join('|')})$`),
165165
replacement: '$1',
@@ -178,7 +178,6 @@ export default function renderer(options: RendererOptions = {}): VitePlugin {
178178
snippets = await resolved.build({
179179
cjs: module => Promise.resolve(getSnippets({ import: module, export: module })),
180180
esm: (module, buildOptions) => getPreBundleSnippets({
181-
root,
182181
module,
183182
outdir: cacheDir,
184183
buildOptions,
@@ -188,7 +187,6 @@ export default function renderer(options: RendererOptions = {}): VitePlugin {
188187
snippets = getSnippets({ import: source, export: source })
189188
} else if (resolved.type === 'esm') {
190189
snippets = await getPreBundleSnippets({
191-
root,
192190
module: source,
193191
outdir: cacheDir,
194192
})
@@ -318,13 +316,11 @@ function getSnippets(module: {
318316
}
319317

320318
async function getPreBundleSnippets(options: {
321-
root: string
322319
module: string
323320
outdir: string
324321
buildOptions?: esbuild.BuildOptions
325322
}) {
326323
const {
327-
root,
328324
module,
329325
outdir,
330326
buildOptions = {},
@@ -345,9 +341,8 @@ async function getPreBundleSnippets(options: {
345341

346342
return getSnippets({
347343
import: outfile,
348-
// Since any module will be imported as an `import` in the Renderer process,
349-
// the __dirname(import.meta.url) of the module should be http://localhost:5173/ which is the `root` directory
350-
export: relativeify(path.posix.relative(root, outfile)),
344+
// `require()` in script-module lookup path based on `process.cwd()` 🤔
345+
export: relativeify(path.posix.relative(process.cwd(), outfile)),
351346
})
352347
}
353348

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"baseUrl": ".",
1414
"outDir": "types"
1515
},
16-
"include": ["src/**/*.ts"]
16+
"include": ["src"]
1717
}

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineConfig({
1111
build: {
1212
minify: false,
1313
emptyOutDir: !isdev,
14+
target: 'node14',
1415
lib: {
1516
entry: {
1617
index: 'src/index.ts',

0 commit comments

Comments
 (0)