feat(plugin-vite): add ability to ship esm bundle#4184
feat(plugin-vite): add ability to ship esm bundle#4184
Conversation
| inlineDynamicImports: true, | ||
| entryFileNames: '[name].js', | ||
| chunkFileNames: '[name].js', | ||
| entryFileNames: isEsm ? '[name].mjs' : '[name].js', |
There was a problem hiding this comment.
Should we make the other .cjs?
There was a problem hiding this comment.
Yeah, makes sense to me.
| input: forgeConfigSelf.entry, | ||
| output: { | ||
| format: 'cjs', | ||
| format: isEsm ? 'es' : 'cjs', |
There was a problem hiding this comment.
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 BrowserWindow constructor to allow us to pick the right format, we could account for that by tweaking the JSDoc in packages/plugin/vite/src/Config.ts to make this caveat clear, and maybe even adding some troubleshooting code like the following directly to the createWindow function in the Vite templates' main files to preemptively provide support for this issue:
// 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.`)
}
})|
@erikian @MarshallOfSound I updated the PR! Importantly, I changed the output to always have Alternatives considered:
Not sure what you guys prefer design-wise. |
Closes #4016
Automatically inferring based on
type: moduleinpackage.jsonlimits flexibility for apps that want to ship CommonJS within their Electron app.ESM in Electron has its own set of limitations and caveats, and CommonJS is still the standard way to ship your JavaScript app bundle.
This PR takes a different approach by adding
type: module/commonjsas an option to the Vite plugin, and modifying the bundle output in that manner as well.