Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transformOptions to each graph. Update tests. #679

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/metro/src/DeltaBundler.js
Expand Up @@ -21,6 +21,7 @@ import type {
MixedOutput,
Options,
} from './DeltaBundler/types.flow';
import type {TransformInputOptions} from './DeltaBundler/types.flow';
afoxman marked this conversation as resolved.
Show resolved Hide resolved

export type {
DeltaResult,
Expand Down Expand Up @@ -57,11 +58,17 @@ class DeltaBundler<T = MixedOutput> {

async getDependencies(
entryPoints: $ReadOnlyArray<string>,
transformOptions: TransformInputOptions,
options: Options<T>,
): Promise<Dependencies<T>> {
const depGraph = await this._bundler.getDependencyGraph();

const deltaCalculator = new DeltaCalculator(entryPoints, depGraph, options);
const deltaCalculator = new DeltaCalculator(
entryPoints,
depGraph,
transformOptions,
options,
);

await deltaCalculator.getDelta({reset: true, shallow: options.shallow});
const graph = deltaCalculator.getGraph();
Expand All @@ -75,11 +82,17 @@ class DeltaBundler<T = MixedOutput> {
// To get just the dependencies, use getDependencies which will not leak graphs.
async buildGraph(
entryPoints: $ReadOnlyArray<string>,
transformOptions: TransformInputOptions,
options: Options<T>,
): Promise<Graph<T>> {
const depGraph = await this._bundler.getDependencyGraph();

const deltaCalculator = new DeltaCalculator(entryPoints, depGraph, options);
const deltaCalculator = new DeltaCalculator(
entryPoints,
depGraph,
transformOptions,
options,
);

await deltaCalculator.getDelta({reset: true, shallow: options.shallow});
const graph = deltaCalculator.getGraph();
Expand Down
12 changes: 11 additions & 1 deletion packages/metro/src/DeltaBundler/DeltaCalculator.js
Expand Up @@ -18,7 +18,12 @@ const {
const {EventEmitter} = require('events');

import type DependencyGraph from '../node-haste/DependencyGraph';
import type {DeltaResult, Graph, Options} from './types.flow';
import type {
DeltaResult,
Graph,
Options,
TransformInputOptions,
} from './types.flow';

/**
* This class is in charge of calculating the delta of changed modules that
Expand All @@ -28,6 +33,7 @@ import type {DeltaResult, Graph, Options} from './types.flow';
*/
class DeltaCalculator<T> extends EventEmitter {
_dependencyGraph: DependencyGraph;
_transformOptions: TransformInputOptions;
_options: Options<T>;

_currentBuildPromise: ?Promise<DeltaResult<T>>;
Expand All @@ -39,17 +45,20 @@ class DeltaCalculator<T> extends EventEmitter {
constructor(
entryPoints: $ReadOnlyArray<string>,
dependencyGraph: DependencyGraph,
transformOptions: TransformInputOptions,
options: Options<T>,
) {
super();

this._transformOptions = transformOptions;
this._options = options;
this._dependencyGraph = dependencyGraph;

this._graph = {
dependencies: new Map(),
entryPoints,
importBundleNames: new Set(),
transformOptions: this._transformOptions,
};

this._dependencyGraph
Expand All @@ -72,6 +81,7 @@ class DeltaCalculator<T> extends EventEmitter {
dependencies: new Map(),
entryPoints: this._graph.entryPoints,
importBundleNames: new Set(),
transformOptions: this._transformOptions,
};
this._modifiedFiles = new Set();
this._deletedFiles = new Set();
Expand Down
12 changes: 7 additions & 5 deletions packages/metro/src/DeltaBundler/__tests__/DeltaBundler-test.js
Expand Up @@ -46,7 +46,9 @@ describe('DeltaBundler', () => {
});

it('should create a new graph when buildGraph gets called', async () => {
expect(await deltaBundler.buildGraph({}, {shallow: false})).toEqual(graph);
expect(await deltaBundler.buildGraph({}, {}, {shallow: false})).toEqual(
graph,
);

expect(DeltaCalculator.prototype.getDelta.mock.calls[0][0]).toEqual({
reset: true,
Expand All @@ -55,7 +57,7 @@ describe('DeltaBundler', () => {
});

it('should get a delta when getDelta gets called', async () => {
const graph = await deltaBundler.buildGraph({}, {shallow: false});
const graph = await deltaBundler.buildGraph({}, {}, {shallow: false});

expect(await deltaBundler.getDelta(graph, {reset: false})).toEqual({
modified: new Map(),
Expand All @@ -65,7 +67,7 @@ describe('DeltaBundler', () => {
});

it('should get a reset delta when calling getDelta({reset: true})', async () => {
const graph = await deltaBundler.buildGraph({}, {shallow: false});
const graph = await deltaBundler.buildGraph({}, {}, {shallow: false});

expect(await deltaBundler.getDelta(graph, {reset: true})).toEqual({
modified: graph.dependencies,
Expand All @@ -75,7 +77,7 @@ describe('DeltaBundler', () => {
});

it('should throw an error when trying to get the delta of a graph that does not exist', async () => {
const graph = await deltaBundler.buildGraph({}, {shallow: false});
const graph = await deltaBundler.buildGraph({}, {}, {shallow: false});

deltaBundler.endGraph(graph);

Expand All @@ -85,7 +87,7 @@ describe('DeltaBundler', () => {
});

it('should throw an error when trying to end a graph twice', async () => {
const graph = await deltaBundler.buildGraph({}, {shallow: false});
const graph = await deltaBundler.buildGraph({}, {}, {shallow: false});

deltaBundler.endGraph(graph);

Expand Down
12 changes: 12 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,20 @@ 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>,
transformOptions: TransformInputOptions,
|};

export type TransformResult<T = MixedOutput> = $ReadOnly<{|
Expand Down
41 changes: 23 additions & 18 deletions 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 @@ -109,23 +109,27 @@ class IncrementalBundler {
): Promise<OutputGraph> {
const absoluteEntryFiles = await this._getAbsoluteEntryFiles(entryFiles);

const graph = await this._deltaBundler.buildGraph(absoluteEntryFiles, {
resolve: await transformHelpers.getResolveDependencyFn(
this._bundler,
transformOptions.platform,
),
transform: await transformHelpers.getTransformFn(
absoluteEntryFiles,
this._bundler,
this._deltaBundler,
this._config,
transformOptions,
),
onProgress: otherOptions.onProgress,
experimentalImportBundleSupport: this._config.transformer
.experimentalImportBundleSupport,
shallow: otherOptions.shallow,
});
const graph = await this._deltaBundler.buildGraph(
absoluteEntryFiles,
transformOptions,
{
resolve: await transformHelpers.getResolveDependencyFn(
this._bundler,
transformOptions.platform,
),
transform: await transformHelpers.getTransformFn(
absoluteEntryFiles,
this._bundler,
this._deltaBundler,
this._config,
transformOptions,
),
onProgress: otherOptions.onProgress,
experimentalImportBundleSupport: this._config.transformer
.experimentalImportBundleSupport,
shallow: otherOptions.shallow,
},
);

this._config.serializer.experimentalSerializerHook(graph, {
added: graph.dependencies,
Expand All @@ -149,6 +153,7 @@ class IncrementalBundler {

const dependencies = await this._deltaBundler.getDependencies(
absoluteEntryFiles,
transformOptions,
{
resolve: await transformHelpers.getResolveDependencyFn(
this._bundler,
Expand Down
33 changes: 32 additions & 1 deletion packages/metro/src/Server/__tests__/Server-test.js
Expand Up @@ -119,7 +119,7 @@ describe('processRequest', () => {
beforeEach(() => {
const currentGraphs = new Set();
DeltaBundler.prototype.buildGraph.mockImplementation(
async (entryPoints, options) => {
async (entryPoints, transformOptions, options) => {
dependencies = new Map([
[
'/root/mybundle.js',
Expand Down Expand Up @@ -187,6 +187,7 @@ describe('processRequest', () => {
entryPoints: ['/root/mybundle.js'],
dependencies,
importBundleNames: new Set(),
transformOptions,
};
currentGraphs.add(graph);

Expand Down Expand Up @@ -609,6 +610,16 @@ describe('processRequest', () => {

expect(DeltaBundler.prototype.buildGraph).toBeCalledWith(
['/root/index.js'],
{
customTransformOptions: {},
dev: true,
hot: true,
minify: false,
platform: 'ios',
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'default',
},
{
experimentalImportBundleSupport: false,
onProgress: expect.any(Function),
Expand All @@ -635,6 +646,16 @@ describe('processRequest', () => {

expect(DeltaBundler.prototype.buildGraph).toBeCalledWith(
['/root/index.js'],
{
customTransformOptions: {},
dev: true,
hot: true,
minify: false,
platform: null,
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'hermes-stable',
},
{
experimentalImportBundleSupport: false,
onProgress: expect.any(Function),
Expand Down Expand Up @@ -789,6 +810,16 @@ describe('processRequest', () => {

expect(DeltaBundler.prototype.buildGraph).toBeCalledWith(
['/root/foo file'],
{
customTransformOptions: {},
dev: true,
hot: false,
minify: false,
platform: undefined,
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'default',
},
{
experimentalImportBundleSupport: false,
onProgress: null,
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 @@ -19,7 +19,7 @@ const {compile} = require('metro-hermes-compiler');

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

async function getPrependedScripts(
Expand All @@ -46,6 +46,7 @@ async function getPrependedScripts(

const dependencies = await deltaBundler.getDependencies(
[defaults.moduleSystem, ...polyfillModuleNames],
transformOptions,
{
resolve: await transformHelpers.getResolveDependencyFn(
bundler,
Expand Down
12 changes: 2 additions & 10 deletions packages/metro/src/lib/transformHelpers.js
Expand Up @@ -17,18 +17,10 @@ import type {TransformOptions} from '../DeltaBundler/Worker';
import type DeltaBundler, {TransformFn} from '../DeltaBundler';
import type {ConfigT} from 'metro-config/src/configTypes.flow';
import type {Type} from 'metro-transform-worker';
import type {TransformInputOptions} from '../DeltaBundler/types.flow';

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

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

type TransformOptionsWithRawInlines = {|
...TransformOptions,
+inlineRequires: InlineRequiresRaw,
Expand Down Expand Up @@ -66,7 +58,7 @@ async function calcTransformerOptions(
}

const getDependencies = async (path: string) => {
const dependencies = await deltaBundler.getDependencies([path], {
const dependencies = await deltaBundler.getDependencies([path], options, {
resolve: await getResolveDependencyFn(bundler, options.platform),
transform: await getTransformFn([path], bundler, deltaBundler, config, {
...options,
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