Closed robhogan closed 1 week ago
This pull request was exported from Phabricator. Differential Revision: D65536715
This pull request was exported from Phabricator. Differential Revision: D65536715
This pull request was exported from Phabricator. Differential Revision: D65536715
This pull request was exported from Phabricator. Differential Revision: D65536715
This pull request has been merged in facebook/metro@578a02debac12bf825fcfb01a26a83f0f0d3ed84.
Summary: Currently, when using
experimentalImportPlugin
(import-export-plugin
) withoutbabel-plugin-transform-module-commonjs
, the following completely legal ESM has broken exports and broken internal behaviour:Because it transforms to this, where
var exports
does not throw (asconst
orlet
would for an already-bound name) but it does shadow the injectedexports
argument, so that no assignment to the injectedexports
is ever made (in other words, a consumer would see no defined exports). In fact, an assignment to the localexports
is made where it shouldn't be.Moreover, in a configuration where we don't transform
const
/let
tovar
(eg targeting web or server), this actually throws at runtime, as would binding any ofmodule
,global
orrequire
.The solution is to rename bindings to names that would be fine in ESM but conflict with CommonJS names / Metro's module factory
The analogue in the (roughly) corresponding Babel plugin is here: https://github.com/babel/babel/blob/38d26cd5eeb66b697671cfb8c78f963f02992073/packages/babel-plugin-transform-modules-commonjs/src/index.ts#L198-L204
(Note that we do not inject
__dirname
or__filename
in Metro, but we do injectglobal
)Changelog:
Differential Revision: D65536715