Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 16efae2

Browse files
authoredNov 10, 2017
Consider the commonjs module indicator as a module indicator (microsoft#18490)
* Consider the commonjs module indicator as an indicator that something is effectively an external module * Only use commonjs module indicator when targeting commonjs
1 parent 1d2db09 commit 16efae2

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed
 

‎src/compiler/transformers/module/module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts {
5757
* @param node The SourceFile node.
5858
*/
5959
function transformSourceFile(node: SourceFile) {
60-
if (node.isDeclarationFile || !(isExternalModule(node) || compilerOptions.isolatedModules || node.transformFlags & TransformFlags.ContainsDynamicImport)) {
60+
if (node.isDeclarationFile || !(isEffectiveExternalModule(node, compilerOptions) || node.transformFlags & TransformFlags.ContainsDynamicImport)) {
6161
return node;
6262
}
6363

‎src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ namespace ts {
461461
}
462462

463463
export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
464-
return isExternalModule(node) || compilerOptions.isolatedModules;
464+
return isExternalModule(node) || compilerOptions.isolatedModules || ((getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS) && !!node.commonJsModuleIndicator);
465465
}
466466

467467
/* @internal */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [index.js]
2+
class Foo {}
3+
4+
class Bar extends Foo {}
5+
6+
module.exports = Bar;
7+
8+
9+
//// [index.js]
10+
var tslib_1 = require("tslib");
11+
var Foo = /** @class */ (function () {
12+
function Foo() {
13+
}
14+
return Foo;
15+
}());
16+
var Bar = /** @class */ (function (_super) {
17+
tslib_1.__extends(Bar, _super);
18+
function Bar() {
19+
return _super !== null && _super.apply(this, arguments) || this;
20+
}
21+
return Bar;
22+
}(Foo));
23+
module.exports = Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.js ===
2+
class Foo {}
3+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
4+
5+
class Bar extends Foo {}
6+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
7+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
8+
9+
module.exports = Bar;
10+
>module : Symbol(export=, Decl(index.js, 2, 24))
11+
>exports : Symbol(export=, Decl(index.js, 2, 24))
12+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/index.js ===
2+
class Foo {}
3+
>Foo : Foo
4+
5+
class Bar extends Foo {}
6+
>Bar : Bar
7+
>Foo : Foo
8+
9+
module.exports = Bar;
10+
>module.exports = Bar : typeof Bar
11+
>module.exports : any
12+
>module : any
13+
>exports : any
14+
>Bar : typeof Bar
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @allowJS: true
2+
// @outDir: ./out
3+
// @module: commonjs
4+
// @noEmitHelpers: true
5+
// @importHelpers: true
6+
// @filename: index.js
7+
class Foo {}
8+
9+
class Bar extends Foo {}
10+
11+
module.exports = Bar;

0 commit comments

Comments
 (0)
Please sign in to comment.