Skip to content

Commit

Permalink
Flow type options to Babel transform plugins
Browse files Browse the repository at this point in the history
Summary:
Currently in `metro-transform-worker` we pass the same `babelPluginOpts` bag to all plugins, making it hard to tell what's actually used where, and what's unused.

This adds Flow types for all of the options passed to `metro-transform-plugins`, and specifies the options arg explicitly.

Changelog: Internal

Differential Revision: D65715801
  • Loading branch information
robhogan authored and facebook-github-bot committed Nov 9, 2024
1 parent f6ff8c4 commit c5a4a9a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
15 changes: 8 additions & 7 deletions packages/metro-transform-plugins/src/import-export-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import typeof * as Types from '@babel/types';
const template = require('@babel/template').default;
const nullthrows = require('nullthrows');

export type Options = $ReadOnly<{
importDefault: string,
importAll: string,
resolve: boolean,
out?: {isESModule: boolean, ...},
}>;

type State = {
exportAll: Array<{file: string, loc: ?BabelSourceLocation, ...}>,
exportDefault: Array<{local: string, loc: ?BabelSourceLocation, ...}>,
Expand All @@ -39,13 +46,7 @@ type State = {
imports: Array<{node: Statement}>,
importDefault: BabelNode,
importAll: BabelNode,
opts: {
importDefault: string,
importAll: string,
resolve: boolean,
out?: {isESModule: boolean, ...},
...
},
opts: Options,
...
};

Expand Down
2 changes: 2 additions & 0 deletions packages/metro-transform-plugins/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import typeof ImportExportPlugin from './import-export-plugin';
import typeof InlineRequiresPlugin from './inline-requires-plugin';
import typeof InlinePlugin from './inline-plugin';
import typeof NormalizePseudoGlobalsFn from './normalizePseudoGlobals';
export type {Options as ImportExportPluginOptions} from './import-export-plugin';
export type {Options as InlinePluginOptions} from './inline-plugin';
export type {PluginOptions as InlineRequiresPluginOptions} from './inline-requires-plugin';

type TransformPlugins = {
addParamsToDefineCall(string, ...Array<mixed>): string,
Expand Down
4 changes: 2 additions & 2 deletions packages/metro-transform-plugins/src/inline-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import typeof * as Types from '@babel/types';

const createInlinePlatformChecks = require('./utils/createInlinePlatformChecks');

export type Options = {
export type Options = $ReadOnly<{
dev: boolean,
inlinePlatform: boolean,
isWrapped: boolean,
requireName?: string,
platform: string,
};
}>;

type State = {opts: Options};

Expand Down
40 changes: 27 additions & 13 deletions packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import type {
FBSourceFunctionMap,
MetroSourceMapSegmentTuple,
} from 'metro-source-map';
import type {
ImportExportPluginOptions,
InlinePluginOptions,
InlineRequiresPluginOptions,
} from 'metro-transform-plugins';
import type {TransformResultDependency} from 'metro/src/DeltaBundler';
import type {AllowOptionalDependencies} from 'metro/src/DeltaBundler/types.flow.js';
import type {
Expand Down Expand Up @@ -287,32 +292,43 @@ async function transformJS(
// Perform the import-export transform (in case it's still needed), then
// fold requires and perform constant folding (if in dev).
const plugins: Array<PluginEntry> = [];
const babelPluginOpts = {
...options,
inlineableCalls: [importDefault, importAll],
importDefault,
importAll,
};

if (options.experimentalImportSupport === true) {
plugins.push([metroTransformPlugins.importExportPlugin, babelPluginOpts]);
plugins.push([
metroTransformPlugins.importExportPlugin,
{
importAll,
importDefault,
resolve: false,
} as ImportExportPluginOptions,
]);
}

if (options.inlineRequires) {
plugins.push([
metroTransformPlugins.inlineRequiresPlugin,
{
...babelPluginOpts,
ignoredRequires: options.nonInlinedRequires,
inlineableCalls: [importDefault, importAll],
memoizeCalls:
// $FlowFixMe[incompatible-cast] is this always (?boolean)?
options.customTransformOptions?.unstable_memoizeInlineRequires ??
options.unstable_memoizeInlineRequires,
nonMemoizedModules: options.unstable_nonMemoizedInlineRequires,
},
} as InlineRequiresPluginOptions,
]);
}

plugins.push([metroTransformPlugins.inlinePlugin, babelPluginOpts]);
plugins.push([
metroTransformPlugins.inlinePlugin,
{
dev: options.dev,
inlinePlatform: options.inlinePlatform,
isWrapped: false,
// $FlowFixMe[incompatible-cast] expects a string if inlinePlatform
platform: options.platform,
} as InlinePluginOptions,
]);

ast = nullthrows(
transformFromAstSync(ast, '', {
Expand Down Expand Up @@ -345,9 +361,7 @@ async function transformJS(
configFile: false,
comments: true,
filename: file.filename,
plugins: [
[metroTransformPlugins.constantFoldingPlugin, babelPluginOpts],
],
plugins: [metroTransformPlugins.constantFoldingPlugin],
sourceMaps: false,
cloneInputAst: false,
}).ast,
Expand Down

0 comments on commit c5a4a9a

Please sign in to comment.