Skip to content

Regression with "metro" 0.65.0+: _interopRequireDefault is not defined #641

Closed

Description

@nikoant

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Metro 0.65.0 (and 0.65.1) produces an invalid bundle for the following code:
helloWorld.js:

export default function () { console.log('hello world'); }

index.js:

import helloWorld from './helloWorld';
helloWorld();

node bundle.js throws error:

ReferenceError: _interopRequireDefault is not defined

With "metro" 0.64.0 or below there is no such issue. I've compared bundled produced by both versions and it looks like in 0.65.1 bodies are not bundled for both interopRequireDefault and helloWorld functions.

0.65.1:

var __BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),__DEV__=false,process=this.process||{},__METRO_GLOBAL_PREFIX__='';process.env=process.env||{};process.env.NODE_ENV=process.env.NODE_ENV||"production";
!(function(r){"use strict";r.__r=o,r["".concat(__METRO_GLOBAL_PREFIX__,"__d")]=function(r,i,n){if(null!=e[i])return;var o={dependencyMap:n,factory:r,hasError:!1,importedAll:t,importedDefault:t,isInitialized:!1,publicModule:{exports:{}}};e[i]=o},r.__c=n,r.__registerSegment=function(r,t,i){s[r]=t,i&&i.forEach(function(t){e[t]||v.has(t)||v.set(t,r)})};var e=n(),t={},i={}.hasOwnProperty;function n(){return e=Object.create(null)}function o(r){var t=r,i=e[t];return i&&i.isInitialized?i.publicModule.exports:d(t,i)}function l(r){var i=r;if(e[i]&&e[i].importedDefault!==t)return e[i].importedDefault;var n=o(i),l=n&&n.__esModule?n.default:n;return e[i].importedDefault=l}function u(r){var n=r;if(e[n]&&e[n].importedAll!==t)return e[n].importedAll;var l,u=o(n);if(u&&u.__esModule)l=u;else{if(l={},u)for(var a in u)i.call(u,a)&&(l[a]=u[a]);l.default=u}return e[n].importedAll=l}o.importDefault=l,o.importAll=u;var a=!1;function d(e,t){if(!a&&r.ErrorUtils){var i;a=!0;try{i=h(e,t)}catch(e){r.ErrorUtils.reportFatalError(e)}return a=!1,i}return h(e,t)}var c=16,f=65535;function p(r){return{segmentId:r>>>c,localId:r&f}}o.unpackModuleId=p,o.packModuleId=function(r){return(r.segmentId<<c)+r.localId};var s=[],v=new Map;function h(t,i){if(!i&&s.length>0){var n,a=null!==(n=v.get(t))&&void 0!==n?n:0,d=s[a];null!=d&&(d(t),i=e[t],v.delete(t))}var c=r.nativeRequire;if(!i&&c){var f=p(t),h=f.segmentId;c(f.localId,h),i=e[t]}if(!i)throw Error('Requiring unknown module "'+t+'".');if(i.hasError)throw _(t,i.error);i.isInitialized=!0;var m=i,g=m.factory,I=m.dependencyMap;try{var M=i.publicModule;return M.id=t,g(r,o,l,u,M,M.exports,I),i.factory=void 0,i.dependencyMap=void 0,M.exports}catch(r){throw i.hasError=!0,i.error=r,i.isInitialized=!1,i.publicModule.exports=void 0,r}}function _(r,e){return Error('Requiring module "'+r+'", which threw an exception: '+e)}})('undefined'!=typeof globalThis?globalThis:'undefined'!=typeof global?global:'undefined'!=typeof window?window:this);
__d(function(g,r,i,a,m,e,d){"use strict";(0,_interopRequireDefault(r(d[0])).default)()},0,[1]);
__d(function(g,r,i,a,m,e,d){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=_default},1,[]);
__r(0);

0.64.0:

