@electron-forge/plugin-vite 8.0.0-alpha.7 → 8.0.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
+ import { styleText } from 'node:util';
2
+
1
3
  import { build } from 'vite';
2
4
 
5
+ import { viteDevServerUrls } from './config/vite.base.config.js';
3
6
  import ViteConfigGenerator from './ViteConfig.js';
4
7
 
5
8
  import type {
@@ -7,11 +10,14 @@ import type {
7
10
  VitePluginConfig,
8
11
  VitePluginRendererConfig,
9
12
  } from './Config.js';
13
+ import type { Rollup } from 'vite';
10
14
 
11
15
  const projectDir = process.env.FORGE_VITE_PROJECT_DIR;
12
16
  const kind = process.env.FORGE_VITE_KIND as 'build' | 'renderer';
13
17
  const index = Number(process.env.FORGE_VITE_INDEX);
14
18
  const rawConfig = process.env.FORGE_VITE_CONFIG;
19
+ const watch = process.env.FORGE_VITE_WATCH === '1';
20
+ const rawDevServerUrls = process.env.FORGE_VITE_DEV_SERVER_URLS;
15
21
 
16
22
  if (!projectDir || !kind || !rawConfig || !Number.isInteger(index)) {
17
23
  console.error(
@@ -25,7 +31,7 @@ if (!projectDir || !kind || !rawConfig || !Number.isInteger(index)) {
25
31
  // defines when building main targets.
26
32
  const pluginConfig = JSON.parse(rawConfig) as VitePluginConfig;
27
33
 
28
- const generator = new ViteConfigGenerator(pluginConfig, projectDir, true);
34
+ const generator = new ViteConfigGenerator(pluginConfig, projectDir, !watch);
29
35
 
30
36
  let spec: VitePluginBuildConfig | VitePluginRendererConfig;
31
37
  let target: 'main' | 'preload' | 'renderer';
@@ -37,11 +43,107 @@ if (kind === 'build') {
37
43
  target = 'renderer';
38
44
  }
39
45
 
46
+ // Seed the module-level URL map so getBuildDefine() produces the same
47
+ // *_VITE_DEV_SERVER_URL defines the in-process path would have.
48
+ if (rawDevServerUrls) {
49
+ Object.assign(viteDevServerUrls, JSON.parse(rawDevServerUrls));
50
+ }
51
+
40
52
  const resolved = await generator.resolveConfig(spec, target);
41
53
 
42
- await build({
43
- configFile: false,
44
- logLevel: 'error',
45
- ...resolved,
46
- clearScreen: false,
47
- });
54
+ if (!watch) {
55
+ await build({
56
+ configFile: false,
57
+ logLevel: 'error',
58
+ ...resolved,
59
+ clearScreen: false,
60
+ });
61
+ } else {
62
+ // Matches the format of the default Vite logger
63
+ const timeFormatter = new Intl.DateTimeFormat(undefined, {
64
+ hour: 'numeric',
65
+ minute: 'numeric',
66
+ second: 'numeric',
67
+ });
68
+
69
+ // Rollup's `input` can be a string, an array of strings, or an object.
70
+ // https://rollupjs.org/configuration-options/#input
71
+ const input =
72
+ resolved.build?.rollupOptions?.input ??
73
+ (typeof resolved.build?.lib !== 'boolean'
74
+ ? resolved.build?.lib?.entry
75
+ : undefined);
76
+ const targetDisplay = !input
77
+ ? ''
78
+ : typeof input === 'string'
79
+ ? input
80
+ : Array.isArray(input)
81
+ ? input.join(' ')
82
+ : Object.keys(input).join(' ');
83
+
84
+ let firstBuildSent = false;
85
+ const sendOnce = (msg: { type: string; message?: string }) => {
86
+ if (firstBuildSent) return;
87
+ firstBuildSent = true;
88
+ process.send?.(msg);
89
+ };
90
+
91
+ const result = await build({
92
+ // Avoid recursive builds caused by users configuring @electron-forge/plugin-vite in Vite config file.
93
+ configFile: false,
94
+ // We suppress Vite output and instead log lines using RollupWatcher events
95
+ logLevel: 'silent',
96
+ ...resolved,
97
+ plugins: [
98
+ // `buildEnd` and `closeBundle` are Rollup output generation hooks.
99
+ // https://rollupjs.org/plugin-development/#output-generation-hooks
100
+ {
101
+ name: '@electron-forge/plugin-vite:build-done',
102
+ buildEnd(err) {
103
+ if (err instanceof Error) {
104
+ sendOnce({ type: 'first-build-error', message: err.message });
105
+ }
106
+ },
107
+ closeBundle() {
108
+ sendOnce({ type: 'first-build-done' });
109
+ if (target === 'preload') {
110
+ // pluginHotRestart('reload') is a no-op here because viteDevServers
111
+ // lives in the parent; ask the parent to fan out the ws full-reload.
112
+ process.send?.({ type: 'reload-renderers' });
113
+ }
114
+ },
115
+ },
116
+ ...(resolved.plugins ?? []),
117
+ ],
118
+ clearScreen: false,
119
+ });
120
+
121
+ const isRollupWatcher = (x: unknown): x is Rollup.RollupWatcher =>
122
+ !!x &&
123
+ typeof x === 'object' &&
124
+ 'on' in x &&
125
+ typeof x.on === 'function' &&
126
+ 'close' in x &&
127
+ typeof x.close === 'function';
128
+
129
+ if (isRollupWatcher(result)) {
130
+ // The Rollup watcher emits events for subsequent builds.
131
+ result.on('event', (event) => {
132
+ if (event.code === 'ERROR' && resolved.logLevel !== 'silent') {
133
+ console.error(
134
+ `\n${styleText('dim', timeFormatter.format(new Date()))} ${event.error.message}`,
135
+ );
136
+ } else if (
137
+ event.code === 'BUNDLE_END' &&
138
+ (!resolved.logLevel || resolved.logLevel === 'info')
139
+ ) {
140
+ console.log(
141
+ `${styleText('dim', timeFormatter.format(new Date()))} ${styleText(['cyan', 'bold'], '[@electron-forge/plugin-vite]')} ${styleText(
142
+ 'green',
143
+ 'target built',
144
+ )} ${styleText('dim', targetDisplay)}`,
145
+ );
146
+ }
147
+ });
148
+ }
149
+ }