Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(template-vite): switch to esm by default. #3572

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/template/vite-typescript/tmpl/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const createWindow = () => {
width: 800,
height: 600,
webPreferences: {
// Load preload.mjs when the { "type": "module" }, else load preload.js
preload: path.join(__dirname, 'preload.mjs'),
},
});

// and load the index.html of the app.
// Load the index.html of the app.
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/template/vite-typescript/tmpl/vite.base.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const builtins = ['electron', ...builtinModules.map((m) => [m, `node:${m}

export const external = [...builtins, ...Object.keys('dependencies' in pkg ? (pkg.dependencies as Record<string, unknown>) : {})];

export const esmodule = pkg.type === 'module';
caoxiemeihao marked this conversation as resolved.
Show resolved Hide resolved

export function getBuildConfig(env: ConfigEnv<'build'>): UserConfig {
const { root, mode, command } = env;

Expand Down
4 changes: 2 additions & 2 deletions packages/template/vite-typescript/tmpl/vite.main.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConfigEnv, UserConfig } from 'vite';
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, getBuildDefine, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, getBuildDefine, external, esmodule, pluginHotRestart } from './vite.base.config';

// https://vitejs.dev/config
export default defineConfig((env) => {
Expand All @@ -12,7 +12,7 @@ export default defineConfig((env) => {
lib: {
entry: forgeConfigSelf.entry!,
fileName: () => '[name].js',
formats: ['es'],
formats: [esmodule ? 'es' : 'cjs'],
},
rollupOptions: {
external,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ConfigEnv, UserConfig } from 'vite';
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, external, esmodule, pluginHotRestart } from './vite.base.config';

// https://vitejs.dev/config
export default defineConfig((env) => {
const forgeEnv = env as ConfigEnv<'build'>;
const { forgeConfigSelf } = forgeEnv;
const ext = esmodule ? 'mjs' : 'js';
caoxiemeihao marked this conversation as resolved.
Show resolved Hide resolved
const config: UserConfig = {
build: {
rollupOptions: {
Expand All @@ -18,8 +19,8 @@ export default defineConfig((env) => {
format: 'cjs',
// It should not be split chunks.
inlineDynamicImports: true,
entryFileNames: '[name].mjs',
chunkFileNames: '[name].mjs',
entryFileNames: `[name].${ext}`,
chunkFileNames: `[name].${ext}`,
assetFileNames: '[name].[ext]',
},
},
Expand Down
3 changes: 2 additions & 1 deletion packages/template/vite/tmpl/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const createWindow = () => {
width: 800,
height: 600,
webPreferences: {
// Load preload.mjs when the { "type": "module" }, else load preload.js
preload: path.join(__dirname, 'preload.mjs'),
},
});

// and load the index.html of the app.
// Load the index.html of the app.
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/template/vite/tmpl/vite.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const builtins = ['electron', ...builtinModules.map((m) => [m, `node:${m}

export const external = [...builtins, ...Object.keys(pkg.dependencies || {})];

export const esmodule = pkg.type === 'module';

/** @type {(env: import('vite').ConfigEnv<'build'>) => import('vite').UserConfig} */
export const getBuildConfig = (env) => {
const { root, mode, command } = env;
Expand Down
4 changes: 2 additions & 2 deletions packages/template/vite/tmpl/vite.main.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, getBuildDefine, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, getBuildDefine, external, esmodule, pluginHotRestart } from './vite.base.config';

// https://vitejs.dev/config
export default defineConfig((env) => {
Expand All @@ -12,7 +12,7 @@ export default defineConfig((env) => {
lib: {
entry: forgeConfigSelf.entry,
fileName: () => '[name].js',
formats: ['es'],
formats: [esmodule ? 'es' : 'cjs'],
},
rollupOptions: {
external,
Expand Down
7 changes: 4 additions & 3 deletions packages/template/vite/tmpl/vite.preload.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineConfig, mergeConfig } from 'vite';
import { getBuildConfig, external, pluginHotRestart } from './vite.base.config';
import { getBuildConfig, external, esmodule, pluginHotRestart } from './vite.base.config';

// https://vitejs.dev/config
export default defineConfig((env) => {
/** @type {import('vite').ConfigEnv<'build'>} */
const forgeEnv = env;
const { forgeConfigSelf } = forgeEnv;
const ext = esmodule ? 'mjs' : 'js';
/** @type {import('vite').UserConfig} */
const config = {
build: {
Expand All @@ -19,8 +20,8 @@ export default defineConfig((env) => {
format: 'cjs',
// It should not be split chunks.
inlineDynamicImports: true,
entryFileNames: '[name].mjs',
chunkFileNames: '[name].mjs',
entryFileNames: `[name].${ext}`,
chunkFileNames: `[name].${ext}`,
assetFileNames: '[name].[ext]',
},
},
Expand Down