-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ec7e18
commit 6774799
Showing
9 changed files
with
86 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,101 @@ | ||
import { builtinModules } from 'node:module'; | ||
import path from 'node:path'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { external } from '../src/config/vite.base.config'; | ||
import ViteConfigGenerator from '../src/ViteConfig'; | ||
|
||
import type { VitePluginConfig } from '../src/Config'; | ||
import type { UserConfig } from 'vite'; | ||
import type { Plugin } from 'vite'; | ||
|
||
const builtins = ['electron', ...builtinModules.map((m) => [m, `node:${m}`]).flat()]; | ||
const configRoot = path.join(__dirname, 'fixture/config'); | ||
const configRoot = path.join(__dirname, 'fixtures/vite-configs'); | ||
|
||
describe('ViteConfigGenerator', () => { | ||
it('getBuildConfig', async () => { | ||
const config = { | ||
build: [{ config: path.join(configRoot, 'vite.main.config.mjs') }], | ||
renderer: [], | ||
} as VitePluginConfig; | ||
const generator = new ViteConfigGenerator(config, configRoot, true); | ||
const buildConfig1 = (await generator.getBuildConfig())[0]; | ||
const buildConfig2: UserConfig = { | ||
root: configRoot, | ||
mode: 'production', | ||
build: { | ||
lib: { | ||
it('getBuildConfig:main', async () => { | ||
const forgeConfig: VitePluginConfig = { | ||
build: [ | ||
{ | ||
entry: 'src/main.js', | ||
formats: ['cjs'], | ||
// shims | ||
fileName: (buildConfig1.build?.lib as any)?.fileName, | ||
}, | ||
emptyOutDir: false, | ||
outDir: '.vite/build', | ||
minify: true, // this.isProd === true | ||
watch: null, | ||
rollupOptions: { | ||
external: builtins, | ||
config: path.join(configRoot, 'vite.main.config.js'), | ||
target: 'main', | ||
}, | ||
}, | ||
clearScreen: false, | ||
], | ||
renderer: [], | ||
}; | ||
const generator = new ViteConfigGenerator(forgeConfig, configRoot, true); | ||
const buildConfig = (await generator.getBuildConfig())[0]; | ||
|
||
expect(buildConfig1).deep.equal(buildConfig2); | ||
expect(buildConfig.root).equal(configRoot); | ||
expect(buildConfig.mode).equal('production'); | ||
expect(buildConfig.build?.emptyOutDir).false; | ||
expect(buildConfig.build?.outDir).equal('.vite/build'); | ||
expect(buildConfig.build?.watch).null; | ||
expect(buildConfig.build?.minify).true; | ||
expect(buildConfig.build?.lib && buildConfig.build.lib.entry).equal('src/main.js'); | ||
expect(buildConfig.build?.lib && (buildConfig.build.lib.fileName as () => string)()).equal('[name].js'); | ||
expect(buildConfig.build?.lib && buildConfig.build.lib.formats).deep.equal(['cjs']); | ||
expect(buildConfig.build?.rollupOptions?.external).deep.equal(external); | ||
expect(buildConfig.clearScreen).false; | ||
expect(buildConfig.plugins?.map((plugin) => (plugin as Plugin).name)).deep.equal(['@electron-forge/plugin-vite:hot-restart']); | ||
expect(buildConfig.define).deep.equal({}); | ||
expect(buildConfig.resolve).deep.equal({ | ||
conditions: ['node'], | ||
mainFields: ['module', 'jsnext:main', 'jsnext'], | ||
}); | ||
}); | ||
|
||
it('getRendererConfig', async () => { | ||
const config = { | ||
build: [{ config: path.join(configRoot, 'vite.renderer.config.mjs') }], | ||
it('getBuildConfig:preload', async () => { | ||
const forgeConfig: VitePluginConfig = { | ||
build: [ | ||
{ | ||
entry: 'src/preload.js', | ||
config: path.join(configRoot, 'vite.preload.config.js'), | ||
target: 'preload', | ||
}, | ||
], | ||
renderer: [], | ||
} as VitePluginConfig; | ||
const generator = new ViteConfigGenerator(config, configRoot, true); | ||
const buildConfig1 = (await generator.getBuildConfig())[0]; | ||
const buildConfig2: UserConfig = { | ||
root: configRoot, | ||
mode: 'production', | ||
base: './', | ||
build: { | ||
outDir: 'renderer/main_window', | ||
}, | ||
}; | ||
const generator = new ViteConfigGenerator(forgeConfig, configRoot, true); | ||
const buildConfig = (await generator.getBuildConfig())[0]; | ||
|
||
expect(buildConfig.root).equal(configRoot); | ||
expect(buildConfig.mode).equal('production'); | ||
expect(buildConfig.build?.emptyOutDir).false; | ||
expect(buildConfig.build?.outDir).equal('.vite/build'); | ||
expect(buildConfig.build?.watch).null; | ||
expect(buildConfig.build?.minify).true; | ||
expect(buildConfig.build?.rollupOptions?.external).deep.equal(external); | ||
expect(buildConfig.build?.rollupOptions?.input).equal('src/preload.js'); | ||
expect(buildConfig.build?.rollupOptions?.output).deep.equal({ | ||
format: 'cjs', | ||
inlineDynamicImports: true, | ||
entryFileNames: '[name].js', | ||
chunkFileNames: '[name].js', | ||
assetFileNames: '[name].[ext]', | ||
}); | ||
expect(buildConfig.clearScreen).false; | ||
expect(buildConfig.plugins?.map((plugin) => (plugin as Plugin).name)).deep.equal(['@electron-forge/plugin-vite:hot-restart']); | ||
}); | ||
|
||
it('getRendererConfig:renderer', async () => { | ||
const forgeConfig = { | ||
build: [], | ||
renderer: [ | ||
{ | ||
name: 'main_window', | ||
config: path.join(configRoot, 'vite.renderer.config.js'), | ||
}, | ||
], | ||
}; | ||
const generator = new ViteConfigGenerator(forgeConfig, configRoot, true); | ||
const rendererConfig = (await generator.getRendererConfig())[0]; | ||
|
||
expect(buildConfig1).deep.equal(buildConfig2); | ||
expect(rendererConfig.root).equal(configRoot); | ||
expect(rendererConfig.mode).equal('production'); | ||
expect(rendererConfig.base).equal('./'); | ||
expect(rendererConfig.build?.outDir).equal('.vite/renderer/main_window'); | ||
expect(rendererConfig.plugins?.map((plugin) => (plugin as Plugin).name)).deep.equal(['@electron-forge/plugin-vite:expose-renderer']); | ||
expect(rendererConfig.resolve).deep.equal({ preserveSymlinks: true }); | ||
expect(rendererConfig.clearScreen).false; | ||
}); | ||
}); |
31 changes: 0 additions & 31 deletions
31
packages/plugin/vite/test/fixture/config/vite.main.config.mjs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
packages/plugin/vite/test/fixture/config/vite.renderer.config.mjs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
2 changes: 2 additions & 0 deletions
2
packages/plugin/vite/test/fixtures/vite-configs/vite.main.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* eslint-disable */ | ||
export default {}; |
2 changes: 2 additions & 0 deletions
2
packages/plugin/vite/test/fixtures/vite-configs/vite.preload.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* eslint-disable */ | ||
export default {}; |
2 changes: 2 additions & 0 deletions
2
packages/plugin/vite/test/fixtures/vite-configs/vite.renderer.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* eslint-disable */ | ||
export default {}; |
This file was deleted.
Oops, something went wrong.