Skip to content

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed
 

‎src/compiler/transformers/module/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
getOriginalNodeId,
5757
getStrictOptionValue,
5858
getTextOfIdentifierOrLiteral,
59+
hasJSFileExtension,
5960
hasJsonModuleEmitEnabled,
6061
hasSyntacticModifier,
6162
Identifier,
@@ -243,6 +244,9 @@ export function transformModule(context: TransformationContext): (x: SourceFile
243244
}
244245

245246
function shouldEmitUnderscoreUnderscoreESModule() {
247+
if (hasJSFileExtension(currentSourceFile.fileName) && currentSourceFile.commonJsModuleIndicator && (!currentSourceFile.externalModuleIndicator || currentSourceFile.externalModuleIndicator === true)) {
248+
return false;
249+
}
246250
if (!currentModuleInfo.exportEquals && isExternalModule(currentSourceFile)) {
247251
return true;
248252
}

‎tests/baselines/reference/nodeModulesAllowJsExportAssignment(module=node16).js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const a = {};
3535
module.exports = a;
3636
//// [file.js]
3737
"use strict";
38-
Object.defineProperty(exports, "__esModule", { value: true });
3938
// cjs format file
4039
const a = {};
4140
module.exports = a;

‎tests/baselines/reference/nodeModulesAllowJsExportAssignment(module=nodenext).js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const a = {};
3535
module.exports = a;
3636
//// [file.js]
3737
"use strict";
38-
Object.defineProperty(exports, "__esModule", { value: true });
3938
// cjs format file
4039
const a = {};
4140
module.exports = a;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/3.cjs(2,1): error TS2304: Cannot find name 'exports'.
2+
/5.cjs(2,8): error TS1192: Module '"/3"' has no default export.
3+
4+
5+
==== /1.cjs (0 errors) ====
6+
module.exports = {};
7+
8+
==== /2.cjs (0 errors) ====
9+
exports.foo = 0;
10+
11+
==== /3.cjs (1 errors) ====
12+
import "foo";
13+
exports.foo = {};
14+
~~~~~~~
15+
!!! error TS2304: Cannot find name 'exports'.
16+
17+
==== /4.cjs (0 errors) ====
18+
;
19+
20+
==== /5.cjs (1 errors) ====
21+
import two from "./2.cjs"; // ok
22+
import three from "./3.cjs"; // error
23+
~~~~~
24+
!!! error TS1192: Module '"/3"' has no default export.
25+
two.foo;
26+
three.foo;
27+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/conformance/node/nodeModulesCJSEmit1.ts] ////
2+
3+
//// [1.cjs]
4+
module.exports = {};
5+
6+
//// [2.cjs]
7+
exports.foo = 0;
8+
9+
//// [3.cjs]
10+
import "foo";
11+
exports.foo = {};
12+
13+
//// [4.cjs]
14+
;
15+
16+
//// [5.cjs]
17+
import two from "./2.cjs"; // ok
18+
import three from "./3.cjs"; // error
19+
two.foo;
20+
three.foo;
21+
22+
23+
//// [1.cjs]
24+
"use strict";
25+
module.exports = {};
26+
//// [2.cjs]
27+
"use strict";
28+
exports.foo = 0;
29+
//// [3.cjs]
30+
"use strict";
31+
Object.defineProperty(exports, "__esModule", { value: true });
32+
require("foo");
33+
exports.foo = {};
34+
//// [4.cjs]
35+
"use strict";
36+
Object.defineProperty(exports, "__esModule", { value: true });
37+
;
38+
//// [5.cjs]
39+
"use strict";
40+
var __importDefault = (this && this.__importDefault) || function (mod) {
41+
return (mod && mod.__esModule) ? mod : { "default": mod };
42+
};
43+
Object.defineProperty(exports, "__esModule", { value: true });
44+
const _2_cjs_1 = __importDefault(require("./2.cjs")); // ok
45+
const _3_cjs_1 = __importDefault(require("./3.cjs")); // error
46+
_2_cjs_1.default.foo;
47+
_3_cjs_1.default.foo;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @module: nodenext
2+
// @checkJs: true
3+
// @outDir: dist
4+
// @noTypesAndSymbols: true
5+
6+
// @Filename: /1.cjs
7+
module.exports = {};
8+
9+
// @Filename: /2.cjs
10+
exports.foo = 0;
11+
12+
// @Filename: /3.cjs
13+
import "foo";
14+
exports.foo = {};
15+
16+
// @Filename: /4.cjs
17+
;
18+
19+
// @Filename: /5.cjs
20+
import two from "./2.cjs"; // ok
21+
import three from "./3.cjs"; // error
22+
two.foo;
23+
three.foo;

0 commit comments

Comments
 (0)
Please sign in to comment.