You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiler produces mixed ESM and CJS import syntax (Electron Forge x Vite x DeepKit) #615
Setting up a very basic project with Electron Forge, TypeScript, and the default bundler Vite (included with Forge) together with DeepKit seems to produce output script files with mixed ESM and CJS syntax.
Now I’m really wandering in the dark here. As far as I know, Electron generally only works with CJS syntax (e.g. setting tsconfig.jsonmodule to ES6 gives me a number of build time errors while CommonJS is the default in every example and template). Electron Forge uses Vite (ESBuild, Rollup) to bundle everything. Somehow, once it gets to the renderer process (a.k.a. the browser window), it has become ESM syntax again.
I’m assuming DeepKit reads the tsconfig.json, sees CommonJS there and that’s what it outputs. I can’t seem to override this to have the injected DeepKit compiler output ESM or have Vite transform the rest.
Even using "@rollup/plugin-commonjs" to maybe transform mixed syntax does not change the final output. Ultimately, my project crashes with a runtime error.
Electron Forge 7.5.0
Vite 5.0.12
TypeScript 5.6.2
DeepKit 1.0.1-alpha.154
Did some testing and yeah, Forge’s strange mixed module setting is the heart of the issue. A reasonable solution was for me to define three files, tsconfig.common.json with common settings (for me, everything but target, module, and moduleResolution), and then dedicated config entry points, one for Electron Forge (a default tsconfig.json extending the common one), and a new one for DeepKit (a tsconfig.deepkit.json, also extending the common one).
Then I’ve configured the Vite config with an explicit path:
I’d say this issue is predominantly one of Forge not properly supporting ESM, something they’re apparently working on right now anyway. Thanks for your input on this, @marcj!
Activity
marcj commentedon Oct 4, 2024
just as a note, this is the code we use for determining cjs/esm: https://github.com/deepkit/deepkit-framework/blob/ab96f2f3c0f7f795692d49e6a37ee5194647a9c1/packages/type-compiler/src/compiler.ts#L1048-L1056
so it comes from the typescript compiler/tsconfig itself
marcj commentedon Oct 4, 2024
you can enable
DEBUG=deepkit your-build-commandto get more information about what tsconfig is loaded and which module system is pickedAugustSaintFreytag commentedon Oct 5, 2024
Did some testing and yeah, Forge’s strange mixed module setting is the heart of the issue. A reasonable solution was for me to define three files,
tsconfig.common.jsonwith common settings (for me, everything buttarget,module, andmoduleResolution), and then dedicated config entry points, one for Electron Forge (a defaulttsconfig.jsonextending the common one), and a new one for DeepKit (atsconfig.deepkit.json, also extending the common one).Then I’ve configured the Vite config with an explicit path:
I’d say this issue is predominantly one of Forge not properly supporting ESM, something they’re apparently working on right now anyway. Thanks for your input on this, @marcj!