mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 12:32:08 -06:00
Don’t emit __esModule on CJS files that didn’t use ESM syntax (#58169)
This commit is contained in:
parent
b006768548
commit
6431a30761
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
27
tests/baselines/reference/nodeModulesCJSEmit1.errors.txt
Normal file
27
tests/baselines/reference/nodeModulesCJSEmit1.errors.txt
Normal 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;
|
||||
|
||||
47
tests/baselines/reference/nodeModulesCJSEmit1.js
Normal file
47
tests/baselines/reference/nodeModulesCJSEmit1.js
Normal 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;
|
||||
23
tests/cases/conformance/node/nodeModulesCJSEmit1.ts
Normal file
23
tests/cases/conformance/node/nodeModulesCJSEmit1.ts
Normal 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;
|
||||
Loading…
x
Reference in New Issue
Block a user