Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions .changeset/nine-snails-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"bob-the-bundler": major
---

Change the exports map again, to please TypeScript commonjs :)

This is a major breaking change as it requires adjusting your `package.json` exports map.

The `require` entries file extension must be changed from `.d.ts` to `.d.cts`.

```diff
{
"exports": {
".": {
"require": {
- "types": "./dist/typings/index.d.ts",
+ "types": "./dist/typings/index.d.cts"
}
}
}
}
```
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ jobs:
run: yarn install
- name: Run Tests
run: yarn test
- name: Run TS Smoke Tests
run: yarn test:ts
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"prepublish": "yarn build",
"build": "rimraf dist && tsc",
"test": "jest",
"test:ts": "node test/ts-tests/run-tests.mjs",
"prerelease": "yarn build",
"release": "changeset publish",
"release:canary": "node scripts/canary-release.js && yarn build && yarn changeset publish --tag alpha"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const presetFields = Object.freeze({
exports: {
".": {
require: {
types: "./dist/typings/index.d.ts",
types: "./dist/typings/index.d.cts",
default: "./dist/cjs/index.js",
},
import: {
Expand Down
24 changes: 24 additions & 0 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ async function build({
join(distPath, "cjs", "package.json"),
JSON.stringify({ type: "commonjs" })
);
// We need to provide .cjs extension type definitions as well :)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is crazy

// https://github.com/ardatan/graphql-tools/discussions/4581#discussioncomment-3329673

const declarations = await globby("**/*.d.ts", {
cwd: getBuildPath("cjs"),
absolute: false,
ignore: filesToExcludeFromDist,
});
await Promise.all(
declarations.map((filePath) =>
limit(async () => {
const contents = await fse.readFile(
join(getBuildPath("cjs"), filePath),
"utf-8"
);
await fse.writeFile(
join(distPath, "typings", filePath.replace(/\.d\.ts/, ".d.cts")),
contents
.replace(/\.js";\n/g, `.cjs";\n`)
.replace(/\.js';\n/g, `.cjs';\n`)
);
})
)
);
}

// move the package.json to dist
Expand Down
6 changes: 3 additions & 3 deletions test/__fixtures__/simple-monorepo/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.ts",
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
Expand All @@ -23,7 +23,7 @@
},
"./*": {
"require": {
"types": "./dist/typings/*.d.ts",
"types": "./dist/typings/*.d.cts",
"default": "./dist/cjs/*.js"
},
"import": {
Expand All @@ -45,4 +45,4 @@
"access": "public"
},
"type": "module"
}
}
4 changes: 2 additions & 2 deletions test/__fixtures__/simple-monorepo/packages/b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.ts",
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
Expand All @@ -24,7 +24,7 @@
},
"./foo": {
"require": {
"types": "./dist/typings/foo.d.ts",
"types": "./dist/typings/foo.d.cts",
"default": "./dist/cjs/foo.js"
},
"import": {
Expand Down
4 changes: 2 additions & 2 deletions test/__fixtures__/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.ts",
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
Expand All @@ -23,7 +23,7 @@
},
"./*": {
"require": {
"types": "./dist/typings/*.d.ts",
"types": "./dist/typings/*.d.cts",
"default": "./dist/cjs/*.js"
},
"import": {
Expand Down
2 changes: 2 additions & 0 deletions test/__fixtures__/simple/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const someNumber = 1;

export default "kek";
16 changes: 10 additions & 6 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ it("can bundle a simple project", async () => {
exports.__esModule = true;
exports.someNumber = void 0;
exports.someNumber = 1;
exports[\\"default\\"] = \\"kek\\";
"
`);
expect(fse.readFileSync(indexDtsFilePath, "utf8")).toMatchInlineSnapshot(`
"export declare const someNumber = 1;
declare const _default: \\"kek\\";
export default _default;
"
`);
expect(fse.readFileSync(indexMjsFilePath, "utf8")).toMatchInlineSnapshot(`
"export var someNumber = 1;
export default \\"kek\\";
"
`);
expect(fse.readFileSync(readmeFilePath, "utf8")).toMatchInlineSnapshot(`
Expand All @@ -57,7 +61,7 @@ it("can bundle a simple project", async () => {
\\"exports\\": {
\\".\\": {
\\"require\\": {
\\"types\\": \\"./typings/index.d.ts\\",
\\"types\\": \\"./typings/index.d.cts\\",
\\"default\\": \\"./cjs/index.js\\"
},
\\"import\\": {
Expand All @@ -71,7 +75,7 @@ it("can bundle a simple project", async () => {
},
\\"./*\\": {
\\"require\\": {
\\"types\\": \\"./typings/*.d.ts\\",
\\"types\\": \\"./typings/*.d.cts\\",
\\"default\\": \\"./cjs/*.js\\"
},
\\"import\\": {
Expand Down Expand Up @@ -166,7 +170,7 @@ it("can build a monorepo project", async () => {
\\"exports\\": {
\\".\\": {
\\"require\\": {
\\"types\\": \\"./typings/index.d.ts\\",
\\"types\\": \\"./typings/index.d.cts\\",
\\"default\\": \\"./cjs/index.js\\"
},
\\"import\\": {
Expand All @@ -180,7 +184,7 @@ it("can build a monorepo project", async () => {
},
\\"./*\\": {
\\"require\\": {
\\"types\\": \\"./typings/*.d.ts\\",
\\"types\\": \\"./typings/*.d.cts\\",
\\"default\\": \\"./cjs/*.js\\"
},
\\"import\\": {
Expand Down Expand Up @@ -256,7 +260,7 @@ it("can build a monorepo project", async () => {
\\"exports\\": {
\\".\\": {
\\"require\\": {
\\"types\\": \\"./typings/index.d.ts\\",
\\"types\\": \\"./typings/index.d.cts\\",
\\"default\\": \\"./cjs/index.js\\"
},
\\"import\\": {
Expand All @@ -270,7 +274,7 @@ it("can build a monorepo project", async () => {
},
\\"./foo\\": {
\\"require\\": {
\\"types\\": \\"./typings/foo.d.ts\\",
\\"types\\": \\"./typings/foo.d.cts\\",
\\"default\\": \\"./cjs/foo.js\\"
},
\\"import\\": {
Expand Down
1 change: 1 addition & 0 deletions test/ts-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixture.js
4 changes: 4 additions & 0 deletions test/ts-tests/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import kek from "simple";
import { someNumber } from "simple";

console.log(`${kek} ${someNumber}`);
3 changes: 3 additions & 0 deletions test/ts-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
28 changes: 28 additions & 0 deletions test/ts-tests/run-tests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import execa from "execa";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function run(commandStr) {
const [command, ...args] = commandStr.split(" ");
await execa(command, args);
}

async function main() {
await run(`rm -rf ${__dirname}/node_modules`);
await run(`mkdir -p ${__dirname}/node_modules`);
await run(
`ln -s ${__dirname}/../__fixtures__/simple/dist ${__dirname}/node_modules/simple`
);

await run(`yarn tsc --project ${__dirname}/tsconfig.esnext-node.json`);
await run(`yarn tsc --project ${__dirname}/tsconfig.commonjs-node.json`);
await run(`yarn tsc --project ${__dirname}/tsconfig.commonjs-node16.json`);
await run(`yarn tsc --project ${__dirname}/tsconfig.commonjs-nodenext.json`);
await run(`yarn tsc --project ${__dirname}/tsconfig.node16-node16.json`);
await run(`yarn tsc --project ${__dirname}/tsconfig.nodenext-nodenext.json`);
}

main();
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.commonjs-node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "CommonJS",
"moduleResolution": "Node"
}
}
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.commonjs-node16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "CommonJS",
"moduleResolution": "Node16"
}
}
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.commonjs-nodenext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "CommonJS",
"moduleResolution": "NodeNext"
}
}
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.esnext-node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "ESNext",
"moduleResolution": "Node"
}
}
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.node16-node16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "Node16",
"moduleResolution": "Node16"
}
}
8 changes: 8 additions & 0 deletions test/ts-tests/tsconfig.nodenext-nodenext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["fixture.ts"],
"compilerOptions": {
"strict": true,
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}