Skip to content

Commit 6b2e8cd

Browse files
robhoganfacebook-github-bot
authored andcommitted
Flow type options to Babel transform plugins
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 Reviewed By: vzaidman Differential Revision: D65715801 fbshipit-source-id: f72943ff7e10ad20a132126f3ab14f374089e0a9
1 parent db3110d commit 6b2e8cd

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

packages/metro-transform-plugins/src/import-export-plugin.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ import typeof * as Types from '@babel/types';
2727
const template = require('@babel/template').default;
2828
const nullthrows = require('nullthrows');
2929

30+
export type Options = $ReadOnly<{
31+
importDefault: string,
32+
importAll: string,
33+
resolve: boolean,
34+
out?: {isESModule: boolean, ...},
35+
}>;
36+
3037
type State = {
3138
exportAll: Array<{file: string, loc: ?BabelSourceLocation, ...}>,
3239
exportDefault: Array<{local: string, loc: ?BabelSourceLocation, ...}>,
@@ -39,13 +46,7 @@ type State = {
3946
imports: Array<{node: Statement}>,
4047
importDefault: BabelNode,
4148
importAll: BabelNode,
42-
opts: {
43-
importDefault: string,
44-
importAll: string,
45-
resolve: boolean,
46-
out?: {isESModule: boolean, ...},
47-
...
48-
},
49+
opts: Options,
4950
...
5051
};
5152

packages/metro-transform-plugins/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import typeof ImportExportPlugin from './import-export-plugin';
1616
import typeof InlinePlugin from './inline-plugin';
1717
import typeof InlineRequiresPlugin from './inline-requires-plugin';
1818
import typeof NormalizePseudoGlobalsFn from './normalizePseudoGlobals';
19+
export type {Options as ImportExportPluginOptions} from './import-export-plugin';
1920
export type {Options as InlinePluginOptions} from './inline-plugin';
21+
export type {PluginOptions as InlineRequiresPluginOptions} from './inline-requires-plugin';
2022

2123
type TransformPlugins = {
2224
addParamsToDefineCall(string, ...Array<mixed>): string,

packages/metro-transform-plugins/src/inline-plugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import typeof * as Types from '@babel/types';
2525

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

28-
export type Options = {
28+
export type Options = $ReadOnly<{
2929
dev: boolean,
3030
inlinePlatform: boolean,
3131
isWrapped: boolean,
3232
requireName?: string,
3333
platform: string,
34-
};
34+
}>;
3535

3636
type State = {opts: Options};
3737

packages/metro-transform-worker/src/index.js

+27-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import type {
2323
FBSourceFunctionMap,
2424
MetroSourceMapSegmentTuple,
2525
} from 'metro-source-map';
26+
import type {
27+
ImportExportPluginOptions,
28+
InlinePluginOptions,
29+
InlineRequiresPluginOptions,
30+
} from 'metro-transform-plugins';
2631
import type {TransformResultDependency} from 'metro/src/DeltaBundler';
2732
import type {AllowOptionalDependencies} from 'metro/src/DeltaBundler/types.flow.js';
2833
import type {
@@ -287,32 +292,43 @@ async function transformJS(
287292
// Perform the import-export transform (in case it's still needed), then
288293
// fold requires and perform constant folding (if in dev).
289294
const plugins: Array<PluginEntry> = [];
290-
const babelPluginOpts = {
291-
...options,
292-
inlineableCalls: [importDefault, importAll],
293-
importDefault,
294-
importAll,
295-
};
296295

297296
if (options.experimentalImportSupport === true) {
298-
plugins.push([metroTransformPlugins.importExportPlugin, babelPluginOpts]);
297+
plugins.push([
298+
metroTransformPlugins.importExportPlugin,
299+
{
300+
importAll,
301+
importDefault,
302+
resolve: false,
303+
} as ImportExportPluginOptions,
304+
]);
299305
}
300306

301307
if (options.inlineRequires) {
302308
plugins.push([
303309
metroTransformPlugins.inlineRequiresPlugin,
304310
{
305-
...babelPluginOpts,
306311
ignoredRequires: options.nonInlinedRequires,
312+
inlineableCalls: [importDefault, importAll],
307313
memoizeCalls:
314+
// $FlowFixMe[incompatible-cast] is this always (?boolean)?
308315
options.customTransformOptions?.unstable_memoizeInlineRequires ??
309316
options.unstable_memoizeInlineRequires,
310317
nonMemoizedModules: options.unstable_nonMemoizedInlineRequires,
311-
},
318+
} as InlineRequiresPluginOptions,
312319
]);
313320
}
314321

315-
plugins.push([metroTransformPlugins.inlinePlugin, babelPluginOpts]);
322+
plugins.push([
323+
metroTransformPlugins.inlinePlugin,
324+
{
325+
dev: options.dev,
326+
inlinePlatform: options.inlinePlatform,
327+
isWrapped: false,
328+
// $FlowFixMe[incompatible-cast] expects a string if inlinePlatform
329+
platform: options.platform,
330+
} as InlinePluginOptions,
331+
]);
316332

317333
ast = nullthrows(
318334
transformFromAstSync(ast, '', {
@@ -345,9 +361,7 @@ async function transformJS(
345361
configFile: false,
346362
comments: true,
347363
filename: file.filename,
348-
plugins: [
349-
[metroTransformPlugins.constantFoldingPlugin, babelPluginOpts],
350-
],
364+
plugins: [metroTransformPlugins.constantFoldingPlugin],
351365
sourceMaps: false,
352366
cloneInputAst: false,
353367
}).ast,

0 commit comments

Comments
 (0)