diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index f60e50ea36587..0c8e2cda0f5f4 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -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; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 1931c397ae7b1..e67635cba0411 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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 */ diff --git a/tests/baselines/reference/javascriptCommonjsModule.js b/tests/baselines/reference/javascriptCommonjsModule.js new file mode 100644 index 0000000000000..ab517f63805e1 --- /dev/null +++ b/tests/baselines/reference/javascriptCommonjsModule.js @@ -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; diff --git a/tests/baselines/reference/javascriptCommonjsModule.symbols b/tests/baselines/reference/javascriptCommonjsModule.symbols new file mode 100644 index 0000000000000..923e0b3467b9f --- /dev/null +++ b/tests/baselines/reference/javascriptCommonjsModule.symbols @@ -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)) + diff --git a/tests/baselines/reference/javascriptCommonjsModule.types b/tests/baselines/reference/javascriptCommonjsModule.types new file mode 100644 index 0000000000000..88126ce473727 --- /dev/null +++ b/tests/baselines/reference/javascriptCommonjsModule.types @@ -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 + diff --git a/tests/cases/compiler/javascriptCommonjsModule.ts b/tests/cases/compiler/javascriptCommonjsModule.ts new file mode 100644 index 0000000000000..dde30580f70ac --- /dev/null +++ b/tests/cases/compiler/javascriptCommonjsModule.ts @@ -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;