Skip to content

Commit

Permalink
Fix _interopRequireDefault not defined error
Browse files Browse the repository at this point in the history
Summary:
D25175301 (279b295) stopped cloning the `AST` in the transform worker. Not cloning the AST seems to cause problems if ES Modules are lowered to CommonJS when not using`experimentalImportSupport`.

I don't know exactly why it's causing problems but noticed in another place where I tried to stop cloning the AST nodes that Babel messes up its cache (of scopes) when the same AST tree is traversed multiple times.

Another explanation could be that a plugin is doing something funky or we hold on to an AST node that shouldn't be changed but now gets changed because the AST isn't cloned.

Re-enabling cloning slighlty increase the overall build time (~2s for a 80s build)

Reviewed By: nikoant

Differential Revision: D26749029

fbshipit-source-id: 9dbceb8fe60054a9e7785bf625104fbe6042c57c
  • Loading branch information
Micha Reiser authored and facebook-github-bot committed Mar 2, 2021
1 parent bfd76e7 commit 63f3d1e
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {transformFromAstSync} = require('@babel/core');
const {stableHash} = require('metro-cache');
const types = require('@babel/types');
const countLines = require('metro/src/lib/countLines');
const nullthrows = require('nullthrows');

const {
fromRawMappings,
Expand Down Expand Up @@ -370,18 +371,25 @@ module.exports = {

plugins.push([metroTransformPlugins.inlinePlugin, opts]);

transformFromAstSync(ast, '', {
ast: true,
babelrc: false,
code: false,
configFile: false,
comments: false,
compact: false,
filename,
plugins,
sourceMaps: false,
cloneInputAst: false,
});
ast = nullthrows(
transformFromAstSync(ast, '', {
ast: true,
babelrc: false,
code: false,
configFile: false,
comments: false,
compact: false,
filename,
plugins,
sourceMaps: false,
// Not-Cloning the input AST here should be safe because other code paths above this call
// are mutating the AST as well and no code is depending on the original AST.
// However, switching the flag to false caused issues with ES Modules if `experimentalImportSupport` isn't used https://github.com/facebook/metro/issues/641
// either because one of the plugins is doing something funky or Babel messes up some caches.
// Make sure to test the above mentioned case before flipping the flag back to false.
cloneInputAst: true,
}).ast,
);

let dependencyMapName = '';
let dependencies;
Expand Down

0 comments on commit 63f3d1e

Please sign in to comment.