Skip to content

Commit 36e085b

Browse files
authored
Merge pull request #45 from electron-vite/v0.13.7
V0.13.7
2 parents 521d8e2 + e369d59 commit 36e085b

File tree

4 files changed

+67
-29
lines changed

4 files changed

+67
-29
lines changed

CHANGELOG.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1+
## 0.13.7 (2023-03-25)
2+
3+
- bda7d87 feat: set electron `__esModule`
4+
- 69960bd refactor: remove `cjs`, `external`. add `freeze`, `ignore`.
5+
- 521d8e2 docs: `resolve.alias` comments
6+
7+
#### Main Changed
8+
9+
1. No longer build in `cjs` format by default with improvements to `esm`.
10+
2. Remove the Node.js built-in module from `external` to be compatible with the `esm` build format.
11+
112
## 0.13.6 (2023-03-24)
213

314
- 562aa20 refactor: explicitly specify the module platform
415

16+
#### Main Changed
17+
18+
Since `0.13.6`, Pre-Bundling have been greatly improved and Pure-JavaScript/Node.js modules no longer require any configuration - **out of the box**.
19+
20+
C/C++ modules, however, still require explicit configuration.
21+
22+
```js
23+
export default {
24+
plugins: [
25+
renderer({
26+
optimizeDeps: {
27+
resolve(args) {
28+
if (args.path === 'serialport') {
29+
return { platform: 'node' } // C/C++ module
30+
}
31+
},
32+
},
33+
}),
34+
],
35+
}
36+
```
37+
538
## 0.13.5 (2023-03-23)
639

740
- efaf706 refactor: `options.optimizer` -> `options.optimizeDeps`

install.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const electron = typeof require !== 'undefined'
1717
return {
1818
// TODO: polyfill
1919
};
20-
}());
20+
}()); Object.defineProperty(electron, '__esModule', { value: true });
2121
2222
// Proxy in Worker
2323
let _ipcRenderer;

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.6",
3+
"version": "0.13.7",
44
"description": "Support use Node.js API in Electron-Renderer",
55
"main": "index.js",
66
"types": "types",

src/build-config.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type {
22
Alias,
3+
BuildOptions,
34
Plugin,
45
UserConfig,
56
} from 'vite'
6-
import type { ExternalOption, RollupOptions } from 'rollup'
7+
import type { RollupOptions } from 'rollup'
78
import { electronBuiltins } from './utils'
89

910
export default function buildConfig(nodeIntegration?: boolean): Plugin[] {
@@ -54,45 +55,49 @@ export default function buildConfig(nodeIntegration?: boolean): Plugin[] {
5455

5556
if (nodeIntegration) {
5657
config.build.rollupOptions ??= {}
57-
config.build.rollupOptions.external = withExternal(config.build.rollupOptions.external)
58-
setOutputFormat(config.build.rollupOptions)
58+
config.build.rollupOptions.output ??= {}
59+
60+
// `fs-extra` will extend the `fs` module
61+
setOutputFreeze(config.build.rollupOptions)
62+
63+
// Some third-party modules, such as `fs-extra`, extend the native module
64+
// `__esModule` to bypass Rollup's `getAugmentedNamespace`
65+
// see - https://github.com/rollup/plugins/blob/commonjs-v24.0.0/packages/commonjs/src/helpers.js#L38
66+
withIgnore(config.build)
5967
}
6068
},
6169
},
6270
]
6371
}
6472

65-
function withExternal(external?: ExternalOption) {
66-
if (
67-
Array.isArray(external) ||
68-
typeof external === 'string' ||
69-
external instanceof RegExp
70-
) {
71-
// @ts-ignore
72-
external = electronBuiltins.concat(external)
73-
} else if (typeof external === 'function') {
74-
const original = external
75-
external = function externalFn(source, importer, isResolved) {
76-
if (electronBuiltins.includes(source)) {
77-
return true
78-
}
79-
return original(source, importer, isResolved)
73+
function setOutputFreeze(rollupOptions: RollupOptions) {
74+
rollupOptions.output ??= {}
75+
if (Array.isArray(rollupOptions.output)) {
76+
for (const o of rollupOptions.output) {
77+
o.freeze ??= false
8078
}
8179
} else {
82-
external = electronBuiltins
80+
rollupOptions.output.freeze ??= false
8381
}
84-
return external
8582
}
8683

87-
// At present, Electron can only support CommonJs
88-
function setOutputFormat(rollupOptions: RollupOptions) {
89-
rollupOptions.output ??= {}
90-
if (Array.isArray(rollupOptions.output)) {
91-
for (const o of rollupOptions.output) {
92-
o.format ??= 'cjs'
84+
function withIgnore(configBuild: BuildOptions) {
85+
configBuild.commonjsOptions ??= {}
86+
if (configBuild.commonjsOptions.ignore) {
87+
if (typeof configBuild.commonjsOptions.ignore === 'function') {
88+
const userIgnore = configBuild.commonjsOptions.ignore
89+
configBuild.commonjsOptions.ignore = id => {
90+
if (userIgnore?.(id) === true) {
91+
return true
92+
}
93+
return electronBuiltins.includes(id)
94+
}
95+
} else {
96+
// @ts-ignore
97+
configBuild.commonjsOptions.ignore.push(...electronBuiltins)
9398
}
9499
} else {
95-
rollupOptions.output.format ??= 'cjs'
100+
configBuild.commonjsOptions.ignore = electronBuiltins
96101
}
97102
}
98103

0 commit comments

Comments
 (0)