8000 Metro strips imports when import references are transformed with variable shadowing · Issue #1481 · facebook/metro · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Metro strips imports when import references are transformed with variable shadowing #1481
Open
@tjzel

Description

@tjzel

I think I encountered a bug in Metro regarding transformation of module imports.

In Reanimated (Worklets) Babel plugin I'm transforming the following code:

import {foo} from './file.js';

function bar(){
  foo();
};

to

import {foo} from './file.js';

const bar = function factory(foo){
  return function bar(){
    foo();
  };
}(foo);

Basically I'm replacing a FunctionDeclaration node with a VariableDeclaration node. After the replacement I make sure to recrawl the scope so foo in the arguments of factory would be correctly bound to foo from the import.

After these transformations Metro strips the import totally and then foo is not defined and file.js is not loaded into the bundle. Relevant part of Metro output below:

__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
  var bar = function factory(foo) {
    var bar = function bar() {
      foo();
      console.log('bar');
    };
    return bar;
  }(foo);
},0,[],"filefile.js");

As you can see, foo isn't defined anywhere. Calling visit on the ancestor Program node (or any other node) doesn't fix it.

This doesn't happen when I put the already transformed file into Metro.

The reason why I'm quite positive this is a bug is due to fact that renaming foo actually makes the import to be properly transformed. If I transform the original file to:
(note _foo here)

import {foo} from './file.js';

const bar = function factory(_foo){
  return function bar(){
    _foo();
  };
}(foo);

The import is transformed properly, and the Metro output is:

__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
  var bar = function factory(_foo) {
    var bar = function bar() {
      _foo();
    };
    return bar;
  }(_$$_REQUIRE(_dependencyMap[0], "./file.js").foo);
},0,[1],"filefile.js");

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

      0