Skip to content

Commit eab5de2

Browse files
committed
fix: ship dist/index.cjs as a thin wrapper around dist/index.mjs
The CJS build bundled a second full copy of the library (47.9 kB) next to the ESM one. dist/index.cjs is now src/cjs/index.cjs copied verbatim by tsup's publicDir: it require()s dist/index.mjs (Node >= 22.12 loads ESM synchronously) and returns the default export, keeping the historical module.exports = fixHeaders shape. This matches @cldmv/vitest-runner. The build is also minified like vitest-runner's, with keepNames so fixHeaders.name and stack traces stay readable. Published sizes: dist/index.cjs 1.2 kB, dist/index.mjs 24.0 kB, bin/fix-headers.mjs 30.4 kB (were 47.9 / 46.4 / 56.2 kB).
1 parent f611eb4 commit eab5de2

5 files changed

Lines changed: 50 additions & 52 deletions

File tree

‎.configs/tsconfig.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"forceConsistentCasingInFileNames": true
1818
},
1919
"include": ["../src/**/*.mjs", "../src/*.mjs"],
20-
"exclude": ["../node_modules", "../tests", "../coverage", "../src/index.cjs.mjs"]
20+
"exclude": ["../node_modules", "../tests", "../coverage"]
2121
}

‎src/cjs/index.cjs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @Project: @cldmv/fix-headers
3+
* @Filename: /src/cjs/index.cjs
4+
* @Date: 2026-09-28 06:56:09 -07:00 (1790603769)
5+
* @Author: Nate Corcoran <CLDMV>
6+
* @Email: <[email protected]>
7+
* -----
8+
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
9+
* @Last modified time: 2026-09-28 06:56:09 -07:00 (1790603769)
10+
* -----
11+
* @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12+
*/
13+
14+
/**
15+
* @fileoverview CommonJS entry point — a thin, synchronous wrapper around the ESM build.
16+
*
17+
* Node's `require()` loads an ES module synchronously and returns its namespace, as
18+
* long as that module has no top-level await (dist/index.mjs has none). The wrapper
19+
* returns the default export, keeping the historical `module.exports = fixHeaders`
20+
* shape, instead of tsup bundling a second full copy of the library for CJS.
21+
*
22+
* tsup copies this folder verbatim into dist/ (`publicDir` in tsup.config.mjs); it
23+
* never passes through esbuild, so it stays this small however much the library grows.
24+
* @module fix-headers/cjs-entry
25+
*/
26+
"use strict";
27+
28+
module.exports = require("./index.mjs").default;

