Skip to content

Commit

Permalink
Add transformOptions to each graph. Update tests. (#679)
Browse files Browse the repository at this point in the history
Summary:
**Summary**

Each graph is built using a specific set of transformOptions. Bind these to the graph, making them available to extensibility APIs such as `customSerializer` and `experimentalSerializerHook`. This data -- specifically, `platform` -- makes it possible to perform TypeScript validation during bundle/serve scenarios. `platform` is used to resolve types for platform-specific files.

**Test plan**

I updated existing Metro tests to validate the transformOptions in a graph.

I also wrote a sample application which bundles using Metro, and uses `experimentalSerializerHook`. I had my app print `platform` to the console, verifying that it was the correct value.

Pull Request resolved: #679

Reviewed By: GijsWeterings

Differential Revision: D29335649

Pulled By: motiz88

fbshipit-source-id: fa4c5f9a36123479629d7118e7e62002d8d7d571
  • Loading branch information
afoxman authored and facebook-github-bot committed Jun 24, 2021
1 parent 598de6f commit 57106d2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/metro/src/DeltaBundler/DeltaCalculator.js
Expand Up @@ -50,6 +50,7 @@ class DeltaCalculator<T> extends EventEmitter {
dependencies: new Map(),
entryPoints,
importBundleNames: new Set(),
transformOptions: this._options.transformOptions,
};

this._dependencyGraph
Expand All @@ -72,6 +73,7 @@ class DeltaCalculator<T> extends EventEmitter {
dependencies: new Map(),
entryPoints: this._graph.entryPoints,
importBundleNames: new Set(),
transformOptions: this._options.transformOptions,
};
this._modifiedFiles = new Set();
this._deletedFiles = new Set();
Expand Down
14 changes: 14 additions & 0 deletions packages/metro/src/DeltaBundler/types.flow.js
Expand Up @@ -10,6 +10,8 @@

'use strict';

import type {JsTransformOptions} from 'metro-transform-worker';

export type MixedOutput = {|
+data: mixed,
+type: string,
Expand Down Expand Up @@ -63,10 +65,21 @@ export type Module<T = MixedOutput> = {|

export type Dependencies<T = MixedOutput> = Map<string, Module<T>>;

export type TransformInputOptions = $Diff<
JsTransformOptions,
{
inlinePlatform: boolean,
inlineRequires: boolean,
...
},
>;

export type Graph<T = MixedOutput> = {|
dependencies: Dependencies<T>,
importBundleNames: Set<string>,
+entryPoints: $ReadOnlyArray<string>,
// Unused in core but useful for custom serializers / experimentalSerializerHook
+transformOptions: TransformInputOptions,
|};

export type TransformResult<T = MixedOutput> = $ReadOnly<{|
Expand All @@ -92,6 +105,7 @@ export type AllowOptionalDependencies =
export type Options<T = MixedOutput> = {|
+resolve: (from: string, to: string) => string,
+transform: TransformFn<T>,
+transformOptions: TransformInputOptions,
+onProgress: ?(numProcessed: number, total: number) => mixed,
+experimentalImportBundleSupport: boolean,
+shallow: boolean,
Expand Down
4 changes: 3 additions & 1 deletion packages/metro/src/IncrementalBundler.js
Expand Up @@ -23,11 +23,11 @@ const transformHelpers = require('./lib/transformHelpers');

import type {
Options as DeltaBundlerOptions,
TransformInputOptions,
Dependencies,
} from './DeltaBundler/types.flow';
import type {DeltaResult, Module, Graph} from './DeltaBundler';
import type {GraphId} from './lib/getGraphId';
import type {TransformInputOptions} from './lib/transformHelpers';
import type {ConfigT} from 'metro-config/src/configTypes.flow';

export opaque type RevisionId: string = string;
Expand Down Expand Up @@ -121,6 +121,7 @@ class IncrementalBundler {
this._config,
transformOptions,
),
transformOptions,
onProgress: otherOptions.onProgress,
experimentalImportBundleSupport: this._config.transformer
.experimentalImportBundleSupport,
Expand Down Expand Up @@ -161,6 +162,7 @@ class IncrementalBundler {
this._config,
transformOptions,
),
transformOptions,
onProgress: otherOptions.onProgress,
experimentalImportBundleSupport: this._config.transformer
.experimentalImportBundleSupport,
Expand Down
31 changes: 31 additions & 0 deletions packages/metro/src/Server/__tests__/Server-test.js
Expand Up @@ -187,6 +187,7 @@ describe('processRequest', () => {
entryPoints: ['/root/mybundle.js'],
dependencies,
importBundleNames: new Set(),
transformOptions: options.transformOptions,
};
currentGraphs.add(graph);

Expand Down Expand Up @@ -615,6 +616,16 @@ describe('processRequest', () => {
resolve: expect.any(Function),
shallow: false,
transform: expect.any(Function),
transformOptions: {
customTransformOptions: {},
dev: true,
hot: true,
minify: false,
platform: 'ios',
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'default',
},
},
);
});
Expand All @@ -641,6 +652,16 @@ describe('processRequest', () => {
resolve: expect.any(Function),
shallow: false,
transform: expect.any(Function),
transformOptions: {
customTransformOptions: {},
dev: true,
hot: true,
minify: false,
platform: null,
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'hermes-stable',
},
},
);
});
Expand Down Expand Up @@ -795,6 +816,16 @@ describe('processRequest', () => {
resolve: expect.any(Function),
shallow: false,
transform: expect.any(Function),
transformOptions: {
customTransformOptions: {},
dev: true,
hot: false,
minify: false,
platform: undefined,
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'default',
},
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/lib/getGraphId.js
Expand Up @@ -12,7 +12,7 @@

const canonicalize = require('metro-core/src/canonicalize');

import type {TransformInputOptions} from './transformHelpers';
import type {TransformInputOptions} from '../DeltaBundler/types.flow';

export opaque type GraphId: string = string;

Expand Down
3 changes: 2 additions & 1 deletion packages/metro/src/lib/getPrependedScripts.js
Expand Up @@ -18,8 +18,8 @@ const transformHelpers = require('./transformHelpers');
const {compile} = require('metro-hermes-compiler');

import type Bundler from '../Bundler';
import type {TransformInputOptions} from '../DeltaBundler/types.flow';
import type DeltaBundler, {Module} from '../DeltaBundler';
import type {TransformInputOptions} from '../lib/transformHelpers';
import type {ConfigT} from 'metro-config/src/configTypes.flow';

async function getPrependedScripts(
Expand Down Expand Up @@ -58,6 +58,7 @@ async function getPrependedScripts(
config,
transformOptions,
),
transformOptions,
onProgress: null,
experimentalImportBundleSupport:
config.transformer.experimentalImportBundleSupport,
Expand Down
11 changes: 2 additions & 9 deletions packages/metro/src/lib/transformHelpers.js
Expand Up @@ -14,21 +14,13 @@ const path = require('path');

import type Bundler from '../Bundler';
import type {TransformOptions} from '../DeltaBundler/Worker';
import type {TransformInputOptions} from '../DeltaBundler/types.flow';
import type DeltaBundler, {TransformFn} from '../DeltaBundler';
import type {ConfigT} from 'metro-config/src/configTypes.flow';
import type {Type} from 'metro-transform-worker';

type InlineRequiresRaw = {+blockList: {[string]: true, ...}, ...} | boolean;

export type TransformInputOptions = $Diff<
TransformOptions,
{
inlinePlatform: boolean,
inlineRequires: boolean,
...
},
>;

type TransformOptionsWithRawInlines = {|
...TransformOptions,
+inlineRequires: InlineRequiresRaw,
Expand Down Expand Up @@ -72,6 +64,7 @@ async function calcTransformerOptions(
...options,
minify: false,
}),
transformOptions: options,
onProgress: null,
experimentalImportBundleSupport:
config.transformer.experimentalImportBundleSupport,
Expand Down
6 changes: 4 additions & 2 deletions packages/metro/src/shared/types.flow.js
Expand Up @@ -10,8 +10,10 @@

'use strict';

import type {Options as DeltaBundlerOptions} from '../DeltaBundler/types.flow';
import type {TransformInputOptions} from '../lib/transformHelpers';
import type {
Options as DeltaBundlerOptions,
TransformInputOptions,
} from '../DeltaBundler/types.flow';
import type {TransformProfile} from 'metro-babel-transformer';
import type {
MixedSourceMap,
Expand Down

0 comments on commit 57106d2

Please sign in to comment.