-
-
Notifications
You must be signed in to change notification settings - Fork 616
feat(plugin-vite): add ability to ship esm bundle #4184
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
base: next
Are you sure you want to change the base?
Changes from 7 commits
f2da3b4
d9589bf
a32c4a2
bdd7822
582edf4
2c20972
03826e3
3712f7e
457bf08
9888d4f
ed1efe2
8866e27
bc21af1
71e424c
e115192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ export function getConfig( | |
| forgeEnv: ConfigEnv<'build'>, | ||
| userConfig: UserConfig = {}, | ||
| ): UserConfig { | ||
| const { forgeConfigSelf } = forgeEnv; | ||
| const { forgeConfigSelf, forgeConfig } = forgeEnv; | ||
| const isEsm = forgeConfig.type === 'module'; | ||
| const config: UserConfig = { | ||
| build: { | ||
| copyPublicDir: false, | ||
|
|
@@ -19,11 +20,11 @@ export function getConfig( | |
| // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`. | ||
| input: forgeConfigSelf.entry, | ||
| output: { | ||
| format: 'cjs', | ||
| format: isEsm ? 'es' : 'cjs', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: ESM preloads will only work in unsandboxed renderers according to the docs. Since the Vite config doesn't have access to the parameters passed to the // ESM preloads only work if your renderer is unsandboxed, which is disabled
// by default for security reasons. If your preload file fails to load and
// your renderer is sandboxed (i.e. the `webPreferences.sandbox` option in your
// `BrowserWindow` constructor is `true` or isn't set), please set
// `config.build.rollupOptions.output.format` to `commonjs` in your
// `vite.preload.config.ts` file.
mainWindow.webContents.on('preload-error', (event, preloadPath, error) => {
if(preloadPath.endsWith('.mjs') &&
// optional - these might be unnecessary or even wrong, but syntax errors
// thrown when using `import` or top-level `await` in a non-ESM context
// both contain the word "module" and they're bound to be the most common
// ones in this scenario 🤷🏼
// would be fine to omit these conditions, though, even if it means
// showing this message for unrelated errors thrown in the preload.
error.stack?.startsWith('SyntaxError') &&
error.message.includes('module')
) {
console.error(`Fail to load ${preloadPath}. Make sure you're using the \`commonjs\` output format in \`vite.preload.config.ts\` if your renderer is sandboxed.`)
}
})
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // It should not be split chunks. | ||
| inlineDynamicImports: true, | ||
| entryFileNames: '[name].js', | ||
| chunkFileNames: '[name].js', | ||
| entryFileNames: isEsm ? '[name].mjs' : '[name].js', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make the other
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, makes sense to me. |
||
| chunkFileNames: isEsm ? '[name].mjs' : '[name].js', | ||
| assetFileNames: '[name].[ext]', | ||
| }, | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.