Skip to content

Commit c472455

Browse files
committed
Silence CJSOnlyExportsDefault in node10/node16-cjs
1 parent 64b1f48 commit c472455

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

packages/cli/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"tsc": "tsc -b",
3434
"local:install": "npm install -g .",
3535
"local:uninstall": "npm uninstall -g @arethetypeswrong/cli",
36-
"pretest": "tsc -b test",
37-
"test": "node --test test/dist",
36+
"test": "tsc -b test && node --test test/dist",
3837
"prepack": "pnpm tsc"
3938
},
4039
"type": "module",

packages/cli/test/snapshots/[email protected]

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Build tools:
1212
🤨 CommonJS module simulates a default export with exports.default and exports.__esModule, but does not also set module.exports for compatibility with Node. Node, and some bundlers under certain conditions (https://andrewbranch.github.io/interop-test/#synthesizing-default-exports-for-cjs-modules), do not respect the __esModule marker, so accessing the intended default export will require a .default property access on the default import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSOnlyExportsDefault.md
1313
1414
15-
┌────────────────────┬───────────────────────┬───────────────────────┬───────────────────────┬───────────────────────┐
16-
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
17-
├────────────────────┼───────────────────────┼───────────────────────┼───────────────────────┼───────────────────────┤
18-
│ "node-html-parser" │ 🤨 CJS default export │ 🤨 CJS default export │ 🤨 CJS default export │ 🤨 CJS default export │
19-
└────────────────────┴───────────────────────┴───────────────────────┴───────────────────────┴───────────────────────┘
15+
┌────────────────────┬───────────────────────────┬───────────────────────┬───────────────────────┐
16+
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
17+
├────────────────────┼───────────────────────────┼───────────────────────┼───────────────────────┤
18+
│ "node-html-parser" │ 🟢 │ 🟢 (CJS) │ 🤨 CJS default export │ 🤨 CJS default export │
19+
└────────────────────┴───────────────────────────┴───────────────────────┴───────────────────────┘
2020
2121
2222
```

packages/core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
},
2020
"scripts": {
2121
"tsc": "tsc",
22-
"pretest": "tsc -b test",
23-
"test": "node --test test/dist",
22+
"test": "tsc -b test && node --test test/dist",
2423
"snapshot": "node scripts/createSnapshotFixture.js",
2524
"prepack": "pnpm tsc"
2625
},

packages/core/src/internal/checks/cjsOnlyExportsDefault.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ export default defineCheck({
66
dependencies: ({ entrypoints, subpath, resolutionKind }) => {
77
const entrypoint = entrypoints[subpath].resolutions[resolutionKind];
88
const implementationFileName = entrypoint.implementationResolution?.fileName;
9-
return [implementationFileName];
9+
return [implementationFileName, resolutionKind];
1010
},
11-
execute: ([implementationFileName], context) => {
11+
execute: ([implementationFileName, resolutionKind], context) => {
1212
if (!implementationFileName) {
1313
return;
1414
}
15+
if (resolutionKind === "node10" || resolutionKind === "node16-cjs") {
16+
// Here, we have a CJS file (most likely transpiled ESM) resolving to a
17+
// CJS transpiled ESM file. This is fine when considered in isolation.
18+
// The pattern of having `module.exports.default = ...` is a problem
19+
// primarily because ESM-detected files in Node (and the same files in
20+
// Webpack/esbuild) will treat `module.exports` as the default export,
21+
// which is both unexpected and different from Babel-style interop seen
22+
// in transpiled default imports and most bundler scenarios. But if Node,
23+
// Webpack, and esbuild never see this file, then it's fine. So, while
24+
// the problematic pattern is a feature of the file alone, the bad outcome
25+
// comes from a combination of the file and the module system that imports
26+
// it. For dual packages that point Node imports and bundlers to a true
27+
// ESM default export, while pointing requires to this CJS "default export,"
28+
// we don't want to report a problem.
29+
//
30+
// TODO: It would be nice to report this information *somehow*, as neutral
31+
// metadata attached to the file (c.f. `Analysis["programInfo"]`).
32+
return;
33+
}
1534
const host = context.hosts.findHostForFiles([implementationFileName]) ?? context.hosts.bundler;
1635
const sourceFile = host.getSourceFile(implementationFileName)!;
1736
if (

packages/core/test/snapshots/[email protected]

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@
8181
"/node_modules/node-html-parser/dist/valid.d.ts",
8282
"/node_modules/node-html-parser/dist/index.d.ts"
8383
],
84-
"visibleProblems": [
85-
0
86-
]
84+
"visibleProblems": []
8785
},
8886
"node16-cjs": {
8987
"name": ".",
@@ -158,9 +156,7 @@
158156
"/node_modules/node-html-parser/dist/valid.d.ts",
159157
"/node_modules/node-html-parser/dist/index.d.ts"
160158
],
161-
"visibleProblems": [
162-
0
163-
]
159+
"visibleProblems": []
164160
},
165161
"node16-esm": {
166162
"name": ".",
@@ -342,7 +338,7 @@
342338
"/node_modules/node-html-parser/dist/index.d.ts"
343339
],
344340
"visibleProblems": [
345-
0
341+
1
346342
]
347343
}
348344
},
@@ -414,6 +410,12 @@
414410
"bundler": {}
415411
},
416412
"problems": [
413+
{
414+
"kind": "CJSOnlyExportsDefault",
415+
"fileName": "/node_modules/node-html-parser/dist/index.js",
416+
"pos": 1077,
417+
"end": 1092
418+
},
417419
{
418420
"kind": "CJSOnlyExportsDefault",
419421
"fileName": "/node_modules/node-html-parser/dist/index.js",

packages/history/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"scripts": {
3030
"tsc": "tsc -b",
3131
"build:scripts": "tsc -b scripts",
32-
"pregenerate": "pnpm build:scripts",
33-
"generate": "tsx scripts/generateFull.ts",
32+
"generate": "pnpm build:scripts && tsx scripts/generateFull.ts",
3433
"prepublishOnly": "pnpm tsc && pnpm generate"
3534
},
3635
"dependencies": {

0 commit comments

Comments
 (0)