Skip to content

Commit

Permalink
Remove unused collectDependencies extension points
Browse files Browse the repository at this point in the history
Summary:
Changelog: Internal

Removes the `unstable_collectDependenciesPath` top-level config option, as well as the `dependencyRegistry` parameter to `collectDependencies`.

Reviewed By: huntie

Differential Revision: D43477213

fbshipit-source-id: 5f98f25a6fab0632721aa9ec699352685db52182
  • Loading branch information
motiz88 authored and facebook-github-bot committed Mar 20, 2023
1 parent 83a6076 commit 439f0d0
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ Object {
"default": Object {},
},
"unstable_allowRequireContext": false,
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
Expand Down Expand Up @@ -324,7 +323,6 @@ Object {
"default": Object {},
},
"unstable_allowRequireContext": false,
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
Expand Down Expand Up @@ -502,7 +500,6 @@ Object {
"default": Object {},
},
"unstable_allowRequireContext": false,
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
Expand Down Expand Up @@ -680,7 +677,6 @@ Object {
"default": Object {},
},
"unstable_allowRequireContext": false,
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
Expand Down
2 changes: 0 additions & 2 deletions packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
publicPath: '/assets',
allowOptionalDependencies: false,
unstable_allowRequireContext: false,
unstable_collectDependenciesPath:
'metro/src/ModuleGraph/worker/collectDependencies.js',
unstable_dependencyMapReservedName: null,
unstable_disableModuleWrapping: false,
unstable_disableNormalizePseudoGlobals: false,
Expand Down
60 changes: 0 additions & 60 deletions packages/metro-transform-worker/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const baseConfig: JsTransformerConfig = {
minifierPath: 'minifyModulePath',
optimizationSizeLimit: 100000,
publicPath: '/assets',
unstable_collectDependenciesPath:
'metro/src/ModuleGraph/worker/collectDependencies',
unstable_dependencyMapReservedName: null,
unstable_compactOutput: false,
unstable_disableModuleWrapping: false,
Expand Down Expand Up @@ -471,64 +469,6 @@ it('transforms a script to JS source and bytecode', async () => {
).not.toThrow();
});

it('allows replacing the collectDependencies implementation', async () => {
jest.mock(
'metro-transform-worker/__virtual__/collectModifiedDependencies',
() =>
jest.fn((ast, opts) => {
const metroCoreCollectDependencies = jest.requireActual<
(empty, empty) => {dependencies: {map: mixed => mixed}},
>('metro/src/ModuleGraph/worker/collectDependencies');
const collectedDeps = metroCoreCollectDependencies(ast, opts);
return {
...collectedDeps,
// $FlowFixMe[missing-local-annot]
dependencies: collectedDeps.dependencies.map(dep => ({
...dep,
name: 'modified_' + dep.name,
})),
};
}),
{virtual: true},
);

const config = {
...baseConfig,
unstable_collectDependenciesPath:
'metro-transform-worker/__virtual__/collectModifiedDependencies',
};
const options = {
dev: true,
type: 'module',
};
const result = await Transformer.transform(
config,
'/root',
'local/file.js',
Buffer.from('require("foo")', 'utf8'),
// $FlowFixMe[prop-missing] Added when annotating Transformer.
options,
);

// $FlowIgnore[cannot-resolve-module] this is a virtual module
const collectModifiedDependencies = require('metro-transform-worker/__virtual__/collectModifiedDependencies');
expect(collectModifiedDependencies).toHaveBeenCalledWith(
expect.objectContaining({type: 'File'}),
{
allowOptionalDependencies: config.allowOptionalDependencies,
asyncRequireModulePath: config.asyncRequireModulePath,
dynamicRequires: 'reject',
inlineableCalls: ['_$$_IMPORT_DEFAULT', '_$$_IMPORT_ALL'],
keepRequireNames: options.dev,
unstable_allowRequireContext: false,
dependencyMapName: null,
},
);
expect(result.dependencies).toEqual([
expect.objectContaining({name: 'modified_foo'}),
]);
});

it('uses a reserved dependency map name and prevents it from being minified', async () => {
const result = await Transformer.transform(
{...baseConfig, unstable_dependencyMapReservedName: 'THE_DEP_MAP'},
Expand Down
13 changes: 2 additions & 11 deletions packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import type {
DependencyTransformer,
DynamicRequiresBehavior,
} from 'metro/src/ModuleGraph/worker/collectDependencies';
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';

const getMinifier = require('./utils/getMinifier');
const {transformFromAstSync} = require('@babel/core');
Expand All @@ -52,6 +51,7 @@ const countLines = require('metro/src/lib/countLines');
const {
InvalidRequireCallError: InternalInvalidRequireCallError,
} = require('metro/src/ModuleGraph/worker/collectDependencies');
const collectDependencies = require('metro/src/ModuleGraph/worker/collectDependencies');
const generateImportNames = require('metro/src/ModuleGraph/worker/generateImportNames');
const JsFileWrapping = require('metro/src/ModuleGraph/worker/JsFileWrapping');
const nullthrows = require('nullthrows');
Expand Down Expand Up @@ -94,7 +94,6 @@ export type JsTransformerConfig = $ReadOnly<{
optimizationSizeLimit: number,
publicPath: string,
allowOptionalDependencies: AllowOptionalDependencies,
unstable_collectDependenciesPath: string,
unstable_dependencyMapReservedName: ?string,
unstable_disableModuleWrapping: boolean,
unstable_disableNormalizePseudoGlobals: boolean,
Expand Down Expand Up @@ -395,8 +394,6 @@ async function transformJS(
dependencyMapName: config.unstable_dependencyMapReservedName,
unstable_allowRequireContext: config.unstable_allowRequireContext,
};
// $FlowFixMe[unsupported-syntax] dynamic require
const collectDependencies: CollectDependenciesFn = require(config.unstable_collectDependenciesPath);
({ast, dependencies, dependencyMapName} = collectDependencies(ast, opts));
} catch (error) {
if (error instanceof InternalInvalidRequireCallError) {
Expand Down Expand Up @@ -724,19 +721,13 @@ module.exports = {
},

getCacheKey: (config: JsTransformerConfig): string => {
const {
babelTransformerPath,
minifierPath,
unstable_collectDependenciesPath,
...remainingConfig
} = config;
const {babelTransformerPath, minifierPath, ...remainingConfig} = config;

const filesKey = getCacheKey([
require.resolve(babelTransformerPath),
require.resolve(minifierPath),
require.resolve('./utils/getMinifier'),
require.resolve('./utils/assetTransformer'),
require.resolve(unstable_collectDependenciesPath),
require.resolve('metro/src/ModuleGraph/worker/generateImportNames'),
require.resolve('metro/src/ModuleGraph/worker/JsFileWrapping'),
...metroTransformPlugins.getTransformPluginCacheKeyFiles(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import type {Dependency} from '../collectDependencies';
import type {
DependencyTransformer,
ImportQualifier,
InternalDependency,
ModuleDependencyRegistry,
MutableInternalDependency,
Options,
State,
} from '../collectDependencies';
Expand Down Expand Up @@ -1389,34 +1386,6 @@ it('uses the dependency transformer specified in the options to transform the de
);
});

it('uses the dependency registry specified in the options to register dependencies', () => {
const ast = astFromCode(`
const a = require('b/lib/a');
import b from 'b/lib/b';
export {Banana} from 'Banana';
import("some/async/module").then(foo => {});
__prefetchImport("a/prefetch/module");
`);

const {dependencies} = collectDependencies(ast, {
...opts,
dependencyTransformer: MockDependencyTransformer,
dependencyRegistry: new MockModuleDependencyRegistry(),
});

expect(dependencies).toEqual([
{name: 'b/lib/a', data: objectContaining({asyncType: null})},
{name: 'b/lib/b', data: objectContaining({asyncType: null})},
{name: 'Banana', data: objectContaining({asyncType: null})},
{name: 'some/async/module', data: objectContaining({asyncType: 'async'})},
{
name: 'a/prefetch/module',
data: objectContaining({asyncType: 'prefetch'}),
},
]);
});

it('collects require.resolveWeak calls', () => {
const ast = astFromCode(`
require.resolveWeak("some/async/module");
Expand Down Expand Up @@ -1481,34 +1450,6 @@ function astFromCode(code: string): BabelNodeFile {
});
}

// Mock registry that collects *all* `registerDependency` calls.
// Allows to verify that the `registerDependency` function was called for each
// extracted dependency. Collapsing dependencies is implemented by specific
// `collectDependencies` implementations and should be tested there.
class MockModuleDependencyRegistry implements ModuleDependencyRegistry {
_dependencies: Array<InternalDependency> = [];

registerDependency(qualifier: ImportQualifier): InternalDependency {
const data: MutableInternalDependency = {
index: this._dependencies.length,
name: qualifier.name,
asyncType: qualifier.asyncType,
isOptional: qualifier.optional ?? false,
locs: [],

// Index = easy key for every dependency since we don't collapse/reorder
key: String(this._dependencies.length),
};

this._dependencies.push(data);
return data;
}

getDependencies(): Array<InternalDependency> {
return this._dependencies;
}
}

// Mock transformer for dependencies. Uses a "readable" format
// require() -> require(id, module name)
// import() -> require(async moudle name).async(id, module name)
Expand Down
17 changes: 3 additions & 14 deletions packages/metro/src/ModuleGraph/worker/collectDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type InternalDependency = $ReadOnly<MutableInternalDependency>;
export type State = {
asyncRequireModulePathStringLiteral: ?StringLiteral,
dependencyCalls: Set<string>,
dependencyRegistry: ModuleDependencyRegistry,
dependencyRegistry: DependencyRegistry,
dependencyTransformer: DependencyTransformer,
dynamicRequires: DynamicRequiresBehavior,
dependencyMapIdentifier: ?Identifier,
Expand All @@ -89,7 +89,6 @@ export type Options = $ReadOnly<{
inlineableCalls: $ReadOnlyArray<string>,
keepRequireNames: boolean,
allowOptionalDependencies: AllowOptionalDependencies,
dependencyRegistry?: ModuleDependencyRegistry,
dependencyTransformer?: DependencyTransformer,
/** Enable `require.context` statements which can be used to import multiple files in a directory. */
unstable_allowRequireContext: boolean,
Expand All @@ -101,15 +100,6 @@ export type CollectedDependencies = $ReadOnly<{
dependencies: $ReadOnlyArray<Dependency>,
}>;

// Registry for the dependency of a module.
// Defines when dependencies should be collapsed.
// E.g. should a module that's once required optionally and once not
// be treated as the same or different dependencies.
export interface ModuleDependencyRegistry {
registerDependency(qualifier: ImportQualifier): InternalDependency;
getDependencies(): Array<InternalDependency>;
}

export interface DependencyTransformer {
transformSyncRequire(
path: NodePath<CallExpression>,
Expand Down Expand Up @@ -149,8 +139,7 @@ function collectDependencies(
const state: State = {
asyncRequireModulePathStringLiteral: null,
dependencyCalls: new Set(),
dependencyRegistry:
options.dependencyRegistry ?? new DefaultModuleDependencyRegistry(),
dependencyRegistry: new DependencyRegistry(),
dependencyTransformer:
options.dependencyTransformer ?? DefaultDependencyTransformer,
dependencyMapIdentifier: null,
Expand Down Expand Up @@ -774,7 +763,7 @@ function getKeyForDependency(qualifier: ImportQualifier): string {
return key;
}

class DefaultModuleDependencyRegistry implements ModuleDependencyRegistry {
class DependencyRegistry {
_dependencies: Map<string, InternalDependency> = new Map();

registerDependency(qualifier: ImportQualifier): InternalDependency {
Expand Down

0 comments on commit 439f0d0

Please sign in to comment.