Skip to content

Commit

Permalink
fix: export types for typescript (#78)
Browse files Browse the repository at this point in the history
When TypeScript's module system is set to `NodeNext` (as [recommended](https://www.typescriptlang.org/tsconfig#module)), the types cannot be found because they are not present in the export map.

This change fixes the problem by adding the types to the export map.  Additionally:

- it reconfigures the "types" test fixture to:
    - use `strict` mode
    - `module: NodeNext`
    - enables lib checking (thus validating `env.d.ts`)
    - runs `tsc --build` on `npm install`
- updates `types.test.js` accordingly
- upgrades `typescript` to latest
- fix `pkg.test.mjs` to correctly reference the updated export map
  • Loading branch information
boneskull authored Aug 15, 2023
1 parent 86a983d commit 81348f2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
"module": "dist/env.mjs",
"types": "dist/env.d.ts",
"exports": {
"require": "./dist/env.cjs.js",
"import": "./dist/env.mjs"
"require": {
"default": "./dist/env.cjs.js",
"types": "./dist/env.d.ts"
},
"import": {
"default": "./dist/env.mjs",
"types": "./dist/env.d.ts"
}
},
"files": [
"dist"
Expand Down Expand Up @@ -57,7 +63,7 @@
"mocha": "8.4.0",
"rollup": "1.32.1",
"rollup-plugin-babel-minify": "9.0.0",
"typescript": "4.7.4",
"typescript": "5.1.6",
"yorkie": "2.0.0"
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/typescript-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc"
"prepare": "tsc -b"
},
"keywords": [],
"author": "",
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/typescript-project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"compilerOptions": {}
"compilerOptions": {
"skipLibCheck": false,
"module": "NodeNext",
"strict": true,
},
"files": ["index.ts"]
}
2 changes: 1 addition & 1 deletion tests/pkg.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fs from "fs";

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8"));
const url = new URL("../" + pkg.exports.import, import.meta.url);
const url = new URL("../" + pkg.exports.import.default, import.meta.url);

import(url).then(({ Env }) => {
new Env();
Expand Down
2 changes: 1 addition & 1 deletion tests/types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/* eslint-disable no-console */

const { execSync } = require("child_process");
execSync("cd tests/fixtures/typescript-project && npm i && tsc --showConfig index.ts");
execSync("cd tests/fixtures/typescript-project && npm i");
console.log("env.d.ts load: success");

0 comments on commit 81348f2

Please sign in to comment.