-
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathVitePlugin.spec.ts
More file actions
299 lines (266 loc) · 9.69 KB
/
VitePlugin.spec.ts
File metadata and controls
299 lines (266 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { IgnoreFunction } from '@electron/packager';
import { ResolvedForgeConfig } from '@electron-forge/shared-types';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { VitePluginConfig } from '../src/Config';
import { VitePlugin } from '../src/VitePlugin';
describe('VitePlugin', async () => {
const baseConfig: VitePluginConfig = {
build: [],
renderer: [],
};
const tmp = os.tmpdir();
const tmpdir = path.join(tmp, 'electron-forge-test-');
const viteTestDir = await fs.promises.mkdtemp(tmpdir);
afterAll(async () => {
await fs.promises.rm(viteTestDir, { recursive: true });
});
describe('packageAfterCopy', () => {
const packageJSONPath = path.join(viteTestDir, 'package.json');
const packagedPath = path.join(viteTestDir, 'packaged');
const packagedPackageJSONPath = path.join(packagedPath, 'package.json');
let plugin: VitePlugin;
beforeAll(async () => {
await fs.promises.mkdir(packagedPath);
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(viteTestDir);
});
it('should remove config.forge from package.json', async () => {
const packageJSON = {
main: './.vite/build/main.js',
config: { forge: 'config.js' },
};
await fs.promises.writeFile(
packageJSONPath,
JSON.stringify(packageJSON),
'utf-8',
);
await plugin.packageAfterCopy({} as ResolvedForgeConfig, packagedPath);
expect(fs.existsSync(packagedPackageJSONPath)).toEqual(true);
expect(
JSON.parse(await fs.promises.readFile(packagedPackageJSONPath, 'utf-8'))
.config,
).not.toHaveProperty('forge');
});
it('should succeed if there is no config.forge', async () => {
const packageJSON = { main: '.vite/build/main.js' };
await fs.promises.writeFile(
packageJSONPath,
JSON.stringify(packageJSON),
'utf-8',
);
await plugin.packageAfterCopy({} as ResolvedForgeConfig, packagedPath);
expect(fs.existsSync(packagedPackageJSONPath)).toEqual(true);
expect(
JSON.parse(
await fs.promises.readFile(packagedPackageJSONPath, 'utf-8'),
),
).not.toHaveProperty('config');
});
it('should fail if there is no main key in package.json', async () => {
const packageJSON = {};
await fs.promises.writeFile(
packageJSONPath,
JSON.stringify(packageJSON),
'utf-8',
);
await expect(
plugin.packageAfterCopy({} as ResolvedForgeConfig, packagedPath),
).rejects.toThrow(/entry point/);
});
it('should fail if main in package.json does not starts with .vite/', async () => {
const packageJSON = { main: 'src/main.js' };
await fs.promises.writeFile(
packageJSONPath,
JSON.stringify(packageJSON),
'utf-8',
);
await expect(
plugin.packageAfterCopy({} as ResolvedForgeConfig, packagedPath),
).rejects.toThrow(/entry point/);
});
});
describe('resolveForgeConfig', () => {
let plugin: VitePlugin;
beforeAll(() => {
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(viteTestDir);
});
it('sets packagerConfig and packagerConfig.ignore if it does not exist', async () => {
const config = await plugin.resolveForgeConfig({} as ResolvedForgeConfig);
expect(config.packagerConfig).not.toEqual(undefined);
expect(config.packagerConfig.ignore).toBeTypeOf('function');
});
describe('packagerConfig.ignore', () => {
it('does not overwrite an existing ignore value', async () => {
const config = await plugin.resolveForgeConfig({
packagerConfig: {
ignore: /test/,
},
} as ResolvedForgeConfig);
expect(config.packagerConfig.ignore).toEqual(/test/);
});
it('ignores everything but files in .vite', async () => {
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
expect(ignore('')).toEqual(false);
expect(ignore('/abc')).toEqual(true);
expect(ignore('/.vite')).toEqual(false);
expect(ignore('/.vite/foo')).toEqual(false);
});
it('ignores source map files by default', async () => {
const viteConfig = { ...baseConfig };
plugin = new VitePlugin(viteConfig);
plugin.setDirectories(viteTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
expect(ignore(path.posix.join('/.vite', 'build', 'main.js'))).toEqual(
false,
);
expect(
ignore(path.posix.join('/.vite', 'build', 'main.js.map')),
).toEqual(false);
expect(
ignore(
path.posix.join(
'/.vite',
'renderer',
'main_window',
'assets',
'index.js',
),
),
).toEqual(false);
expect(
ignore(
path.posix.join(
'/.vite',
'renderer',
'main_window',
'assets',
'index.js.map',
),
),
).toEqual(false);
});
it('includes source map files when specified by config', async () => {
const viteConfig = { ...baseConfig, packageSourceMaps: true };
plugin = new VitePlugin(viteConfig);
plugin.setDirectories(viteTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
expect(ignore(path.posix.join('/.vite', 'build', 'main.js'))).toEqual(
false,
);
expect(
ignore(path.posix.join('/.vite', 'build', 'main.js.map')),
).toEqual(false);
expect(
ignore(
path.posix.join(
'/.vite',
'renderer',
'main_window',
'assets',
'index.js',
),
),
).toEqual(false);
expect(
ignore(
path.posix.join(
'/.vite',
'renderer',
'main_window',
'assets',
'index.js.map',
),
),
).toEqual(false);
});
it('includes /node_modules directory', async () => {
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(viteTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
expect(ignore('/node_modules')).toEqual(false);
});
it('excludes packages without native modules from node_modules', async () => {
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(viteTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
// Non-native packages should be excluded
expect(ignore('/node_modules/lodash')).toEqual(true);
expect(ignore('/node_modules/lodash/index.js')).toEqual(true);
});
describe('with native modules', () => {
const nativeModulesTestDir = path.join(viteTestDir, 'native-test');
beforeAll(async () => {
// Create a fake node_modules structure with a native module
const nativePackagePath = path.join(
nativeModulesTestDir,
'node_modules',
'native-package',
'build',
'Release',
);
await fs.promises.mkdir(nativePackagePath, { recursive: true });
await fs.promises.writeFile(
path.join(nativePackagePath, 'binding.node'),
'fake native module',
);
// Create a non-native package
const nonNativePackagePath = path.join(
nativeModulesTestDir,
'node_modules',
'non-native-package',
);
await fs.promises.mkdir(nonNativePackagePath, { recursive: true });
await fs.promises.writeFile(
path.join(nonNativePackagePath, 'index.js'),
'module.exports = {}',
);
});
it('includes packages containing .node files', async () => {
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(nativeModulesTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
// Native package should be included (not ignored)
expect(ignore('/node_modules/native-package')).toEqual(false);
expect(
ignore('/node_modules/native-package/build/Release/binding.node'),
).toEqual(false);
});
it('excludes packages without .node files', async () => {
plugin = new VitePlugin(baseConfig);
plugin.setDirectories(nativeModulesTestDir);
const config = await plugin.resolveForgeConfig(
{} as ResolvedForgeConfig,
);
const ignore = config.packagerConfig.ignore as IgnoreFunction;
// Non-native package should be excluded (ignored)
expect(ignore('/node_modules/non-native-package')).toEqual(true);
expect(ignore('/node_modules/non-native-package/index.js')).toEqual(
true,
);
});
});
});
});
});