‎src/index.cjs.mjs‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎tests/integration.module.test.vitest.mjs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { pathToFileURL } from "node:url";
2121
import { build } from "tsup";
2222
import tsupConfigs from "../tsup.config.mjs";
2323
import fixHeadersDefault, { fixHeaders } from "../src/fix-header.mjs";
24-
import cjsEntryDefault from "../src/index.cjs.mjs";
2524
import { discoverFiles } from "../src/core/file-discovery.mjs";
2625
import { fixHeaders as coreFixHeaders } from "../src/core/fix-headers.mjs";
2726
import { detectProjectFromMarkers, resolveProjectMetadata } from "../src/detect/project.mjs";
@@ -76,7 +75,6 @@ describe("module integration and coverage", () => {
7675
await initializeGitWorkspace(workspace);
7776

7877
expect(fixHeadersDefault).toBe(fixHeaders);
79-
expect(cjsEntryDefault).toBe(fixHeaders);
8078

8179
const result = await fixHeaders({
8280
cwd: workspace,
@@ -110,6 +108,13 @@ describe("module integration and coverage", () => {
110108
expect(typeof esmModule.default).toBe("function");
111109
expect(esmModule.fixHeaders).toBe(esmModule.default);
112110

111+
// dist/index.cjs is the thin src/cjs/index.cjs wrapper copied verbatim, not a second
112+
// bundle of the library: it must stay tiny and only delegate to dist/index.mjs.
113+
const cjsSource = await readFile(join(outRoot, "dist", "index.cjs"), "utf8");
114+
expect(cjsSource).toBe(await readFile(new URL("../src/cjs/index.cjs", import.meta.url), "utf8"));
115+
expect(cjsSource).toContain('require("./index.mjs").default');
116+
expect(Buffer.byteLength(cjsSource)).toBeLessThan(2048);
117+
113118
// `require()` must return the function itself (historical CJS shape), and an ESM
114119
// `import()` of the CJS build sees a function as its default export. (Not asserted
115120
// identical: vitest loads CJS through its own module runner, a separate instance.)

‎tsup.config.mjs‎

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
/**
1515
* @fileoverview Bundler config — produces the published dist/ and bin/ output from source.
1616
*
17-
* Three entries:
17+
* Two builds plus one copied file:
1818
* - ESM API → dist/index.mjs — `fixHeaders` named export plus the default export.
19-
* - CJS API → dist/index.cjs — built from src/index.cjs.mjs, which exposes only the
20-
* default export; with `cjsInterop` that becomes `module.exports = fixHeaders`, the
21-
* same shape the old hand-written index.cjs shim had, so `require()` callers keep
22-
* getting the function itself.
19+
* - CJS API → dist/index.cjs — not built: src/cjs/index.cjs is a thin wrapper that
20+
* `require()`s dist/index.mjs and returns its default export (`module.exports =
21+
* fixHeaders`, the historical shape). tsup copies it into the ESM build's outDir via
22+
* `publicDir`, so it follows any outDir override and never duplicates the library.
2323
* - CLI → bin/fix-headers.mjs — ESM only, bundled from src/cli.mjs. bin/ is the
2424
* stable published path (package.json "bin") but is a build artifact like dist/ — not
2525
* tracked in git. esbuild keeps the entry's shebang, so the output stays executable.
2626
*
27-
* Runtime dependencies (package.json "dependencies") stay external. Sourcemaps are
28-
* generated for local debugging and excluded from the tarball via package.json "files".
29-
* Each output is one self-contained file (no shared chunks). Nothing here cleans the
30-
* output folders because the three builds run in parallel and two share dist/; the
31-
* `build` script clears dist/ and bin/ before invoking tsup instead.
27+
* Runtime dependencies (package.json "dependencies") stay external. Output is minified;
28+
* sourcemaps are generated for local debugging and excluded from the tarball via
29+
* package.json "files". Each output is one self-contained file (no shared chunks).
30+
* Nothing here cleans the output folders; the `build` script clears dist/ and bin/
31+
* before invoking tsup instead.
3232
*/
3333
import { defineConfig } from "tsup";
3434

@@ -40,35 +40,23 @@ const shared = {
4040
removeNodeProtocol: false,
4141
sourcemap: true,
4242
dts: false,
43-
minify: false
43+
minify: true,
44+
// Keep function and class names through minification (`fixHeaders.name`, stack traces).
45+
keepNames: true
4446
};
4547

4648
export default defineConfig([
4749
{
4850
...shared,
4951
entry: { index: "src/fix-header.mjs" },
5052
format: ["esm"],
53+
publicDir: "src/cjs",
5154
outDir: "dist",
5255
clean: false,
5356
outExtension() {
5457
return { js: ".mjs" };
5558
}
5659
},
57-
{
58-
...shared,
59-
entry: { index: "src/index.cjs.mjs" },
60-
format: ["cjs"],
61-
cjsInterop: true,
62-
// tsup only records a CJS chunk's exports (which cjsInterop needs) when the
63-
// CJS output is produced via its splitting path; with one entry there are no
64-
// extra chunks, so this changes nothing else about the output.
65-
splitting: true,
66-
outDir: "dist",
67-
clean: false,
68-
outExtension() {
69-
return { js: ".cjs" };
70-
}
71-
},
7260
{
7361
...shared,
7462
entry: { "fix-headers": "src/cli.mjs" },

0 commit comments

Comments
 (0)