Description
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");