Skip to content

Commit

Permalink
Consider the commonjs module indicator as a module indicator (#18490)
Browse files Browse the repository at this point in the history
* Consider the commonjs module indicator as an indicator that something is effectively an external module

* Only use commonjs module indicator when targeting commonjs
  • Loading branch information
weswigham authored Nov 10, 2017
1 parent 1d2db09 commit 16efae2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/javascriptCommonjsModule.js
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions tests/baselines/reference/javascriptCommonjsModule.symbols
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/baselines/reference/javascriptCommonjsModule.types
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions tests/cases/compiler/javascriptCommonjsModule.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 16efae2

Please sign in to comment.