Skip to content

Commit

Permalink
chore: Add type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Jul 26, 2024
1 parent e0b5457 commit 19a2d55
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
"url": "https://github.com/btmills"
},
"type": "module",
"main": "src/index.js",
"main": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"import": {
"default": "./src/index.js"
"default": "./dist/esm/index.js"
}
},
"files": [
"src"
"dist"
],
"publishConfig": {
"access": "public"
Expand All @@ -34,18 +35,24 @@
],
"scripts": {
"lint": "eslint .",
"build:dedupe-types": "node tools/dedupe-types.js dist/esm/index.js",
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json",
"prepare": "node ./npm-prepare.cjs",
"test": "c8 mocha \"tests/**/*.test.js\" --timeout 30000"
},
"devDependencies": {
"@eslint/core": "^0.2.0",
"@eslint/js": "^9.4.0",
"@types/eslint": "^9.6.0",
"c8": "^10.1.2",
"chai": "^5.1.1",
"eslint": "^9.4.0",
"eslint-config-eslint": "^11.0.0",
"globals": "^15.1.0",
"mocha": "^10.6.0"
"mocha": "^10.6.0",
"rollup": "^4.19.0",
"rollup-plugin-copy": "^3.5.0",
"typescript": "^5.5.4"
},
"dependencies": {
"mdast-util-from-markdown": "^2.0.1"
Expand Down
10 changes: 10 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
input: "src/index.js",
output: [
{
file: "dist/esm/index.js",
format: "esm",
banner: '// @ts-self-types="./index.d.ts"'
}
]
};
43 changes: 43 additions & 0 deletions tools/dedupe-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @fileoverview Strips typedef aliases from the rolled-up file. This
* is necessary because the TypeScript compiler throws an error when
* it encounters a duplicate typedef.
*
* Usage:
* node scripts/strip-typedefs.js filename1.js filename2.js ...
*
* @author Nicholas C. Zakas
*/

//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------

import fs from "node:fs";

//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------

// read files from the command line
const files = process.argv.slice(2);

files.forEach(filePath => {
const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/gu);
const typedefs = new Set();

const remainingLines = lines.filter(line => {
if (!line.startsWith("/** @typedef {import")) {
return true;
}

if (typedefs.has(line)) {
return false;
}

typedefs.add(line);
return true;
});

fs.writeFileSync(filePath, remainingLines.join("\n"), "utf8");
});
4 changes: 4 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"files": ["dist/esm/index.js"]
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"files": ["src/index.js"],
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"allowJs": true,
"checkJs": true,
"outDir": "dist/esm",
"target": "ES2022",
"moduleResolution": "NodeNext",
"module": "NodeNext"
}
}

0 comments on commit 19a2d55

Please sign in to comment.