Skip to content

Commit 39a1998

Browse files
authored
Export ESM module (#1518)
Allow bundlers to pick the best version, do tree-shaking, … I checked that the below works with typescript, but I didn’t check if npmjs.com is smart enough to add the little “TS” icon. I assume so according to their docs, but if not, we can re-add the old `"types"` field. <details> <summary>Details about <code>exports</code></summary> According to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing: > if you write an import from an ES module, it will look up the import field, and from a CommonJS module, it will look at the require field. If it finds them, it will look for a corresponding declaration file. If you need to point to a different location for your type declarations, you can add a "types" import condition. > […] > It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. and according to npmjs.com’s [RFC 46](https://github.com/npm/rfcs/blob/6f61e5a2394040fa5843024d799db4ac675378d5/implemented/0046-add-types-metadata-to-package-json.md): > If there is not the explicit fields: Resolving the main with both TypeScript and Flow's extra file resolvers (e.g. when distribution/danger.js look for the files ./distribution/danger.js.flow and ./distribution/danger.d.ts) So in summary, TypeScript 4.7+ should find the `.d.ts` files, and npmjs.com should therefore resolve it to, but others had problems with that: browserslist/browserslist-useragent-regexp#1545 </details>
1 parent a1513ca commit 39a1998

5 files changed

Lines changed: 309 additions & 5 deletions

File tree

api/package-lock.json

Lines changed: 281 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
"API",
1212
"Environments"
1313
],
14-
"main": "./out/main.js",
15-
"types": "./out/main.d.ts",
14+
"exports": {
15+
"import": {
16+
"types": "./out/esm/main.d.ts",
17+
"default": "./out/esm/main.mjs"
18+
},
19+
"require": {
20+
"types": "./out/cjs/main.d.ts",
21+
"default": "./out/cjs/main.cjs"
22+
}
23+
},
1624
"engines": {
1725
"node": ">=22.21.1",
1826
"vscode": "^1.110.0"
@@ -30,12 +38,15 @@
3038
"prepublishOnly": "echo \"⛔ Can only publish from a secure pipeline ⛔\" && node -e \"process.exitCode = 1\"",
3139
"prepack": "npm run all:publish",
3240
"all:publish": "git clean -xfd . && npm install && npm run compile",
33-
"compile": "tsc -b ./tsconfig.json",
41+
"compile": "npm run compile:esm && npm run compile:cjs",
42+
"compile:esm": "tsc -b ./tsconfig.esm.json && mve out/esm/main.js out/esm/main.mjs",
43+
"compile:cjs": "tsc -b ./tsconfig.cjs.json && mve out/cjs/main.js out/cjs/main.cjs",
3444
"clean": "node -e \"const fs = require('fs'); fs.rmSync('./out', { recursive: true, force: true });\""
3545
},
3646
"devDependencies": {
3747
"@types/node": "^22.0.0",
3848
"@types/vscode": "^1.99.0",
49+
"mve": "^0.1.2",
3950
"typescript": "^5.1.3"
4051
}
4152
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
{
22
"compilerOptions": {
33
"target": "ES2020",
4-
"module": "commonjs",
54
"lib": ["ES2020"],
65
"declaration": true,
76
"strict": true,
8-
"outDir": "./out",
97
"rootDir": "src",
108
"esModuleInterop": true,
119
"skipLibCheck": true,

api/tsconfig.cjs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "./out/cjs"
6+
}
7+
}

api/tsconfig.esm.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "esnext",
5+
"outDir": "./out/esm"
6+
}
7+
}

0 commit comments

Comments
 (0)