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

fix: make plugin-kit types usable in CommonJS #143

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/plugin-kit/build-cts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { readFile, writeFile } from "node:fs/promises";
fasttime marked this conversation as resolved.
Show resolved Hide resolved

const oldSourceText = await readFile("dist/esm/index.d.ts", "utf-8");
const newSourceText = oldSourceText.replaceAll(
'import("./types.ts")',
'import("./types.ts", { with: { "resolution-mode": "import" } })',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious: Will this work with TypeScript 4? And if not, does it matter?

Copy link
Member Author

@fasttime fasttime Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will not work because TypeScript 4 doesn't support import attributes. The alternative would be renaming types.ts to types.cts in dist/cjs and updating the imports in index.d.cts accordingly.

Update:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think renaming to types.cts and updating the references seems like a safer option for best compatibility. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I switched to importing from types.cts in 38bc8e9. We don't have policy about TypeScript versions support like we have for Node.js versions, but since the import attributes proposal is still not finalized, types.cts looks like a safer choice.

);
await writeFile("dist/cjs/index.d.cts", newSourceText);
2 changes: 1 addition & 1 deletion packages/plugin-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"homepage": "https://github.com/eslint/rewrite#readme",
"scripts": {
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
"build:cts": "node -e \"fs.writeFileSync('dist/cjs/index.d.cts', fs.readFileSync('dist/esm/index.d.ts', 'utf-8').replaceAll('import(\\\"./types.ts\\\")', 'import(\\\"./types.ts\\\", { with: { \\\"resolution-mode\\\": \\\"import\\\" } })'))\"",
"build:cts": "node ./build-cts.js",
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
"pretest": "npm run build",
"test": "mocha tests/",
Expand Down
Loading