mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
Consider the commonjs module indicator as a module indicator (#18490)
* Consider the commonjs module indicator as an indicator that something is effectively an external module * Only use commonjs module indicator when targeting commonjs
This commit is contained in:
parent
1d2db09af2
commit
16efae2433
@ -57,7 +57,7 @@ namespace ts {
|
||||
* @param node The SourceFile node.
|
||||
*/
|
||||
function transformSourceFile(node: SourceFile) {
|
||||
if (node.isDeclarationFile || !(isExternalModule(node) || compilerOptions.isolatedModules || node.transformFlags & TransformFlags.ContainsDynamicImport)) {
|
||||
if (node.isDeclarationFile || !(isEffectiveExternalModule(node, compilerOptions) || node.transformFlags & TransformFlags.ContainsDynamicImport)) {
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@ -461,7 +461,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
|
||||
return isExternalModule(node) || compilerOptions.isolatedModules;
|
||||
return isExternalModule(node) || compilerOptions.isolatedModules || ((getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS) && !!node.commonJsModuleIndicator);
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
|
||||
23
tests/baselines/reference/javascriptCommonjsModule.js
Normal file
23
tests/baselines/reference/javascriptCommonjsModule.js
Normal file
@ -0,0 +1,23 @@
|
||||
//// [index.js]
|
||||
class Foo {}
|
||||
|
||||
class Bar extends Foo {}
|
||||
|
||||
module.exports = Bar;
|
||||
|
||||
|
||||
//// [index.js]
|
||||
var tslib_1 = require("tslib");
|
||||
var Foo = /** @class */ (function () {
|
||||
function Foo() {
|
||||
}
|
||||
return Foo;
|
||||
}());
|
||||
var Bar = /** @class */ (function (_super) {
|
||||
tslib_1.__extends(Bar, _super);
|
||||
function Bar() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
return Bar;
|
||||
}(Foo));
|
||||
module.exports = Bar;
|
||||
13
tests/baselines/reference/javascriptCommonjsModule.symbols
Normal file
13
tests/baselines/reference/javascriptCommonjsModule.symbols
Normal file
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.js ===
|
||||
class Foo {}
|
||||
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
|
||||
|
||||
class Bar extends Foo {}
|
||||
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
|
||||
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
|
||||
|
||||
module.exports = Bar;
|
||||
>module : Symbol(export=, Decl(index.js, 2, 24))
|
||||
>exports : Symbol(export=, Decl(index.js, 2, 24))
|
||||
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
|
||||
|
||||
15
tests/baselines/reference/javascriptCommonjsModule.types
Normal file
15
tests/baselines/reference/javascriptCommonjsModule.types
Normal file
@ -0,0 +1,15 @@
|
||||
=== tests/cases/compiler/index.js ===
|
||||
class Foo {}
|
||||
>Foo : Foo
|
||||
|
||||
class Bar extends Foo {}
|
||||
>Bar : Bar
|
||||
>Foo : Foo
|
||||
|
||||
module.exports = Bar;
|
||||
>module.exports = Bar : typeof Bar
|
||||
>module.exports : any
|
||||
>module : any
|
||||
>exports : any
|
||||
>Bar : typeof Bar
|
||||
|
||||
11
tests/cases/compiler/javascriptCommonjsModule.ts
Normal file
11
tests/cases/compiler/javascriptCommonjsModule.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// @allowJS: true
|
||||
// @outDir: ./out
|
||||
// @module: commonjs
|
||||
// @noEmitHelpers: true
|
||||
// @importHelpers: true
|
||||
// @filename: index.js
|
||||
class Foo {}
|
||||
|
||||
class Bar extends Foo {}
|
||||
|
||||
module.exports = Bar;
|
||||
Loading…
x
Reference in New Issue
Block a user