Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Generate declarations from JS #564

Merged
merged 9 commits into from
Jun 27, 2024
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ assets/js/prism.js
api/
/dist/
docs/
types/src/
node_modules/
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ node_modules/
!test/*.html
!benchmarks/*.html

api/
# Ignore generated TypeScript files
types/src/

api/
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@
"description": "Let’s get serious about color",
"files": [
"dist/",
"src/"
"src/",
"types/src/",
"types/index.d.ts"
],
"exports": {
".": {
"import": {
"types": "./types/index.d.ts",
"default": "./dist/color.js"
},
"require": {
"types": "./types/index.d.ts",
"default": "./dist/color.cjs"
}
},
"./fn": {
"import": {
"types": "./types/src/index-fn.d.ts",
"default": "./src/index-fn.js"
},
"require": {
"types": "./types/src/index-fn.d.ts",
"default": "./dist/color-fn.cjs"
}
},
"./dist/*": "./dist/*"
},
"typesVersions": {
"*": {
"fn": [
"./types/src/index-fn.d.ts"
]
}
},
"type": "module",
"main": "./dist/color.cjs",
"module": "./dist/color.js",
Expand All @@ -41,11 +54,12 @@
"watch:html": "npx @11ty/eleventy --config=_build/eleventy.js --serve",
"build:js": "rollup -c _build/rollup.config.js",
"watch:js": "rollup -c _build/rollup.config.js --watch",
"build:ts": "tsc && node ./scripts/copy-declarations.js",
MysteryBlokHed marked this conversation as resolved.
Show resolved Hide resolved
"build:apidocs": "npx typedoc",
"watch:apidocs": "npx typedoc --watch",
"build:js:legacy": "rollup -c _build/rollup.legacy.config.js",
"build:space-accessors": "node ./scripts/generate-space-accessor-types.js",
"build": "run-s build:apidocs build:html build:js build:js:legacy build:space-accessors",
"build": "run-s build:apidocs build:html build:js build:ts build:js:legacy build:space-accessors",
"watch": "run-p watch:*",
"prepack": "npm run build",
"release": "release-it"
Expand Down
31 changes: 31 additions & 0 deletions scripts/copy-declarations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copy manually-written `.d.ts` files from `src/` to `types/src/`
import { copyFileSync } from "node:fs";
MysteryBlokHed marked this conversation as resolved.
Show resolved Hide resolved
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const SOURCE = "../src/";
const DEST = "../types/src/";

/**
* `.d.ts` files to copy.
* Paths written relative to the `src/` directory
*/
const TO_COPY = [
"color.d.ts",
"ColorSpace.d.ts",
"hooks.d.ts",
"index.d.ts",
"space-coord-accessors.d.ts",
"space.d.ts",
MysteryBlokHed marked this conversation as resolved.
Show resolved Hide resolved
"types.d.ts",
];

/** Script file directory */
const __dirname = dirname(fileURLToPath(import.meta.url));

MysteryBlokHed marked this conversation as resolved.
Show resolved Hide resolved
for (const file of TO_COPY) {
copyFileSync(
resolve(__dirname, SOURCE, file),
resolve(__dirname, DEST, file),
);
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"checkJs": true,
"lib": ["DOM", "ESNext"],
"skipLibCheck": true,
"noEmit": true
"declaration": true,
"emitDeclarationOnly": true,
"declarationMap": true,
"outDir": "types/src"
},
"include": ["src/**/*.js", "src/**/*.d.ts"],
"exclude": ["node_modules/**"]
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Minimum TypeScript Version: 5.0
export { default } from "./src/index.js";
export * from "./src/index.js";
7 changes: 3 additions & 4 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"paths": {
"colorjs.io": ["../src"],
"colorjs.io/fn": ["../src/index-fn.js"],
"colorjs.io/*": ["../*"]
"colorjs.io": ["."],
"colorjs.io/fn": ["./src/index-fn.js"],
"colorjs.io/*": ["./*"]
}
}
}
Loading