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:
Wesley Wigham 2017-11-09 16:49:04 -08:00 committed by GitHub
parent 1d2db09af2
commit 16efae2433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 2 deletions

View File

@ -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;
}

View File

@ -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 */

View 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;

View 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))

View 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

View 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;