Skip to content

Commit f8df139

Browse files
authored
fix: improve type support for isolated dependencies in pnpm (#289)
1 parent a902bc4 commit f8df139

File tree

10 files changed

+78
-17
lines changed

10 files changed

+78
-17
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,25 @@ jobs:
112112
run: |
113113
npm run build
114114
npm run test:jsr
115+
116+
pnpm_test:
117+
name: Test pnpm Type Support
118+
runs-on: ubuntu-latest
119+
steps:
120+
- uses: actions/checkout@v5
121+
122+
- uses: pnpm/action-setup@v4
123+
with:
124+
version: latest
125+
126+
- uses: actions/setup-node@v5
127+
with:
128+
node-version: "lts/*"
129+
130+
- name: Install Packages
131+
run: npm install
132+
133+
- name: Run pnpm test
134+
run: |
135+
npm run build
136+
npm run test:pnpm --ws --if-present

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ bun.lockb
142142

143143
# Automatically generated files by GitHub Actions workflow
144144
tools/update-readme.js
145+
146+
# pnpm
147+
pnpm-lock.yaml

packages/config-helpers/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
2929
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
3030
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
31-
"test:jsr": "npx jsr@latest publish --dry-run",
3231
"test": "mocha \"tests/**/*.test.js\"",
3332
"test:coverage": "c8 npm test",
33+
"test:jsr": "npx jsr@latest publish --dry-run",
34+
"test:pnpm": "cd tests/pnpm && pnpm install && pnpm exec tsc",
3435
"test:types": "tsc -p tests/types/tsconfig.json"
3536
},
3637
"repository": {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"@eslint/config-helpers": "file:../..",
5+
"typescript": "^5.9.3"
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig, globalIgnores } from "@eslint/config-helpers";
2+
3+
export const ignoresConfig = globalIgnores([]);
4+
5+
export default defineConfig([ignoresConfig]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"checkJs": true,
4+
"declaration": true,
5+
"module": "nodenext",
6+
"noEmit": true
7+
},
8+
"files": ["test-eslint.config.js"]
9+
}

packages/object-schema/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
"test": "tests"
2626
},
2727
"scripts": {
28+
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
2829
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
29-
"build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts",
30+
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
3031
"test": "mocha \"tests/**/*.test.js\"",
3132
"test:coverage": "c8 npm test",
3233
"test:jsr": "npx jsr@latest publish --dry-run",

tools/build-cts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ if (!newFilename) {
2525

2626
const oldSourceText = await readFile(filename, "utf-8");
2727
const newSourceText = oldSourceText.replaceAll(
28-
'import("./types.ts")',
29-
'import("./types.cts")',
28+
' from "./types.ts";\n',
29+
' from "./types.cts";\n',
3030
);
3131

3232
await writeFile(newFilename, newSourceText);

tools/dedupe-types.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,31 @@ const files = process.argv.slice(2);
2525
files.forEach(filePath => {
2626
const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/gu);
2727
const typedefs = new Set();
28-
29-
const remainingLines = lines.filter(line => {
30-
if (!line.startsWith("/** @typedef {import")) {
31-
return true;
32-
}
33-
34-
if (typedefs.has(line)) {
35-
return false;
28+
const importSources = new Set();
29+
const outputLines = [];
30+
31+
for (const line of lines) {
32+
const match = line.match(
33+
/^(?<start>\/\*\*\s*@typedef\s+\{)import\("(?<importSource>.+?)"\)(?<end>.*)/u,
34+
);
35+
if (!match) {
36+
// not a typedef, so just copy the line
37+
outputLines.push(line);
38+
} else if (!typedefs.has(line)) {
39+
// we haven't seen this typedef before, so process it
40+
typedefs.add(line);
41+
const { start, importSource, end } = match.groups;
42+
const importName = `$${importSource.replace(/\W/gu, "")}`;
43+
if (!importSources.has(importSource)) {
44+
// we haven't seen this import before, so add an @import comment
45+
importSources.add(importSource);
46+
outputLines.push(
47+
`/** @import * as ${importName} from "${importSource}"; */`,
48+
);
49+
}
50+
outputLines.push(`${start}${importName}${end}`);
3651
}
52+
}
3753

38-
typedefs.add(line);
39-
return true;
40-
});
41-
42-
fs.writeFileSync(filePath, remainingLines.join("\n"), "utf8");
54+
fs.writeFileSync(filePath, outputLines.join("\n"), "utf8");
4355
});

tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"allowImportingTsExtensions": true,
34
"allowJs": true,
45
"checkJs": true,
56
"declaration": true,

0 commit comments

Comments
 (0)