Pass the full dependencies object

Reviewed By: jeanlauliac

Differential Revision: D7731623

fbshipit-source-id: 070ab5f9909525102e19f782e8a26e79d7b880cc
This commit is contained in:
Miguel Jimenez Esun 2018-04-24 09:42:33 -07:00 committed by Facebook Github Bot
parent 2efc8bef2f
commit 884d2ca248
8 changed files with 658 additions and 175 deletions

View File

@ -30,7 +30,7 @@ const extractDependencies = function(sourceCode: string) {
let match;
while ((match = regexp.exec(sourceCode))) {
deps.push(match[2]);
deps.push({name: match[2], isAsync: false});
}
return deps;

View File

@ -131,11 +131,8 @@ beforeEach(async () => {
return path;
},
transform: path => {
const deps = mockedDependencyTree.get(path);
const dependencies = deps ? deps.map(dep => dep.name) : [];
return {
dependencies,
dependencies: mockedDependencyTree.get(path) || [],
output: {
code: '// code',
map: [],

View File

@ -10,6 +10,7 @@
'use strict';
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
export type DependencyType = 'module' | 'script' | 'asset';
@ -46,7 +47,7 @@ type Delta = {
};
export type TransformFn = string => Promise<{
dependencies: $ReadOnlyArray<string>,
dependencies: $ReadOnlyArray<TransformResultDependency>,
output: {
+code: string,
+map: Array<MetroSourceMapSegmentTuple>,
@ -322,13 +323,13 @@ function createModule(filePath: string, graph: Graph): Module {
function resolveDependencies(
parentPath: string,
dependencies: $ReadOnlyArray<string>,
dependencies: $ReadOnlyArray<TransformResultDependency>,
options: Options,
): Map<string, string> {
return new Map(
dependencies.map(relativePath => [
relativePath,
options.resolve(parentPath, relativePath),
relativePath.name,
options.resolve(parentPath, relativePath.name),
]),
);
}

View File

@ -30,6 +30,7 @@ const {
toSegmentTuple,
} = require('metro-source-map');
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {Ast} from '@babel/core';
@ -39,7 +40,7 @@ import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
export type TransformedCode = {
code: string,
dependencies: $ReadOnlyArray<string>,
dependencies: $ReadOnlyArray<TransformResultDependency>,
map: Array<MetroSourceMapSegmentTuple>,
};
@ -249,8 +250,6 @@ async function transformCode(
wrapped.requireName,
);
}
dependencies = dependencies.map(dep => dep.name);
}
const result = generate(

View File

@ -126,7 +126,11 @@ describe('code transformation worker:', () => {
].join('\n'),
);
expect(result.map).toHaveLength(14);
expect(result.dependencies).toEqual(['./c', './a', 'b']);
expect(result.dependencies).toEqual([
{isAsync: false, name: './c'},
{isAsync: false, name: './a'},
{isAsync: false, name: 'b'},
]);
});
} else {
it(`transforms a module with dependencies (v${BABEL_VERSION})`, async () => {

View File

@ -30,6 +30,7 @@ const {
} = require('metro-core');
import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
import type {Reporter} from '../lib/reporting';
import type {ModuleMap} from './DependencyGraph/ModuleResolution';
import type {TransformCode} from './Module';
@ -211,7 +212,7 @@ class DependencyGraph extends EventEmitter {
async getShallowDependencies(
entryPath: string,
transformOptions: JSTransformerOptions,
): Promise<$ReadOnlyArray<string>> {
): Promise<$ReadOnlyArray<TransformResultDependency>> {
const module = this._moduleCache.getModule(entryPath);
const result = await module.read(transformOptions);

View File

@ -17,13 +17,14 @@ import type {
TransformedCode,
Options as WorkerOptions,
} from '../JSTransformer/worker';
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
import type ModuleCache from './ModuleCache';
import type {LocalPath} from './lib/toLocalPath';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
export type ReadResult = {
+code: string,
+dependencies: $ReadOnlyArray<string>,
+dependencies: $ReadOnlyArray<TransformResultDependency>,
+map: Array<MetroSourceMapSegmentTuple>,
+source: string,
};