Skip to content

Commit f685733

Browse files
authored
fix: commonjs imports (#118)
* fix: commonjs imports * :) :) :) * add ts smoke tests * this is not a test lul * add more module resolution variations * better changeset
1 parent f947893 commit f685733

20 files changed

+153
-14
lines changed

.changeset/nine-snails-speak.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
"bob-the-bundler": major
3+
---
4+
5+
Change the exports map again, to please TypeScript commonjs :)
6+
7+
This is a major breaking change as it requires adjusting your `package.json` exports map.
8+
9+
The `require` entries file extension must be changed from `.d.ts` to `.d.cts`.
10+
11+
```diff
12+
{
13+
"exports": {
14+
".": {
15+
"require": {
16+
- "types": "./dist/typings/index.d.ts",
17+
+ "types": "./dist/typings/index.d.cts"
18+
}
19+
}
20+
}
21+
}
22+
```

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ jobs:
3636
run: yarn install
3737
- name: Run Tests
3838
run: yarn test
39+
- name: Run TS Smoke Tests
40+
run: yarn test:ts

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"prepublish": "yarn build",
5050
"build": "rimraf dist && tsc",
5151
"test": "jest",
52+
"test:ts": "node test/ts-tests/run-tests.mjs",
5253
"prerelease": "yarn build",
5354
"release": "changeset publish",
5455
"release:canary": "node scripts/canary-release.js && yarn build && yarn changeset publish --tag alpha"

