* Only bind module.exports if no local definition exists Note that this uses `lookupSymbolForNameWorker`, which is really a best-effort check since it only knows about symbols that it has already encountered. As a side-effect, even when `module` is bound as part of a `module.exports` reference, it only declares it once instead of one declaration per reference. * Only type module.exports inside module files It is an error inside script files, but the binder sometimes creates a ModuleExports symbol because we doesn't know whether we have a commonjs module until after binding is done. * Only bind module.exports in a commonjs module Note that this, too, is a best-effort check since evidence of commonjs-ness may be found after a *reference* to module.exports. (A reference to module.exports alone is not enough evidence that a file is commonjs. It has to have an assignment to it.)
27 lines
568 B
TypeScript
27 lines
568 B
TypeScript
// @noEmit: true
|
|
// @allowJs: true
|
|
// @checkJs: true
|
|
// @Filename: webpackLibNormalModule.js
|
|
class C {
|
|
/** @param {number} x */
|
|
constructor(x) {
|
|
this.x = x
|
|
this.exports = [x]
|
|
}
|
|
/** @param {number} y */
|
|
m(y) {
|
|
return this.x + y
|
|
}
|
|
}
|
|
function exec() {
|
|
const module = new C(12);
|
|
return module.exports; // should be fine because `module` is defined locally
|
|
}
|
|
|
|
function tricky() {
|
|
// (a trickier variant of what webpack does)
|
|
const module = new C(12);
|
|
return () => {
|
|
return module.exports;
|
|
}
|
|
}
|