Skip to content

Commit

Permalink
build(next-drupal): add .d.cts type definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAlbin committed Nov 15, 2023
1 parent 1be6184 commit 9d5d410
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/next-drupal/.npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/coverage
/CHANGELOG.md
/postBuild.js
/tests
/tsconfig.json
13 changes: 13 additions & 0 deletions packages/next-drupal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.modern.js",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.modern.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"types": "dist/index.d.ts",
"license": "MIT",
"publishConfig": {
Expand All @@ -18,6 +30,7 @@
},
"scripts": {
"prepare": "microbundle --no-compress --jsx React.createElement --format modern,cjs",
"postprepare": "node postBuild.js",
"dev": "microbundle watch --no-compress --jsx React.createElement --format modern,cjs",
"test": "jest"
},
Expand Down
22 changes: 22 additions & 0 deletions packages/next-drupal/postBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readdir, copyFile } from "node:fs/promises"

const files = await readdir("./dist")
const tasks = []
for (const file of files) {
if (file.endsWith(".modern.js")) {
const base = file.replace(/\.modern\.js$/, "")

// Make a duplicate of the type definitions.
//
// From the TypeScript docs:
//
// "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."
//
// @see https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing
tasks.push(copyFile(`./dist/${base}.d.ts`, `./dist/${base}.d.cts`))
}
}
await Promise.all(tasks)
console.log(`Created unique *.d.cts files for CommonJS build.`)

0 comments on commit 9d5d410

Please sign in to comment.