Skip to content

Commit e16808d

Browse files
authored
Merge pull request #2 from electron-vite/dev
release: v0.4.2
2 parents 2a06db4 + 206590e commit e16808d

File tree

4 files changed

+92
-48
lines changed

4 files changed

+92
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface Configuration {
6262

6363
## How to work
6464

65-
This plugin is just a builtin scripts of [electron-vite-boilerplate](https://github.com/electron-vite/electron-vite-boilerplate)
65+
The plugin is just the encapsulation of the built-in scripts of [electron-vite-boilerplate/scripts](https://github.com/electron-vite/electron-vite-boilerplate/tree/main/scripts)
6666

6767
🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧
6868

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",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "Integrate Vite and Electron",
55
"main": "dist/index.js",
66
"repository": {

polyfill-exports.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
* @type {() => import('vite').Plugin}
4+
*/
5+
module.exports = function polyfilleExports() {
6+
/**
7+
* @type {import('vite').ResolvedConfig}
8+
*/
9+
let config;
10+
11+
return {
12+
name: 'vite-plugin-electron:polyfill-exports',
13+
configResolved(_config) {
14+
config = _config;
15+
},
16+
transformIndexHtml(html) {
17+
const output = config.build.rollupOptions.output;
18+
if (!output) return;
19+
20+
const format = Array.isArray(output) ? output.find(e => e.format) : output.format;
21+
22+
// https://github.com/electron-vite/electron-vite-vue/issues/103#issuecomment-1097540635
23+
if (['cjs', 'commonjs'].includes(format)) {
24+
const polyfill = '<script>var exports = module.exports;</script>';
25+
return html.replace(/(<\/[\s\r\n]*?head[\s\r\n]*?>)/, polyfill + '\n$1');
26+
}
27+
},
28+
};
29+
};

renderer/index.js

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,40 @@ const optimizer = require('vite-plugin-optimizer');
77
*/
88
module.exports = function () {
99
const name = 'vite-plugin-electron-renderer';
10-
const plugin = optimizer(
11-
builtinModulesExport(builtinModules.filter(e => !e.startsWith('_'))),
12-
{ dir: `.${name}` },
13-
);
14-
plugin.name = name;
10+
const builtins = builtinModules.filter(e => !e.startsWith('_'));
11+
const plugin = optimizer(builtinModulesExport(builtins), { dir: `.${name}` },);
12+
13+
plugin.name = `${name}:optimizer`;
14+
plugin.apply = 'serve';
1515

1616
return [
17+
plugin,
18+
{
19+
name: `${name}:config-serve`,
20+
apply: 'serve',
21+
config(config) {
22+
// Vite ---- resolve.alias ----
23+
if (!config.resolve) config.resolve = {};
24+
if (!config.resolve.conditions) config.resolve.conditions = ['node'];
25+
if (!config.resolve.alias) config.resolve.alias = [];
26+
27+
const electronjs = path.join(__dirname, 'modules/electron-renderer.js');
28+
if (Array.isArray(config.resolve.alias)) {
29+
config.resolve.alias.push({ find: 'electron', replacement: electronjs });
30+
} else {
31+
config.resolve.alias['electron'] = electronjs;
32+
}
33+
34+
// Vite ---- optimizeDeps.exclude ----
35+
if (!config.optimizeDeps) config.optimizeDeps = {};
36+
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = [];
37+
38+
config.optimizeDeps.exclude.push('electron');
39+
},
40+
},
1741
{
18-
name: `${name}:config`,
42+
name: `${name}:config-build`,
43+
apply: 'build',
1944
config(config) {
2045
// make sure that Electron can be loaded into the local file using `loadFile` after packaging
2146
if (!config.base) config.base = './';
@@ -25,57 +50,47 @@ module.exports = function () {
2550
// ensure that static resources are loaded normally
2651
if (!config.build.assetsDir) config.build.assetsDir = '';
2752

28-
// ----------------------------------------
29-
// Rollup: output.format, output.external
30-
53+
// Rollup ---- init ----
3154
if (!config.build.rollupOptions) config.build.rollupOptions = {};
3255
if (!config.build.rollupOptions.output) config.build.rollupOptions.output = {};
3356

34-
const prodExternals = [...builtinModules.filter(e => !e.startsWith('_')), 'electron'];
35-
36-
const modifyOutput = output => {
37-
if (!output.format) {
38-
// the packaged Electron app should use "cjs"
39-
output.format = 'cjs';
40-
}
41-
42-
// make builtin modules & electron external when rollup
43-
output.external = [...(output.external || []), ...prodExternals];
44-
};
45-
if (Array.isArray(config.build.rollupOptions.output)) {
46-
config.build.rollupOptions.output.forEach(output => {
47-
modifyOutput(output);
48-
});
57+
// Rollup ---- external ----
58+
let external = config.build.rollupOptions.external;
59+
const electronBuiltins = builtins.map(e => [e, `node:${e}`]).flat().concat('electron');
60+
if (
61+
Array.isArray(external) ||
62+
typeof external === 'string' ||
63+
external instanceof RegExp
64+
) {
65+
external = electronBuiltins.concat(external);
66+
} else if (typeof external === 'function') {
67+
const original = external;
68+
external = function (source, importer, isResolved) {
69+
if (electronBuiltins.includes(source)) {
70+
return true;
71+
}
72+
return original(source, importer, isResolved);
73+
};
4974
} else {
50-
modifyOutput(config.build.rollupOptions.output);
75+
external = electronBuiltins;
5176
}
5277

53-
// ----------------------------------------
54-
// Vite: resolve.alias
55-
56-
if (!config.resolve) config.resolve = {};
57-
if (!config.resolve.conditions) config.resolve.conditions = ['node'];
58-
if (!config.resolve.alias) config.resolve.alias = [];
59-
const electronjs = path.join(__dirname, 'modules/electron-renderer.js');
78+
// make builtin modules & electron external when rollup
79+
config.build.rollupOptions.external = external;
6080

61-
if (Array.isArray(config.resolve.alias)) {
62-
config.resolve.alias.push({
63-
find: 'electron',
64-
replacement: electronjs,
65-
});
81+
// Rollup ---- output.format ----
82+
const output = config.build.rollupOptions.output;
83+
if (Array.isArray(output)) {
84+
for (const o of output) {
85+
if (o.format === undefined) o.format = 'cjs';
86+
}
6687
} else {
67-
config.resolve.alias['electron'] = electronjs;
88+
// external modules such as `electron`, `fs`
89+
// they can only be loaded normally under CommonJs
90+
if (output.format === undefined) output.format = 'cjs';
6891
}
69-
70-
// ----------------------------------------
71-
// Vite: optimizeDeps.exclude
72-
73-
if (!config.optimizeDeps) config.optimizeDeps = {};
74-
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = [];
75-
config.optimizeDeps.exclude.push('electron');
7692
},
7793
},
78-
plugin,
7994
];
8095
};
8196

0 commit comments

Comments
 (0)