-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Consider the commonjs module indicator as a module indicator #18490
Conversation
… is effectively an external module
function foo(name) { | ||
var s = require("t/" + name); | ||
} | ||
define("a", ["require", "exports"], function (require, exports) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
It's also worth noting that our current output is actually correctly handled by including |
This is then a bigger change than originally intended. |
@mhegazy 👍 👎 Punt? Disable |
src/compiler/utilities.ts
Outdated
@@ -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; |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so what u think?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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)...
There was a problem hiding this comment.
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?
@mhegazy Accept like so? 👍 👎 |
within the module transformer. This causes us to pick up some js files with
require
orexport.
as modules now, as can be seen in the baseline changes.Fixes #17192