Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider the commonjs module indicator as a module indicator #18490

Merged
merged 2 commits into from
Nov 10, 2017

Conversation

weswigham
Copy link
Member

within the module transformer. This causes us to pick up some js files with require or export. as modules now, as can be seen in the baseline changes.

Fixes #17192

function foo(name) {
var s = require("t/" + name);
}
define("a", ["require", "exports"], function (require, exports) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. this does not look good. we should not mess up the module output if we can.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what to do though.. @rbuckton thoughts?

Copy link
Member Author

@weswigham weswigham Oct 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has an explicit @module: amd BTW (this could be seen as an improvement). Our module emit and our global emit for commonjs are pretty much identical - the only difference is in how the typechecker sees the top-level scope.

@weswigham
Copy link
Member Author

It's also worth noting that our current output is actually correctly handled by including tslib with the output, as is evident from discussion here.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 11, 2017

This is then a bigger change than originally intended.

@weswigham
Copy link
Member Author

@mhegazy 👍 👎 Punt? Disable commonjsModuleIndicator flag collection when module is not commonjs?

@@ -444,7 +444,7 @@ namespace ts {
}

export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
return isExternalModule(node) || compilerOptions.isolatedModules;
return isExternalModule(node) || compilerOptions.isolatedModules || !!node.commonJsModuleIndicator;
Copy link
Contributor

@mhegazy mhegazy Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like the best compromise here:
(!!node.commonJsModuleIndicator && getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what u think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems reasonable. Though anyone with mixed-mode compilations targeting commonjs where they use require in some global files is going to get sad when their global files start getting transpiled as modules. Though why we even support mixed mode compilations in the first place...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other option is to do nothing and leave the current behavior.. which is broken on import helpers + js files (which could be a smaller group affected than by the proposed change)...

Copy link
Member Author

@weswigham weswigham Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmmmmm I think once limited to commonjs it's likely fine. The only difference other than helpers, which is what this aims to change, for module vs non-module would be that top level declarations are local to the file instead of global. And given that the commonjs module flag is only set in JS files, it's not like there'll be any potential interface or namespace merging going on; so it'd just be weather people expected the var x they declared in one file with a require to be visible in another file with a require..... except that's governed by isExternalModule and not isEffectiveExternalModule (since the latter only affects emit), so I'm pretty sure once limited to commonjs there's no obserable changes?

@weswigham
Copy link
Member Author

@mhegazy Accept like so? 👍 👎

@weswigham weswigham merged commit 16efae2 into microsoft:master Nov 10, 2017
@weswigham weswigham deleted the import-helpers-in-commonjs branch November 10, 2017 00:49
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants