Skip to content

Commit

Permalink
Delete Meta-only __jsResource support
Browse files Browse the repository at this point in the history
Summary:
Changelog: Internal

Removes the Meta-only `jsResource` feature from `collectDependencies`. This is one of the prerequisites for open sourcing a stable version of the `asyncRequire` API for dev-mode bundle splitting.

Reviewed By: robhogan

Differential Revision: D42801122

fbshipit-source-id: cf42be56d4b9024dd1a74eb7e00c11f246e05e07
  • Loading branch information
motiz88 authored and facebook-github-bot committed Feb 27, 2023
1 parent f1d905b commit 69c8fc7
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 73 deletions.
1 change: 0 additions & 1 deletion packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ const compileToBytecode = (
const disabledDependencyTransformer: DependencyTransformer = {
transformSyncRequire: () => void 0,
transformImportCall: () => void 0,
transformJSResource: () => void 0,
transformPrefetch: () => void 0,
transformIllegalDynamicRequire: () => void 0,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,22 +858,6 @@ it('distinguishes sync and async dependencies on the same module; 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: objectContaining({asyncType: 'async'})},
{name: 'asyncRequire', data: objectContaining({asyncType: null})},
]);
expect(codeFromAst(ast)).toEqual(
comparableCode(`
require(${dependencyMapName}[1], "asyncRequire").resource(${dependencyMapName}[0], "some/async/module", _dependencyMap.paths);
`),
);
});

describe('import() prefetching', () => {
it('collects prefetch calls', () => {
const ast = astFromCode(`
Expand Down Expand Up @@ -1116,7 +1100,7 @@ it('records locations of dependencies', () => {
import * as d from 'do';
import type {s} from 'setup/something';
import('some/async/module').then(foo => {});
__jsResource('some/async/module');
require('foo'); __prefetchImport('baz');
Expand Down Expand Up @@ -1146,12 +1130,8 @@ it('records locations of dependencies', () => {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #2 (setup/something)
> 4 | import('some/async/module').then(foo => {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #3 (some/async/module)
> 5 | __jsResource('some/async/module');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #3 (some/async/module)
> 4 | import('some/async/module').then(foo => {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #4 (asyncRequire)
> 5 | __jsResource('some/async/module');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #4 (asyncRequire)
> 8 | require('foo'); __prefetchImport('baz');
| ^^^^^^^^^^^^^^^^^^^^^^^^ dep #4 (asyncRequire)
> 8 | require('foo'); __prefetchImport('baz');
Expand Down Expand Up @@ -1347,7 +1327,6 @@ it('uses the dependency transformer specified in the options to transform the de
export {Banana} from 'Banana';
import("some/async/module").then(foo => {});
__jsResource("some/async/module");
__prefetchImport("some/async/module");
`);

Expand All @@ -1364,7 +1343,6 @@ it('uses the dependency transformer specified in the options to transform the de
import b from 'b/lib/b';
export { Banana } from 'Banana';
require("asyncRequire").async(_dependencyMap[3], "some/async/module").then(foo => {});
require("asyncRequire").jsresource(_dependencyMap[3], "some/async/module");
require("asyncRequire").prefetch(_dependencyMap[4], "some/async/module");
`),
);
Expand All @@ -1377,7 +1355,6 @@ it('uses the dependency registry specified in the options to register dependenci
export {Banana} from 'Banana';
import("some/async/module").then(foo => {});
__jsResource("a/jsresouce");
__prefetchImport("a/prefetch/module");
`);

Expand All @@ -1392,7 +1369,6 @@ it('uses the dependency registry specified in the options to register dependenci
{name: 'b/lib/b', data: objectContaining({asyncType: null})},
{name: 'Banana', data: objectContaining({asyncType: null})},
{name: 'some/async/module', data: objectContaining({asyncType: 'async'})},
{name: 'a/jsresouce', data: objectContaining({asyncType: 'async'})},
{
name: 'a/prefetch/module',
data: objectContaining({asyncType: 'prefetch'}),
Expand Down Expand Up @@ -1495,7 +1471,6 @@ class MockModuleDependencyRegistry implements ModuleDependencyRegistry {
// Mock transformer for dependencies. Uses a "readable" format
// require() -> require(id, module name)
// import() -> require(async moudle name).async(id, module name)
// jsresource -> require(async moudle name).jsresource(id, module name)
// prefetch -> require(async moudle name).prefetch(id, module name)
const MockDependencyTransformer: DependencyTransformer = {
transformSyncRequire(
Expand All @@ -1519,14 +1494,6 @@ const MockDependencyTransformer: DependencyTransformer = {
transformAsyncRequire(path, dependency, state, 'async');
},

transformJSResource(
path: NodePath<>,
dependency: InternalDependency,
state: State,
): void {
transformAsyncRequire(path, dependency, state, 'jsresource');
},

transformPrefetch(
path: NodePath<>,
dependency: InternalDependency,
Expand Down
39 changes: 1 addition & 38 deletions packages/metro/src/ModuleGraph/worker/collectDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {isImport} = types;

type ImportDependencyOptions = $ReadOnly<{
asyncType: AsyncDependencyType,
jsResource?: boolean,
}>;

export type Dependency = $ReadOnly<{
Expand Down Expand Up @@ -122,11 +121,6 @@ export interface DependencyTransformer {
dependency: InternalDependency,
state: State,
): void;
transformJSResource(
path: NodePath<>,
dependency: InternalDependency,
state: State,
): void;
transformPrefetch(
path: NodePath<>,
dependency: InternalDependency,
Expand Down Expand Up @@ -192,14 +186,6 @@ function collectDependencies(
return;
}

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

// Match `require.context`
if (
// Feature gate, defaults to `false`.
Expand Down Expand Up @@ -480,9 +466,7 @@ function processImportCall(

const transformer = state.dependencyTransformer;

if (options.jsResource) {
transformer.transformJSResource(path, dep, state);
} else if (options.asyncType === 'async') {
if (options.asyncType === 'async') {
transformer.transformImportCall(path, dep, state);
} else {
transformer.transformPrefetch(path, dep, state);
Expand Down Expand Up @@ -649,10 +633,6 @@ const makeAsyncPrefetchTemplate = template.expression(`
require(ASYNC_REQUIRE_MODULE_PATH).prefetch(MODULE_ID, MODULE_NAME, DEPENDENCY_MAP.paths)
`);

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

const makeResolveWeakTemplate = template.expression(`
MODULE_ID
`);
Expand Down Expand Up @@ -693,23 +673,6 @@ const DefaultDependencyTransformer: DependencyTransformer = {
);
},

transformJSResource(
path: NodePath<>,
dependency: InternalDependency,
state: State,
): void {
path.replaceWith(
makeJSResourceTemplate({
ASYNC_REQUIRE_MODULE_PATH: nullthrows(
state.asyncRequireModulePathStringLiteral,
),
MODULE_ID: createModuleIDExpression(dependency, state),
MODULE_NAME: createModuleNameLiteral(dependency),
DEPENDENCY_MAP: nullthrows(state.dependencyMapIdentifier),
}),
);
},

transformPrefetch(
path: NodePath<>,
dependency: InternalDependency,
Expand Down

0 comments on commit 69c8fc7

Please sign in to comment.