Skip to content

Commit

Permalink
Add support for JSResource in Metro
Browse files Browse the repository at this point in the history
Reviewed By: cpojer

Differential Revision: D17527390

fbshipit-source-id: 6bf971038ab23d1c56401099bdf6c72fbde5aa73
  • Loading branch information
rubennorte authored and facebook-github-bot committed Sep 30, 2019
1 parent a1caae7 commit 662b416
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ it('collects mixed dependencies as being sync; reverse order', () => {
);
});

it('collects __jsResource calls', () => {
const ast = astFromCode(`
__jsResource("some/async/module");
`);
const {dependencies, dependencyMapName} = collectDependencies(ast, opts);
expect(dependencies).toEqual([
{name: 'some/async/module', data: {isAsync: true}},
{name: 'asyncRequire', data: {isAsync: false}},
]);
expect(codeFromAst(ast)).toEqual(
comparableCode(`
require(${dependencyMapName}[1], "asyncRequire").resource(${dependencyMapName}[0], "some/async/module");
`),
);
});

describe('import() prefetching', () => {
it('collects prefetch calls', () => {
const ast = astFromCode(`
Expand Down
31 changes: 28 additions & 3 deletions packages/metro/src/ModuleGraph/worker/collectDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ opaque type Path = any;

type DepOptions = {|
+prefetchOnly: boolean,
+jsResource?: boolean,
|};

type InternalDependency<D> = {|
Expand Down Expand Up @@ -104,6 +105,10 @@ const makeAsyncPrefetchTemplate = template(`
require(ASYNC_REQUIRE_MODULE_PATH).prefetch(MODULE_ID, MODULE_NAME)
`);

const makeJSResourceTemplate = template(`
require(ASYNC_REQUIRE_MODULE_PATH).resource(MODULE_ID, MODULE_NAME)
`);

/**
* Transform all the calls to `require()` and `import()` in a file into ID-
* independent code, and return the list of dependencies. For example, a call
Expand Down Expand Up @@ -141,12 +146,24 @@ function collectDependencies(
const name = callee.node.name;

if (callee.isImport()) {
processImportCall(path, state, {prefetchOnly: false});
processImportCall(path, state, {
prefetchOnly: false,
});
return;
}

if (name === '__prefetchImport' && !path.scope.getBinding(name)) {
processImportCall(path, state, {prefetchOnly: true});
processImportCall(path, state, {
prefetchOnly: true,
});
return;
}

if (name === '__jsResource' && !path.scope.getBinding(name)) {
processImportCall(path, state, {
prefetchOnly: false,
jsResource: true,
});
return;
}

Expand Down Expand Up @@ -219,7 +236,15 @@ function processImportCall(
);
const MODULE_NAME = types.stringLiteral(name);

if (!options.prefetchOnly) {
if (options.jsResource) {
path.replaceWith(
makeJSResourceTemplate({
ASYNC_REQUIRE_MODULE_PATH,
MODULE_ID,
MODULE_NAME,
}),
);
} else if (!options.prefetchOnly) {
path.replaceWith(
makeAsyncRequireTemplate({
ASYNC_REQUIRE_MODULE_PATH,
Expand Down

0 comments on commit 662b416

Please sign in to comment.