src/commands/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const presetFields = Object.freeze({
2121
exports: {
2222
".": {
2323
require: {
24-
types: "./dist/typings/index.d.ts",
24+
types: "./dist/typings/index.d.cts",
2525
default: "./dist/cjs/index.js",
2626
},
2727
import: {

src/commands/build.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ async function build({
274274
join(distPath, "cjs", "package.json"),
275275
JSON.stringify({ type: "commonjs" })
276276
);
277+
// We need to provide .cjs extension type definitions as well :)
278+
// https://github.com/ardatan/graphql-tools/discussions/4581#discussioncomment-3329673
279+
280+
const declarations = await globby("**/*.d.ts", {
281+
cwd: getBuildPath("cjs"),
282+
absolute: false,
283+
ignore: filesToExcludeFromDist,
284+
});
285+
await Promise.all(
286+
declarations.map((filePath) =>
287+
limit(async () => {
288+
const contents = await fse.readFile(
289+
join(getBuildPath("cjs"), filePath),
290+
"utf-8"
291+
);
292+
await fse.writeFile(
293+
join(distPath, "typings", filePath.replace(/\.d\.ts/, ".d.cts")),
294+
contents
295+
.replace(/\.js";\n/g, `.cjs";\n`)
296+
.replace(/\.js';\n/g, `.cjs';\n`)
297+
);
298+
})
299+
)
300+
);
277301
}
278302

279303
// move the package.json to dist

test/__fixtures__/simple-monorepo/packages/a/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"exports": {
1010
".": {
1111
"require": {
12-
"types": "./dist/typings/index.d.ts",
12+
"types": "./dist/typings/index.d.cts",
1313
"default": "./dist/cjs/index.js"
1414
},
1515
"import": {
@@ -23,7 +23,7 @@
2323
},
2424
"./*": {
2525
"require": {
26-
"types": "./dist/typings/*.d.ts",
26+
"types": "./dist/typings/*.d.cts",
2727
"default": "./dist/cjs/*.js"
2828
},
2929
"import": {
@@ -45,4 +45,4 @@
4545
"access": "public"
4646
},
4747
"type": "module"
48-
}
48+
}

test/__fixtures__/simple-monorepo/packages/b/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"exports": {
1111
".": {
1212
"require": {
13-
"types": "./dist/typings/index.d.ts",
13+
"types": "./dist/typings/index.d.cts",
1414
"default": "./dist/cjs/index.js"
1515
},
1616
"import": {
@@ -24,7 +24,7 @@
2424
},
2525
"./foo": {
2626
"require": {
27-
"types": "./dist/typings/foo.d.ts",
27+
"types": "./dist/typings/foo.d.cts",
2828
"default": "./dist/cjs/foo.js"
2929
},
3030
"import": {

test/__fixtures__/simple/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"exports": {
1010
".": {
1111
"require": {
12-
"types": "./dist/typings/index.d.ts",
12+
"types": "./dist/typings/index.d.cts",
1313
"default": "./dist/cjs/index.js"
1414
},
1515
"import": {
@@ -23,7 +23,7 @@
2323
},
2424
"./*": {
2525
"require": {
26-
"types": "./dist/typings/*.d.ts",
26+
"types": "./dist/typings/*.d.cts",
2727
"default": "./dist/cjs/*.js"
2828
},
2929
"import": {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const someNumber = 1;
2+
3+
export default "kek";

test/integration.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ it("can bundle a simple project", async () => {
2626
exports.__esModule = true;
2727
exports.someNumber = void 0;
2828
exports.someNumber = 1;
29+
exports[\\"default\\"] = \\"kek\\";
2930
"
3031
`);
3132
expect(fse.readFileSync(indexDtsFilePath, "utf8")).toMatchInlineSnapshot(`
3233
"export declare const someNumber = 1;
34+
declare const _default: \\"kek\\";
35+
export default _default;
3336
"
3437
`);
3538
expect(fse.readFileSync(indexMjsFilePath, "utf8")).toMatchInlineSnapshot(`
3639
"export var someNumber = 1;
40+
export default \\"kek\\";
3741
"
3842
`);
3943
expect(fse.readFileSync(readmeFilePath, "utf8")).toMatchInlineSnapshot(`
@@ -57,7 +61,7 @@ it("can bundle a simple project", async () => {
5761
\\"exports\\": {
5862
\\".\\": {
5963
\\"require\\": {
60-
\\"types\\": \\"./typings/index.d.ts\\",
64+
\\"types\\": \\"./typings/index.d.cts\\",
6165
\\"default\\": \\"./cjs/index.js\\"
6266
},
6367
\\"import\\": {
@@ -71,7 +75,7 @@ it("can bundle a simple project", async () => {
7175
},
7276
\\"./*\\": {
7377
\\"require\\": {
74-
\\"types\\": \\"./typings/*.d.ts\\",
78+
\\"types\\": \\"./typings/*.d.cts\\",
7579
\\"default\\": \\"./cjs/*.js\\"
7680
},
7781
\\"import\\": {
@@ -166,7 +170,7 @@ it("can build a monorepo project", async () => {
166170
\\"exports\\": {
167171
\\".\\": {
168172
\\"require\\": {
169-
\\"types\\": \\"./typings/index.d.ts\\",
173+
\\"types\\": \\"./typings/index.d.cts\\",
170174
\\"default\\": \\"./cjs/index.js\\"
171175
},
172176
\\"import\\": {
@@ -180,7 +184,7 @@ it("can build a monorepo project", async () => {
180184
},
181185
\\"./*\\": {
182186
\\"require\\": {
183-
\\"types\\": \\"./typings/*.d.ts\\",
187+
\\"types\\": \\"./typings/*.d.cts\\",
184188
\\"default\\": \\"./cjs/*.js\\"
185189
},
186190
\\"import\\": {
@@ -256,7 +260,7 @@ it("can build a monorepo project", async () => {
256260
\\"exports\\": {
257261
\\".\\": {
258262
\\"require\\": {
259-
\\"types\\": \\"./typings/index.d.ts\\",
263+
\\"types\\": \\"./typings/index.d.cts\\",
260264
\\"default\\": \\"./cjs/index.js\\"
261265
},
262266
\\"import\\": {
@@ -270,7 +274,7 @@ it("can build a monorepo project", async () => {
270274
},
271275
\\"./foo\\": {
272276
\\"require\\": {
273-
\\"types\\": \\"./typings/foo.d.ts\\",
277+
\\"types\\": \\"./typings/foo.d.cts\\",
274278
\\"default\\": \\"./cjs/foo.js\\"
275279
},
276280
\\"import\\": {

0 commit comments

Comments
 (0)