mirror of
https://github.com/status-im/metro.git
synced 2025-01-13 12:35:57 +00:00
Rename edge to module in DeltaBundler
Reviewed By: jeanlauliac Differential Revision: D7652219 fbshipit-source-id: 88922711eaccd0a6bff1a0f3cc9bfaa770f4c275
This commit is contained in:
parent
2446fe2211
commit
47793c25a5
@ -18,10 +18,10 @@ const {
|
|||||||
const {EventEmitter} = require('events');
|
const {EventEmitter} = require('events');
|
||||||
|
|
||||||
import type DependencyGraph from '../node-haste/DependencyGraph';
|
import type DependencyGraph from '../node-haste/DependencyGraph';
|
||||||
import type {DependencyEdge, Graph, Options} from './traverseDependencies';
|
import type {Graph, Module, Options} from './traverseDependencies';
|
||||||
|
|
||||||
export type DeltaResult = {|
|
export type DeltaResult = {|
|
||||||
+modified: Map<string, DependencyEdge>,
|
+modified: Map<string, Module>,
|
||||||
+deleted: Set<string>,
|
+deleted: Set<string>,
|
||||||
+reset: boolean,
|
+reset: boolean,
|
||||||
|};
|
|};
|
||||||
@ -124,8 +124,8 @@ class DeltaCalculator extends EventEmitter {
|
|||||||
modifiedFiles.forEach(file => this._modifiedFiles.add(file));
|
modifiedFiles.forEach(file => this._modifiedFiles.add(file));
|
||||||
deletedFiles.forEach(file => this._deletedFiles.add(file));
|
deletedFiles.forEach(file => this._deletedFiles.add(file));
|
||||||
|
|
||||||
// If after an error the number of edges has changed, we could be in
|
// If after an error the number of modules has changed, we could be in
|
||||||
// a weird state. As a safe net we clean the dependency edges to force
|
// a weird state. As a safe net we clean the dependency modules to force
|
||||||
// a clean traversal of the graph next time.
|
// a clean traversal of the graph next time.
|
||||||
if (this._graph.dependencies.size !== numDependencies) {
|
if (this._graph.dependencies.size !== numDependencies) {
|
||||||
this._graph.dependencies = new Map();
|
this._graph.dependencies = new Map();
|
||||||
@ -151,7 +151,7 @@ class DeltaCalculator extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the graph with all the dependency edges. Each edge contains the
|
* Returns the graph with all the dependencies. Each module contains the
|
||||||
* needed information to do the traversing (dependencies, inverseDependencies)
|
* needed information to do the traversing (dependencies, inverseDependencies)
|
||||||
* plus some metadata.
|
* plus some metadata.
|
||||||
*/
|
*/
|
||||||
@ -208,10 +208,10 @@ class DeltaCalculator extends EventEmitter {
|
|||||||
// If a file has been deleted, we want to invalidate any other file that
|
// If a file has been deleted, we want to invalidate any other file that
|
||||||
// depends on it, so we can process it and correctly return an error.
|
// depends on it, so we can process it and correctly return an error.
|
||||||
deletedFiles.forEach(filePath => {
|
deletedFiles.forEach(filePath => {
|
||||||
const edge = this._graph.dependencies.get(filePath);
|
const module = this._graph.dependencies.get(filePath);
|
||||||
|
|
||||||
if (edge) {
|
if (module) {
|
||||||
edge.inverseDependencies.forEach(path => modifiedFiles.add(path));
|
module.inverseDependencies.forEach(path => modifiedFiles.add(path));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ const getAppendScripts = require('../../lib/getAppendScripts');
|
|||||||
const {wrapModule} = require('./helpers/js');
|
const {wrapModule} = require('./helpers/js');
|
||||||
|
|
||||||
import type {Delta, Graph} from '../../DeltaBundler';
|
import type {Delta, Graph} from '../../DeltaBundler';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
type Options = {|
|
type Options = {|
|
||||||
+createModuleId: string => number | string,
|
+createModuleId: string => number | string,
|
||||||
@ -28,7 +28,7 @@ type Options = {|
|
|||||||
|
|
||||||
function deltaJSBundle(
|
function deltaJSBundle(
|
||||||
entryPoint: string,
|
entryPoint: string,
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
delta: Delta,
|
delta: Delta,
|
||||||
sequenceId: string,
|
sequenceId: string,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
|
@ -13,14 +13,14 @@
|
|||||||
const {getAssetFiles} = require('../../Assets');
|
const {getAssetFiles} = require('../../Assets');
|
||||||
|
|
||||||
import type {Graph} from '../DeltaCalculator';
|
import type {Graph} from '../DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
type Options = {|
|
type Options = {|
|
||||||
platform: ?string,
|
platform: ?string,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
async function getAllFiles(
|
async function getAllFiles(
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<$ReadOnlyArray<string>> {
|
): Promise<$ReadOnlyArray<string>> {
|
||||||
|
@ -21,7 +21,7 @@ const {wrapModule} = require('./helpers/js');
|
|||||||
import type {GetTransformOptions} from '../../Bundler';
|
import type {GetTransformOptions} from '../../Bundler';
|
||||||
import type {ModuleTransportLike} from '../../shared/types.flow';
|
import type {ModuleTransportLike} from '../../shared/types.flow';
|
||||||
import type {Graph} from '../DeltaCalculator';
|
import type {Graph} from '../DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
type Options = {|
|
type Options = {|
|
||||||
+createModuleId: string => number,
|
+createModuleId: string => number,
|
||||||
@ -44,7 +44,7 @@ export type RamBundleInfo = {|
|
|||||||
|
|
||||||
async function getRamBundleInfo(
|
async function getRamBundleInfo(
|
||||||
entryPoint: string,
|
entryPoint: string,
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<RamBundleInfo> {
|
): Promise<RamBundleInfo> {
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
const addParamsToDefineCall = require('../../../lib/addParamsToDefineCall');
|
const addParamsToDefineCall = require('../../../lib/addParamsToDefineCall');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
import type {DependencyEdge} from '../../traverseDependencies';
|
import type {Module} from '../../traverseDependencies';
|
||||||
|
|
||||||
export type Options = {
|
export type Options = {
|
||||||
+createModuleId: string => number | string,
|
+createModuleId: string => number | string,
|
||||||
@ -25,7 +25,7 @@ export type Options = {
|
|||||||
// Make sure to set PRINT_REQUIRE_PATHS = true too, and restart Metro
|
// Make sure to set PRINT_REQUIRE_PATHS = true too, and restart Metro
|
||||||
const PASS_MODULE_PATHS_TO_DEFINE = false;
|
const PASS_MODULE_PATHS_TO_DEFINE = false;
|
||||||
|
|
||||||
function wrapModule(module: DependencyEdge, options: Options) {
|
function wrapModule(module: Module, options: Options) {
|
||||||
if (module.output.type === 'script') {
|
if (module.output.type === 'script') {
|
||||||
return module.output.code;
|
return module.output.code;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ const addParamsToDefineCall = require('../../lib/addParamsToDefineCall');
|
|||||||
const {wrapModule} = require('./helpers/js');
|
const {wrapModule} = require('./helpers/js');
|
||||||
|
|
||||||
import type {Delta, Graph} from '../../DeltaBundler';
|
import type {Delta, Graph} from '../../DeltaBundler';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
createModuleId: string => number,
|
createModuleId: string => number,
|
||||||
@ -48,7 +48,7 @@ function hmrJSBundle(delta: Delta, graph: Graph, options: Options): Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _prepareModule(
|
function _prepareModule(
|
||||||
module: DependencyEdge,
|
module: Module,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options,
|
options: Options,
|
||||||
): {|+id: number, +code: string|} {
|
): {|+id: number, +code: string|} {
|
||||||
|
@ -15,7 +15,7 @@ const getAppendScripts = require('../../lib/getAppendScripts');
|
|||||||
const {wrapModule} = require('./helpers/js');
|
const {wrapModule} = require('./helpers/js');
|
||||||
|
|
||||||
import type {Graph} from '../DeltaCalculator';
|
import type {Graph} from '../DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
type Options = {|
|
type Options = {|
|
||||||
+createModuleId: string => number | string,
|
+createModuleId: string => number | string,
|
||||||
@ -28,7 +28,7 @@ type Options = {|
|
|||||||
|
|
||||||
function plainJSBundle(
|
function plainJSBundle(
|
||||||
entryPoint: string,
|
entryPoint: string,
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options,
|
options: Options,
|
||||||
): string {
|
): string {
|
||||||
|
@ -13,11 +13,11 @@
|
|||||||
const {fromRawMappings} = require('metro-source-map');
|
const {fromRawMappings} = require('metro-source-map');
|
||||||
|
|
||||||
import type {Graph} from '../DeltaCalculator';
|
import type {Graph} from '../DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
import type {BabelSourceMap} from '@babel/core';
|
import type {BabelSourceMap} from '@babel/core';
|
||||||
|
|
||||||
function fullSourceMapObject(
|
function fullSourceMapObject(
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: {|+excludeSource: boolean|},
|
options: {|+excludeSource: boolean|},
|
||||||
): BabelSourceMap {
|
): BabelSourceMap {
|
||||||
|
@ -13,10 +13,10 @@
|
|||||||
const {fromRawMappings} = require('metro-source-map');
|
const {fromRawMappings} = require('metro-source-map');
|
||||||
|
|
||||||
import type {Graph} from '../DeltaCalculator';
|
import type {Graph} from '../DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../traverseDependencies';
|
import type {Module} from '../traverseDependencies';
|
||||||
|
|
||||||
function fullSourceMap(
|
function fullSourceMap(
|
||||||
pre: $ReadOnlyArray<DependencyEdge>,
|
pre: $ReadOnlyArray<Module>,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: {|+excludeSource: boolean|},
|
options: {|+excludeSource: boolean|},
|
||||||
): string {
|
): string {
|
||||||
|
@ -23,15 +23,10 @@ const {EventEmitter} = require('events');
|
|||||||
const DeltaCalculator = require('../DeltaCalculator');
|
const DeltaCalculator = require('../DeltaCalculator');
|
||||||
|
|
||||||
describe('DeltaCalculator', () => {
|
describe('DeltaCalculator', () => {
|
||||||
const entryModule = {path: '/bundle', name: 'bundle'};
|
let entryModule;
|
||||||
const moduleFoo = {path: '/foo', name: 'foo'};
|
let fooModule;
|
||||||
const moduleBar = {path: '/bar', name: 'bar'};
|
let barModule;
|
||||||
const moduleBaz = {path: '/baz', name: 'baz'};
|
let bazModule;
|
||||||
|
|
||||||
let edgeModule;
|
|
||||||
let edgeFoo;
|
|
||||||
let edgeBar;
|
|
||||||
let edgeBaz;
|
|
||||||
|
|
||||||
let deltaCalculator;
|
let deltaCalculator;
|
||||||
let fileWatcher;
|
let fileWatcher;
|
||||||
@ -61,48 +56,61 @@ describe('DeltaCalculator', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
initialTraverseDependencies.mockImplementationOnce(async (graph, opt) => {
|
initialTraverseDependencies.mockImplementationOnce(async (graph, opt) => {
|
||||||
edgeModule = {
|
entryModule = {
|
||||||
output: entryModule,
|
|
||||||
dependencies: new Map([
|
dependencies: new Map([
|
||||||
['foo', '/foo'],
|
['foo', '/foo'],
|
||||||
['bar', '/bar'],
|
['bar', '/bar'],
|
||||||
['baz', '/baz'],
|
['baz', '/baz'],
|
||||||
]),
|
]),
|
||||||
|
inverseDependencies: [],
|
||||||
|
output: {
|
||||||
|
name: 'bundle',
|
||||||
|
},
|
||||||
|
path: '/bundle',
|
||||||
};
|
};
|
||||||
edgeFoo = {
|
fooModule = {
|
||||||
output: moduleFoo,
|
|
||||||
dependencies: new Map(),
|
dependencies: new Map(),
|
||||||
inverseDependencies: ['/bundle'],
|
inverseDependencies: ['/bundle'],
|
||||||
|
output: {
|
||||||
|
name: 'foo',
|
||||||
|
},
|
||||||
|
path: '/foo',
|
||||||
};
|
};
|
||||||
edgeBar = {
|
barModule = {
|
||||||
output: moduleBar,
|
|
||||||
dependencies: new Map(),
|
dependencies: new Map(),
|
||||||
inverseDependencies: ['/bundle'],
|
inverseDependencies: ['/bundle'],
|
||||||
|
output: {
|
||||||
|
name: 'bar',
|
||||||
|
},
|
||||||
|
path: '/bar',
|
||||||
};
|
};
|
||||||
edgeBaz = {
|
bazModule = {
|
||||||
output: moduleBaz,
|
|
||||||
dependencies: new Map(),
|
dependencies: new Map(),
|
||||||
inverseDependencies: ['/bundle'],
|
inverseDependencies: ['/bundle'],
|
||||||
|
output: {
|
||||||
|
name: 'baz',
|
||||||
|
},
|
||||||
|
path: '/baz',
|
||||||
};
|
};
|
||||||
|
|
||||||
graph.dependencies.set('/bundle', edgeModule);
|
graph.dependencies.set('/bundle', entryModule);
|
||||||
graph.dependencies.set('/foo', edgeFoo);
|
graph.dependencies.set('/foo', fooModule);
|
||||||
graph.dependencies.set('/bar', edgeBar);
|
graph.dependencies.set('/bar', barModule);
|
||||||
graph.dependencies.set('/baz', edgeBaz);
|
graph.dependencies.set('/baz', bazModule);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
added: new Map([
|
added: new Map([
|
||||||
['/bundle', edgeModule],
|
['/bundle', entryModule],
|
||||||
['/foo', edgeFoo],
|
['/foo', fooModule],
|
||||||
['/bar', edgeBar],
|
['/bar', barModule],
|
||||||
['/baz', edgeBaz],
|
['/baz', bazModule],
|
||||||
]),
|
]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
deltaCalculator = new DeltaCalculator(
|
deltaCalculator = new DeltaCalculator(
|
||||||
[entryModule.path],
|
['/bundle'],
|
||||||
dependencyGraph,
|
dependencyGraph,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
@ -130,10 +138,10 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
modified: new Map([
|
modified: new Map([
|
||||||
['/bundle', edgeModule],
|
['/bundle', entryModule],
|
||||||
['/foo', edgeFoo],
|
['/foo', fooModule],
|
||||||
['/bar', edgeBar],
|
['/bar', barModule],
|
||||||
['/baz', edgeBaz],
|
['/baz', bazModule],
|
||||||
]),
|
]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
reset: true,
|
reset: true,
|
||||||
@ -161,10 +169,10 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
modified: new Map([
|
modified: new Map([
|
||||||
['/bundle', edgeModule],
|
['/bundle', entryModule],
|
||||||
['/foo', edgeFoo],
|
['/foo', fooModule],
|
||||||
['/bar', edgeBar],
|
['/bar', barModule],
|
||||||
['/baz', edgeBaz],
|
['/baz', bazModule],
|
||||||
]),
|
]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
reset: true,
|
reset: true,
|
||||||
@ -178,7 +186,7 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
traverseDependencies.mockReturnValue(
|
traverseDependencies.mockReturnValue(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
added: new Map([['/foo', edgeFoo]]),
|
added: new Map([['/foo', fooModule]]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -186,7 +194,7 @@ describe('DeltaCalculator', () => {
|
|||||||
const result = await deltaCalculator.getDelta({reset: false});
|
const result = await deltaCalculator.getDelta({reset: false});
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
modified: new Map([['/foo', edgeFoo]]),
|
modified: new Map([['/foo', fooModule]]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
reset: false,
|
reset: false,
|
||||||
});
|
});
|
||||||
@ -202,7 +210,7 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
traverseDependencies.mockReturnValue(
|
traverseDependencies.mockReturnValue(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
added: new Map([['/foo', edgeFoo]]),
|
added: new Map([['/foo', fooModule]]),
|
||||||
deleted: new Set(['/baz']),
|
deleted: new Set(['/baz']),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -210,7 +218,7 @@ describe('DeltaCalculator', () => {
|
|||||||
const result = await deltaCalculator.getDelta({reset: false});
|
const result = await deltaCalculator.getDelta({reset: false});
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
modified: new Map([['/foo', edgeFoo]]),
|
modified: new Map([['/foo', fooModule]]),
|
||||||
deleted: new Set(['/baz']),
|
deleted: new Set(['/baz']),
|
||||||
reset: false,
|
reset: false,
|
||||||
});
|
});
|
||||||
@ -224,23 +232,25 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
fileWatcher.emit('change', {eventsQueue: [{filePath: '/foo'}]});
|
fileWatcher.emit('change', {eventsQueue: [{filePath: '/foo'}]});
|
||||||
|
|
||||||
const edgeQux = {
|
const quxModule = {
|
||||||
output: {path: '/qux', name: 'qux'},
|
dependencies: new Map(),
|
||||||
inverseDependencies: [],
|
inverseDependencies: [],
|
||||||
|
output: {name: 'qux'},
|
||||||
|
path: '/qux',
|
||||||
};
|
};
|
||||||
|
|
||||||
traverseDependencies.mockImplementation(async (path, graph, options) => {
|
traverseDependencies.mockImplementation(async (path, graph, options) => {
|
||||||
graph.dependencies.set('/qux', edgeQux);
|
graph.dependencies.set('/qux', quxModule);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
added: new Map([['/foo', edgeFoo], ['/qux', edgeQux]]),
|
added: new Map([['/foo', fooModule], ['/qux', quxModule]]),
|
||||||
deleted: new Set(['/bar', '/baz']),
|
deleted: new Set(['/bar', '/baz']),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await deltaCalculator.getDelta({reset: false});
|
const result = await deltaCalculator.getDelta({reset: false});
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
modified: new Map([['/foo', edgeFoo], ['/qux', edgeQux]]),
|
modified: new Map([['/foo', fooModule], ['/qux', quxModule]]),
|
||||||
deleted: new Set(['/bar', '/baz']),
|
deleted: new Set(['/bar', '/baz']),
|
||||||
reset: false,
|
reset: false,
|
||||||
});
|
});
|
||||||
@ -301,13 +311,13 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
traverseDependencies.mockReturnValue(
|
traverseDependencies.mockReturnValue(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
added: new Map([['/bundle', edgeModule]]),
|
added: new Map([['/bundle', entryModule]]),
|
||||||
deleted: new Set(['/foo']),
|
deleted: new Set(['/foo']),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(await deltaCalculator.getDelta({reset: false})).toEqual({
|
expect(await deltaCalculator.getDelta({reset: false})).toEqual({
|
||||||
modified: new Map([['/bundle', edgeModule]]),
|
modified: new Map([['/bundle', entryModule]]),
|
||||||
deleted: new Set(['/foo']),
|
deleted: new Set(['/foo']),
|
||||||
reset: false,
|
reset: false,
|
||||||
});
|
});
|
||||||
@ -329,7 +339,7 @@ describe('DeltaCalculator', () => {
|
|||||||
|
|
||||||
traverseDependencies.mockReturnValue(
|
traverseDependencies.mockReturnValue(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
added: new Map([['/foo', edgeModule]]),
|
added: new Map([['/foo', entryModule]]),
|
||||||
deleted: new Set(),
|
deleted: new Set(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -90,7 +90,7 @@ describe('traverseDependencies', function() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const dependencies = recursive
|
const dependencies = recursive
|
||||||
? [...added.values()].map(edge => edge.path)
|
? [...added.values()].map(module => module.path)
|
||||||
: graph.dependencies.get(entryPath).dependencies.values();
|
: graph.dependencies.get(entryPath).dependencies.values();
|
||||||
|
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
|
@ -88,7 +88,7 @@ function deferred(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getPaths({added, deleted}) {
|
function getPaths({added, deleted}) {
|
||||||
const addedPaths = [...added.values()].map(edge => edge.path);
|
const addedPaths = [...added.values()].map(module => module.path);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
added: new Set(addedPaths),
|
added: new Set(addedPaths),
|
||||||
|
@ -14,7 +14,7 @@ import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
|
|||||||
|
|
||||||
export type DependencyType = 'module' | 'script' | 'asset';
|
export type DependencyType = 'module' | 'script' | 'asset';
|
||||||
|
|
||||||
export type DependencyEdge = {|
|
export type Module = {|
|
||||||
dependencies: Map<string, string>,
|
dependencies: Map<string, string>,
|
||||||
inverseDependencies: Set<string>,
|
inverseDependencies: Set<string>,
|
||||||
path: string,
|
path: string,
|
||||||
@ -26,14 +26,12 @@ export type DependencyEdge = {|
|
|||||||
},
|
},
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export type DependencyEdges = Map<string, DependencyEdge>;
|
|
||||||
|
|
||||||
export type Graph = {|
|
export type Graph = {|
|
||||||
dependencies: DependencyEdges,
|
dependencies: Map<string, Module>,
|
||||||
entryPoints: $ReadOnlyArray<string>,
|
entryPoints: $ReadOnlyArray<string>,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
type Result = {added: Map<string, DependencyEdge>, deleted: Set<string>};
|
type Result = {added: Map<string, Module>, deleted: Set<string>};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal data structure that the traversal logic uses to know which of the
|
* Internal data structure that the traversal logic uses to know which of the
|
||||||
@ -42,8 +40,8 @@ type Result = {added: Map<string, DependencyEdge>, deleted: Set<string>};
|
|||||||
* just has been modified).
|
* just has been modified).
|
||||||
**/
|
**/
|
||||||
type Delta = {
|
type Delta = {
|
||||||
added: Map<string, DependencyEdge>,
|
added: Map<string, Module>,
|
||||||
modified: Map<string, DependencyEdge>,
|
modified: Map<string, Module>,
|
||||||
deleted: Set<string>,
|
deleted: Set<string>,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,10 +67,7 @@ export type Options = {|
|
|||||||
* dependency graph.
|
* dependency graph.
|
||||||
* Instead of traversing the whole graph each time, it just calculates the
|
* Instead of traversing the whole graph each time, it just calculates the
|
||||||
* difference between runs by only traversing the added/removed dependencies.
|
* difference between runs by only traversing the added/removed dependencies.
|
||||||
* To do so, it uses the passed `edges` paramater, which is a data structure
|
* To do so, it uses the passed passed graph dependencies and it mutates it.
|
||||||
* that contains the whole status of the dependency graph. During the
|
|
||||||
* recalculation of the dependencies, it mutates the edges graph.
|
|
||||||
*
|
|
||||||
* The paths parameter contains the absolute paths of the root files that the
|
* The paths parameter contains the absolute paths of the root files that the
|
||||||
* method should traverse. Normally, these paths should be the modified files
|
* method should traverse. Normally, these paths should be the modified files
|
||||||
* since the last traversal.
|
* since the last traversal.
|
||||||
@ -90,15 +85,15 @@ async function traverseDependencies(
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
paths.map(async path => {
|
paths.map(async path => {
|
||||||
const edge = graph.dependencies.get(path);
|
const module = graph.dependencies.get(path);
|
||||||
|
|
||||||
if (!edge) {
|
if (!module) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
delta.modified.set(edge.path, edge);
|
delta.modified.set(module.path, module);
|
||||||
|
|
||||||
await traverseDependenciesForSingleFile(edge, graph, delta, options);
|
await traverseDependenciesForSingleFile(module, graph, delta, options);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -106,13 +101,13 @@ async function traverseDependencies(
|
|||||||
const deleted = new Set();
|
const deleted = new Set();
|
||||||
const modified = new Map();
|
const modified = new Map();
|
||||||
|
|
||||||
for (const [path, edge] of delta.added) {
|
for (const [path, module] of delta.added) {
|
||||||
added.set(path, edge);
|
added.set(path, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [path, edge] of delta.modified) {
|
for (const [path, module] of delta.modified) {
|
||||||
added.set(path, edge);
|
added.set(path, module);
|
||||||
modified.set(path, edge);
|
modified.set(path, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const path of delta.deleted) {
|
for (const path of delta.deleted) {
|
||||||
@ -139,7 +134,7 @@ async function initialTraverseDependencies(
|
|||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<Result> {
|
): Promise<Result> {
|
||||||
graph.entryPoints.forEach(entryPoint => createEdge(entryPoint, graph));
|
graph.entryPoints.forEach(entryPoint => createModule(entryPoint, graph));
|
||||||
|
|
||||||
await traverseDependencies(graph.entryPoints, graph, options);
|
await traverseDependencies(graph.entryPoints, graph, options);
|
||||||
|
|
||||||
@ -152,7 +147,7 @@ async function initialTraverseDependencies(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function traverseDependenciesForSingleFile(
|
async function traverseDependenciesForSingleFile(
|
||||||
edge: DependencyEdge,
|
module: Module,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
delta: Delta,
|
delta: Delta,
|
||||||
options: Options,
|
options: Options,
|
||||||
@ -161,8 +156,8 @@ async function traverseDependenciesForSingleFile(
|
|||||||
let total = 1;
|
let total = 1;
|
||||||
options.onProgress && options.onProgress(numProcessed, total);
|
options.onProgress && options.onProgress(numProcessed, total);
|
||||||
|
|
||||||
await processEdge(
|
await processModule(
|
||||||
edge,
|
module,
|
||||||
graph,
|
graph,
|
||||||
delta,
|
delta,
|
||||||
options,
|
options,
|
||||||
@ -180,37 +175,37 @@ async function traverseDependenciesForSingleFile(
|
|||||||
options.onProgress && options.onProgress(numProcessed, total);
|
options.onProgress && options.onProgress(numProcessed, total);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processEdge(
|
async function processModule(
|
||||||
edge: DependencyEdge,
|
module: Module,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
delta: Delta,
|
delta: Delta,
|
||||||
options: Options,
|
options: Options,
|
||||||
onDependencyAdd: () => mixed,
|
onDependencyAdd: () => mixed,
|
||||||
onDependencyAdded: () => mixed,
|
onDependencyAdded: () => mixed,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const previousDependencies = edge.dependencies;
|
const previousDependencies = module.dependencies;
|
||||||
|
|
||||||
const result = await options.transform(edge.path);
|
const result = await options.transform(module.path);
|
||||||
|
|
||||||
// Get the absolute path of all sub-dependencies (some of them could have been
|
// Get the absolute path of all sub-dependencies (some of them could have been
|
||||||
// moved but maintain the same relative path).
|
// moved but maintain the same relative path).
|
||||||
const currentDependencies = resolveDependencies(
|
const currentDependencies = resolveDependencies(
|
||||||
edge.path,
|
module.path,
|
||||||
result.dependencies,
|
result.dependencies,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update the edge information.
|
// Update the module information.
|
||||||
edge.output = result.output;
|
module.output = result.output;
|
||||||
edge.dependencies = new Map();
|
module.dependencies = new Map();
|
||||||
|
|
||||||
currentDependencies.forEach((absolutePath, relativePath) => {
|
currentDependencies.forEach((absolutePath, relativePath) => {
|
||||||
edge.dependencies.set(relativePath, absolutePath);
|
module.dependencies.set(relativePath, absolutePath);
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const [relativePath, absolutePath] of previousDependencies) {
|
for (const [relativePath, absolutePath] of previousDependencies) {
|
||||||
if (!currentDependencies.has(relativePath)) {
|
if (!currentDependencies.has(relativePath)) {
|
||||||
removeDependency(edge, absolutePath, graph, delta);
|
removeDependency(module, absolutePath, graph, delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,7 +220,7 @@ async function processEdge(
|
|||||||
}
|
}
|
||||||
|
|
||||||
await addDependency(
|
await addDependency(
|
||||||
edge,
|
module,
|
||||||
absolutePath,
|
absolutePath,
|
||||||
graph,
|
graph,
|
||||||
delta,
|
delta,
|
||||||
@ -239,7 +234,7 @@ async function processEdge(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function addDependency(
|
async function addDependency(
|
||||||
parentEdge: DependencyEdge,
|
parentModule: Module,
|
||||||
path: string,
|
path: string,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
delta: Delta,
|
delta: Delta,
|
||||||
@ -247,24 +242,24 @@ async function addDependency(
|
|||||||
onDependencyAdd: () => mixed,
|
onDependencyAdd: () => mixed,
|
||||||
onDependencyAdded: () => mixed,
|
onDependencyAdded: () => mixed,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const existingEdge = graph.dependencies.get(path);
|
const existingModule = graph.dependencies.get(path);
|
||||||
|
|
||||||
// The new dependency was already in the graph, we don't need to do anything.
|
// The new dependency was already in the graph, we don't need to do anything.
|
||||||
if (existingEdge) {
|
if (existingModule) {
|
||||||
existingEdge.inverseDependencies.add(parentEdge.path);
|
existingModule.inverseDependencies.add(parentModule.path);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const edge = createEdge(path, graph);
|
const module = createModule(path, graph);
|
||||||
|
|
||||||
edge.inverseDependencies.add(parentEdge.path);
|
module.inverseDependencies.add(parentModule.path);
|
||||||
delta.added.set(edge.path, edge);
|
delta.added.set(module.path, module);
|
||||||
|
|
||||||
onDependencyAdd();
|
onDependencyAdd();
|
||||||
|
|
||||||
await processEdge(
|
await processModule(
|
||||||
edge,
|
module,
|
||||||
graph,
|
graph,
|
||||||
delta,
|
delta,
|
||||||
options,
|
options,
|
||||||
@ -276,40 +271,40 @@ async function addDependency(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function removeDependency(
|
function removeDependency(
|
||||||
parentEdge: DependencyEdge,
|
parentModule: Module,
|
||||||
absolutePath: string,
|
absolutePath: string,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
delta: Delta,
|
delta: Delta,
|
||||||
): void {
|
): void {
|
||||||
const edge = graph.dependencies.get(absolutePath);
|
const module = graph.dependencies.get(absolutePath);
|
||||||
|
|
||||||
if (!edge) {
|
if (!module) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
edge.inverseDependencies.delete(parentEdge.path);
|
module.inverseDependencies.delete(parentModule.path);
|
||||||
|
|
||||||
// This module is still used by another modules, so we cannot remove it from
|
// This module is still used by another modules, so we cannot remove it from
|
||||||
// the bundle.
|
// the bundle.
|
||||||
if (edge.inverseDependencies.size) {
|
if (module.inverseDependencies.size) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
delta.deleted.add(edge.path);
|
delta.deleted.add(module.path);
|
||||||
|
|
||||||
// Now we need to iterate through the module dependencies in order to
|
// Now we need to iterate through the module dependencies in order to
|
||||||
// clean up everything (we cannot read the module because it may have
|
// clean up everything (we cannot read the module because it may have
|
||||||
// been deleted).
|
// been deleted).
|
||||||
for (const depAbsolutePath of edge.dependencies.values()) {
|
for (const depAbsolutePath of module.dependencies.values()) {
|
||||||
removeDependency(edge, depAbsolutePath, graph, delta);
|
removeDependency(module, depAbsolutePath, graph, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This module is not used anywhere else!! we can clear it from the bundle
|
// This module is not used anywhere else!! we can clear it from the bundle
|
||||||
destroyEdge(edge, graph);
|
graph.dependencies.delete(module.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createEdge(filePath: string, graph: Graph): DependencyEdge {
|
function createModule(filePath: string, graph: Graph): Module {
|
||||||
const edge = {
|
const module = {
|
||||||
dependencies: new Map(),
|
dependencies: new Map(),
|
||||||
inverseDependencies: new Set(),
|
inverseDependencies: new Set(),
|
||||||
path: filePath,
|
path: filePath,
|
||||||
@ -320,13 +315,9 @@ function createEdge(filePath: string, graph: Graph): DependencyEdge {
|
|||||||
type: 'module',
|
type: 'module',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
graph.dependencies.set(filePath, edge);
|
graph.dependencies.set(filePath, module);
|
||||||
|
|
||||||
return edge;
|
return module;
|
||||||
}
|
|
||||||
|
|
||||||
function destroyEdge(edge: DependencyEdge, graph: Graph) {
|
|
||||||
graph.dependencies.delete(edge.path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveDependencies(
|
function resolveDependencies(
|
||||||
@ -357,19 +348,19 @@ function reorderGraph(graph: Graph) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function reorderDependencies(
|
function reorderDependencies(
|
||||||
edge: {|dependencies: Map<string, string>|} | DependencyEdge,
|
module: {|dependencies: Map<string, string>|} | Module,
|
||||||
dependencies: Map<string, DependencyEdge>,
|
dependencies: Map<string, Module>,
|
||||||
orderedDependencies?: Map<string, DependencyEdge> = new Map(),
|
orderedDependencies?: Map<string, Module> = new Map(),
|
||||||
): Map<string, DependencyEdge> {
|
): Map<string, Module> {
|
||||||
if (edge.path) {
|
if (module.path) {
|
||||||
if (orderedDependencies.has(edge.path)) {
|
if (orderedDependencies.has(module.path)) {
|
||||||
return orderedDependencies;
|
return orderedDependencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
orderedDependencies.set(edge.path, edge);
|
orderedDependencies.set(module.path, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
edge.dependencies.forEach(path => {
|
module.dependencies.forEach(path => {
|
||||||
const dep = dependencies.get(path);
|
const dep = dependencies.get(path);
|
||||||
if (dep) {
|
if (dep) {
|
||||||
reorderDependencies(dep, dependencies, orderedDependencies);
|
reorderDependencies(dep, dependencies, orderedDependencies);
|
||||||
|
@ -43,7 +43,7 @@ const {getAsset} = require('./Assets');
|
|||||||
const resolveSync: ResolveSync = require('resolve').sync;
|
const resolveSync: ResolveSync = require('resolve').sync;
|
||||||
|
|
||||||
import type {CustomError} from './lib/formatBundlingError';
|
import type {CustomError} from './lib/formatBundlingError';
|
||||||
import type {DependencyEdge} from './DeltaBundler/traverseDependencies';
|
import type {Module} from './DeltaBundler/traverseDependencies';
|
||||||
import type {IncomingMessage, ServerResponse} from 'http';
|
import type {IncomingMessage, ServerResponse} from 'http';
|
||||||
import type {Reporter} from './lib/reporting';
|
import type {Reporter} from './lib/reporting';
|
||||||
import type {RamBundleInfo} from './DeltaBundler/Serializers/getRamBundleInfo';
|
import type {RamBundleInfo} from './DeltaBundler/Serializers/getRamBundleInfo';
|
||||||
@ -73,7 +73,7 @@ type ResolveSync = (path: string, opts: ?{baseDir?: string}) => string;
|
|||||||
|
|
||||||
type GraphInfo = {|
|
type GraphInfo = {|
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
prepend: $ReadOnlyArray<DependencyEdge>,
|
prepend: $ReadOnlyArray<Module>,
|
||||||
lastModified: Date,
|
lastModified: Date,
|
||||||
+sequenceId: string,
|
+sequenceId: string,
|
||||||
|};
|
|};
|
||||||
@ -526,7 +526,7 @@ class Server {
|
|||||||
return {...graphInfo, prepend, graph};
|
return {...graphInfo, prepend, graph};
|
||||||
}
|
}
|
||||||
|
|
||||||
async _minifyModule(module: DependencyEdge): Promise<DependencyEdge> {
|
async _minifyModule(module: Module): Promise<Module> {
|
||||||
const {code, map} = await this._bundler.minifyModule(
|
const {code, map} = await this._bundler.minifyModule(
|
||||||
module.path,
|
module.path,
|
||||||
module.output.code,
|
module.output.code,
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import type {Graph} from '../DeltaBundler/DeltaCalculator';
|
import type {Graph} from '../DeltaBundler/DeltaCalculator';
|
||||||
import type {DependencyEdge} from '../DeltaBundler/traverseDependencies';
|
import type {Module} from '../DeltaBundler/traverseDependencies';
|
||||||
|
|
||||||
type Options<T: number | string> = {
|
type Options<T: number | string> = {
|
||||||
+createModuleId: string => T,
|
+createModuleId: string => T,
|
||||||
@ -25,7 +25,7 @@ function getAppendScripts<T: number | string>(
|
|||||||
entryPoint: string,
|
entryPoint: string,
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
options: Options<T>,
|
options: Options<T>,
|
||||||
): $ReadOnlyArray<DependencyEdge> {
|
): $ReadOnlyArray<Module> {
|
||||||
const output = [];
|
const output = [];
|
||||||
|
|
||||||
if (options.runModule) {
|
if (options.runModule) {
|
||||||
|
@ -15,7 +15,7 @@ const getPreludeCode = require('./getPreludeCode');
|
|||||||
const transformHelpers = require('./transformHelpers');
|
const transformHelpers = require('./transformHelpers');
|
||||||
|
|
||||||
import type Bundler from '../Bundler';
|
import type Bundler from '../Bundler';
|
||||||
import type {DependencyEdge} from '../DeltaBundler/traverseDependencies';
|
import type {Module} from '../DeltaBundler/traverseDependencies';
|
||||||
import type DeltaBundler from '../DeltaBundler';
|
import type DeltaBundler from '../DeltaBundler';
|
||||||
import type {CustomTransformOptions} from '../JSTransformer/worker';
|
import type {CustomTransformOptions} from '../JSTransformer/worker';
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ async function getPrependedScripts(
|
|||||||
bundleOptions: BundleOptions,
|
bundleOptions: BundleOptions,
|
||||||
bundler: Bundler,
|
bundler: Bundler,
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
): Promise<Array<DependencyEdge>> {
|
): Promise<Array<Module>> {
|
||||||
// Get all the polyfills from the relevant option params (the
|
// Get all the polyfills from the relevant option params (the
|
||||||
// `getPolyfills()` method and the `polyfillModuleNames` variable).
|
// `getPolyfills()` method and the `polyfillModuleNames` variable).
|
||||||
const polyfillModuleNames = options
|
const polyfillModuleNames = options
|
||||||
@ -80,7 +80,7 @@ async function getPrependedScripts(
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getPrelude({dev}: {dev: boolean}): DependencyEdge {
|
function _getPrelude({dev}: {dev: boolean}): Module {
|
||||||
const code = getPreludeCode({isDev: dev});
|
const code = getPreludeCode({isDev: dev});
|
||||||
const name = '__prelude__';
|
const name = '__prelude__';
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import type {DependencyEdge} from '../DeltaBundler/traverseDependencies';
|
import type {Module} from '../DeltaBundler/traverseDependencies';
|
||||||
import type {Graph} from '../DeltaBundler';
|
import type {Graph} from '../DeltaBundler';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,7 +19,7 @@ import type {Graph} from '../DeltaBundler';
|
|||||||
**/
|
**/
|
||||||
async function mapGraph(
|
async function mapGraph(
|
||||||
graph: Graph,
|
graph: Graph,
|
||||||
mappingFn: DependencyEdge => Promise<DependencyEdge>,
|
mappingFn: Module => Promise<Module>,
|
||||||
): Promise<Graph> {
|
): Promise<Graph> {
|
||||||
const dependencies = new Map(
|
const dependencies = new Map(
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user