Don’t emit __esModule on CJS files that didn’t use ESM syntax (#58169)

This commit is contained in:
Andrew Branch 2024-04-12 12:20:09 -07:00 committed by GitHub
parent b006768548
commit 6431a30761
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 101 additions and 2 deletions

View File

@ -56,6 +56,7 @@ import {
getOriginalNodeId,
getStrictOptionValue,
getTextOfIdentifierOrLiteral,
hasJSFileExtension,
hasJsonModuleEmitEnabled,
hasSyntacticModifier,
Identifier,
@ -243,6 +244,9 @@ export function transformModule(context: TransformationContext): (x: SourceFile
}
function shouldEmitUnderscoreUnderscoreESModule() {
if (hasJSFileExtension(currentSourceFile.fileName) && currentSourceFile.commonJsModuleIndicator && (!currentSourceFile.externalModuleIndicator || currentSourceFile.externalModuleIndicator === true)) {
return false;
}
if (!currentModuleInfo.exportEquals && isExternalModule(currentSourceFile)) {
return true;
}

View File

@ -35,7 +35,6 @@ const a = {};
module.exports = a;
//// [file.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// cjs format file
const a = {};
module.exports = a;

View File

@ -35,7 +35,6 @@ const a = {};
module.exports = a;
//// [file.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// cjs format file
const a = {};
module.exports = a;

View File

@ -0,0 +1,27 @@
/3.cjs(2,1): error TS2304: Cannot find name 'exports'.
/5.cjs(2,8): error TS1192: Module '"/3"' has no default export.
==== /1.cjs (0 errors) ====
module.exports = {};
==== /2.cjs (0 errors) ====
exports.foo = 0;
==== /3.cjs (1 errors) ====
import "foo";
exports.foo = {};
~~~~~~~
!!! error TS2304: Cannot find name 'exports'.
==== /4.cjs (0 errors) ====
;
==== /5.cjs (1 errors) ====
import two from "./2.cjs"; // ok
import three from "./3.cjs"; // error
~~~~~
!!! error TS1192: Module '"/3"' has no default export.
two.foo;
three.foo;

View File

@ -0,0 +1,47 @@
//// [tests/cases/conformance/node/nodeModulesCJSEmit1.ts] ////
//// [1.cjs]
module.exports = {};
//// [2.cjs]
exports.foo = 0;
//// [3.cjs]
import "foo";
exports.foo = {};
//// [4.cjs]
;
//// [5.cjs]
import two from "./2.cjs"; // ok
import three from "./3.cjs"; // error
two.foo;
three.foo;
//// [1.cjs]
"use strict";
module.exports = {};
//// [2.cjs]
"use strict";
exports.foo = 0;
//// [3.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("foo");
exports.foo = {};
//// [4.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
;
//// [5.cjs]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const _2_cjs_1 = __importDefault(require("./2.cjs")); // ok
const _3_cjs_1 = __importDefault(require("./3.cjs")); // error
_2_cjs_1.default.foo;
_3_cjs_1.default.foo;

View File

@ -0,0 +1,23 @@
// @module: nodenext
// @checkJs: true
// @outDir: dist
// @noTypesAndSymbols: true
// @Filename: /1.cjs
module.exports = {};
// @Filename: /2.cjs
exports.foo = 0;
// @Filename: /3.cjs
import "foo";
exports.foo = {};
// @Filename: /4.cjs
;
// @Filename: /5.cjs
import two from "./2.cjs"; // ok
import three from "./3.cjs"; // error
two.foo;
three.foo;