deltachat-desktop icon indicating copy to clipboard operation
deltachat-desktop copied to clipboard

Refactoring for ESM module support

Open link2xt opened this issue 1 year ago • 4 comments

This is an attempt to make https://github.com/deltachat/deltachat-core-rust/pull/4971 works. It works without ESM modules currently, but with ESM module PR there are still problems:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/src/deltachat/deltachat-core-rust/node/dist/index.js from /home/user/src/deltachat/deltachat-desktop/tsc-dist/main/deltachat/controller.js not supported.
Instead change the require of index.js in /home/user/src/deltachat/deltachat-desktop/tsc-dist/main/deltachat/controller.js to a dynamic import() which is available in all CommonJS modules.
    at c._load (node:electron/js2c/node_init:2:13672)
    at Object.<anonymous> (/home/user/src/deltachat/deltachat-desktop/tsc-dist/main/deltachat/controller.js:39:39)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Object.<anonymous> (/home/user/src/deltachat/deltachat-desktop/tsc-dist/main/ipc.js:56:38)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Object.<anonymous> (/home/user/src/deltachat/deltachat-desktop/tsc-dist/main/index.js:134:26)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Object.<anonymous> (/home/user/src/deltachat/deltachat-desktop/index.js:11:1)
    at c._load (node:electron/js2c/node_init:2:13672)

I don't understand where this require comes from. Maybe it is because main module is a CommonJS module: https://github.com/deltachat/deltachat-desktop/blob/e603178e5eae0f933138e3e7cbfe399bf70f3c06/src/main/tsconfig.json#L6

link2xt avatar Apr 30 '24 15:04 link2xt

the point of the dynamic require/import is that we can present the errors (when native module is not found) to the user in a dialog without crashing the whole app immediately.

Simon-Laux avatar Apr 30 '24 15:04 Simon-Laux

the point of the dynamic require/import is that we can present the errors (when native module is not found) to the user in a dialog without crashing the whole app immediately.

Is it actually useful? Never seen this error, why would deltachat-node be missing and not some other module?

link2xt avatar Apr 30 '24 16:04 link2xt

We can also drop second commit, but seems easier to remove this code, I don't think anyone wants to try making async dynamic import work.

link2xt avatar Apr 30 '24 16:04 link2xt

why would deltachat-node be missing and not some other module?

because it is the only native dependency. it's not about dc-node js part, it is about the native part of the node bindings.

Simon-Laux avatar Apr 30 '24 16:04 Simon-Laux

First commit is merged as #3795 already, so let's close this for now.

link2xt avatar May 02 '24 11:05 link2xt