Open hl662 opened 3 weeks ago
Pausing work on this PR for now, but documenting the work so far:
"type": "module"
in it's package.json
). This is relevant to @itwin/electron-authorization
, because we have export maps in there. I experimented with making that package to be ESM, pushing my work to a remote branch. I confirmed that electron-authorization's tokenStore was able to run, successfully accessing my OS-based credentials and utilizing 'electron-store' under the hood, so the branch I linked above demonstrates the feasibility of publishing a ESM-based NPM package for backends.__dirname is not defined
error, but it pops up on the frontend side of the electron app, suggesting that our vite.config.js needs to be updated to use electron's ESM version, rather than the CJS version. I suspect that if we are able to solve this particular issue, we'll see more than just the spinner on startupcore/electron/
and run just pnpm build:esm
- I'm using the import.meta.url in ElectronHost.ts
, so building CJS will throw build errors. Then go to test-apps/display-test-app
and run pnpm build
followed by pnpm start
.import.meta.url
. CJS allows require but ESM doesn't. ESM allows import.meta.url but CJS doesn't. Why is this important? core-electron's ElectronHost requires a path to ElectronPreload.mjs passed into one of it's config, and the 2 ways above are the 2 unique and correct ways to resolve file paths within a module.Image of the runtime issue:
I haven't dug into why the preload script is not being able to be loaded - everything does point to the backend being able to find ElectronPreload.mjs though (there was a prior runtime error saying it couldn't find the file).
ESM is not supported for preload script if sandbox is enabled (Electron ESM support matrix). If we plan to drop CJS from both backend and frontend code for Electron, we'll still need to transpile preload script to CJS.
I haven't dug into the
__dirname is not defined
error, but it pops up on the frontend side of the electron app, suggesting that our vite.config.js needs to be updated to use electron's ESM version, rather than the CJS version.
Isn't __dirname
only available in CJS ? __dirname
Node.js docs
Notables:
Note: tsc does not add
.js
extensions to extensionless imports (unlike Vite :( ), so had to add.js
at the end of our relative import paths in DTA's backend.