kaisermann / svelte-i18n

Internationalization library for Svelte
MIT License
1.26k stars 80 forks source link

Allow unit tests with ts-node esm #219

Closed tipy01 closed 1 year ago

tipy01 commented 1 year ago

Describe the bug I make unit test on my Typescript Svelte 4 app using ts-node with the esm loader. Many of my ts files under test import svelte-i18n and ts-node do not handle importing svelte-i18n.

Expected behavior To import the lib as esm module.

Stacktraces

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/myproject/node_modules/svelte/src/runtime/store/index.js from /home/myproject/node_modules/svelte-i18n/dist/runtime.cjs.js not supported.
Instead change the require of index.js in /myproject/node_modules/svelte-i18n/dist/runtime.cjs.js to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/myproject/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/myproject/node_modules/svelte-i18n/dist/runtime.cjs.js:1:20)
    at require.extensions.<computed> [as .js] (/myproject/node_modules/ts-node/dist/index.js:851:20)

Potential solution

I tested to put "type":"module" and changing "main" in node_modules/svelte-i18n/package.json:

  "type": "module",
  "main": "dist/runtime.esm.js",

and that fixed the problem (that seems very logical).

I know doing such change make my Svelte 3 based project unit tests no longer working but the app build still works.

Information about your project:

node v18.16.1

"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"svelte": "^4.0.5",
"svelte-i18n": "^3.7.0",
tipy01 commented 1 year ago

Now with the 3.7.4 I fall in the same as #230, so I think solving #230 will fix this one.