var __BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),__DEV__=false,process=this.process||{},__METRO_GLOBAL_PREFIX__='';process.env=process.env||{};process.env.NODE_ENV=process.env.NODE_ENV||"production";
!(function(r){"use strict";r.__r=o,r["".concat(__METRO_GLOBAL_PREFIX__,"__d")]=function(r,i,n){if(null!=e[i])return;var o={dependencyMap:n,factory:r,hasError:!1,importedAll:t,importedDefault:t,isInitialized:!1,publicModule:{exports:{}}};e[i]=o},r.__c=n,r.__registerSegment=function(r,t,i){s[r]=t,i&&i.forEach(function(t){e[t]||v.has(t)||v.set(t,r)})};var e=n(),t={},i={}.hasOwnProperty;function n(){return e=Object.create(null)}function o(r){var t=r,i=e[t];return i&&i.isInitialized?i.publicModule.exports:d(t,i)}function l(r){var i=r;if(e[i]&&e[i].importedDefault!==t)return e[i].importedDefault;var n=o(i),l=n&&n.__esModule?n.default:n;return e[i].importedDefault=l}function u(r){var n=r;if(e[n]&&e[n].importedAll!==t)return e[n].importedAll;var l,u=o(n);if(u&&u.__esModule)l=u;else{if(l={},u)for(var a in u)i.call(u,a)&&(l[a]=u[a]);l.default=u}return e[n].importedAll=l}o.importDefault=l,o.importAll=u;var a=!1;function d(e,t){if(!a&&r.ErrorUtils){var i;a=!0;try{i=h(e,t)}catch(e){r.ErrorUtils.reportFatalError(e)}return a=!1,i}return h(e,t)}var c=16,f=65535;function p(r){return{segmentId:r>>>c,localId:r&f}}o.unpackModuleId=p,o.packModuleId=function(r){return(r.segmentId<<c)+r.localId};var s=[],v=new Map;function h(t,i){if(!i&&s.length>0){var n,a=null!==(n=v.get(t))&&void 0!==n?n:0,d=s[a];null!=d&&(d(t),i=e[t],v.delete(t))}var c=r.nativeRequire;if(!i&&c){var f=p(t),h=f.segmentId;c(f.localId,h),i=e[t]}if(!i)throw Error('Requiring unknown module "'+t+'".');if(i.hasError)throw _(t,i.error);i.isInitialized=!0;var m=i,g=m.factory,I=m.dependencyMap;try{var M=i.publicModule;return M.id=t,g(r,o,l,u,M,M.exports,I),i.factory=void 0,i.dependencyMap=void 0,M.exports}catch(r){throw i.hasError=!0,i.error=r,i.isInitialized=!1,i.publicModule.exports=void 0,r}}function _(r,e){return Error('Requiring module "'+r+'", which threw an exception: '+e)}})('undefined'!=typeof globalThis?globalThis:'undefined'!=typeof global?global:'undefined'!=typeof window?window:this);
__d(function(g,r,i,a,m,e,d){"use strict";var t;(0,((t=r(d[0]))&&t.__esModule?t:{default:t}).default)()},0,[1]);
__d(function(g,r,i,a,m,e,d){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(){console.log('hello world')}},1,[]);
__r(0);

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

https://github.com/nikoant/metro-0.65-bug-repro

What is the expected behavior?

The produced bundle should run without any errors.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
Metro 0.65.1, yarn 1.22.4, macOS 10.15.7

Activity

  1. MichaReiser commented on Mar 2, 2021

    @MichaReiser

    Thanks @nikoant for reporting this bug. I tried to run the repo locally but it failed with:

     BUNDLE  src/index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1)Error: Import statement may only appear at top level in file src/index.js at 4:2
        at minifyCode (/Users/mreiser/git/metro-0.65-bug-repro/node_modules/metro-transform-worker/src/index.js:95:13)
        at Object.transform (/Users/mreiser/git/metro-0.65-bug-repro/node_modules/metro-transform-worker/src/index.js:348:30)
        at processTicksAndRejections (node:internal/process/task_queues:94:5)
    error Command failed with exit code 1.
    

    Because ES Modules are not lowered to CommonJS. One can then either enable experimentalImportSupport in the metro.config.js which works

    module.exports = {
         "transformer": {
            getTransformOptions() {
                return {
                    transform: {
                        experimentalImportSupport: true,
                        inlineRequires: false,
                    }
                };
            },
         }
    }

    or add a babel.config.js which allows your reported issue

    {
        "presets": [],
        "plugins": [
            "@babel/plugin-transform-modules-commonjs"
        ]
      }
    
  2. MichaReiser commented on Mar 2, 2021

    @MichaReiser

    I narrowed the issue down to 279b295, specifically to no longer cloning the input AST. It isn't clear to me why it's causing issues but will go ahead and revert the change.

  3. nikoant commented on Mar 2, 2021

    @nikoant
    Author

    Thanks @nikoant for reporting this bug. I tried to run the repo locally but it failed with:

     BUNDLE  src/index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1)Error: Import statement may only appear at top level in file src/index.js at 4:2
        at minifyCode (/Users/mreiser/git/metro-0.65-bug-repro/node_modules/metro-transform-worker/src/index.js:95:13)
        at Object.transform (/Users/mreiser/git/metro-0.65-bug-repro/node_modules/metro-transform-worker/src/index.js:348:30)
        at processTicksAndRejections (node:internal/process/task_queues:94:5)
    error Command failed with exit code 1.
    

    Because ES Modules are not lowered to CommonJS. One can then either enable experimentalImportSupport in the metro.config.js which works

    module.exports = {
         "transformer": {
            getTransformOptions() {
                return {
                    transform: {
                        experimentalImportSupport: true,
                        inlineRequires: false,
                    }
                };
            },
         }
    }

    or add a babel.config.js which allows your reported issue

    {
        "presets": [],
        "plugins": [
            "@babel/plugin-transform-modules-commonjs"
        ]
      }

    Oops, sorry, in the initial repo I had "@babel/preset-env", but then while trying to narrow it down I removed babel, but was still able to build the bundle. Now I see it was because of Metro cache, because if I call build with "--reset-cache" after removing babel it shows the error you mentioned. I just updated the repo and returned the initial version with babel. However the produced bundle is broken anyway.

  4. nikoant commented on Mar 2, 2021

    @nikoant
    Author

    I narrowed the issue down to 279b295, specifically to no longer cloning the input AST. It isn't clear to me why it's causing issues but will go ahead and revert the change.

    Thanks for looking into this!

  5. MichaReiser commented on Mar 3, 2021

    @MichaReiser

    This issue should now be resolved and I just scheduled the release of 0.65.2.

    Fixed 63f3d1e

  6. added a commit that references this issue on Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions