Stop using Module.read() for transforming files

Reviewed By: jeanlauliac

Differential Revision: D7894083

fbshipit-source-id: 27685280cca1d6eb219bd7cbfc33b8b05a580296
This commit is contained in:
Rafael Oleza 2018-05-11 15:05:21 -07:00 committed by Facebook Github Bot
parent 51dd867f45
commit 9a1bfe6388
5 changed files with 30 additions and 32 deletions

View File

@ -17,13 +17,18 @@ const assert = require('assert');
const defaults = require('./defaults');
const fs = require('fs');
const getTransformCacheKeyFn = require('./lib/getTransformCacheKeyFn');
const toLocalPath = require('./node-haste/lib/toLocalPath');
const {Cache, stableHash} = require('metro-cache');
import type {TransformedCode, WorkerOptions} from './JSTransformer/worker';
import type {TransformResult} from './DeltaBundler/traverseDependencies';
import type {
JsOutput,
WorkerOptions,
TransformedCode,
} from './JSTransformer/worker';
import type {DynamicRequiresBehavior} from './ModuleGraph/worker/collectDependencies';
import type {Reporter} from './lib/reporting';
import type Module from './node-haste/Module';
import type {BabelSourceMap} from '@babel/core';
import type {CacheStore} from 'metro-cache';
import type {CustomResolver} from 'metro-resolver';
@ -147,7 +152,7 @@ class Bundler {
reporter: opts.reporter,
resolveRequest: opts.resolveRequest,
sourceExts: opts.sourceExts,
transformCode: this._cachedTransformCode.bind(this),
transformCode: this.transformFile.bind(this),
watch: opts.watch,
});
@ -215,10 +220,10 @@ class Bundler {
return this._depGraphPromise;
}
async _cachedTransformCode(
module: Module,
async transformFile(
filePath: string,
transformCodeOptions: WorkerOptions,
): Promise<TransformedCode> {
): Promise<TransformResult<JsOutput>> {
const cache = this._cache;
const {
@ -243,12 +248,14 @@ class Bundler {
}
}
const localPath = toLocalPath(this._projectRoots, filePath);
const partialKey = stableHash([
// This is the hash related to the global Bundler config.
this._baseHash,
// Path.
module.localPath,
localPath,
// We cannot include "transformCodeOptions" because of "projectRoot".
assetDataPlugins,
@ -262,7 +269,7 @@ class Bundler {
platform,
]);
const sha1 = (await this.getDependencyGraph()).getSha1(module.path);
const sha1 = (await this.getDependencyGraph()).getSha1(filePath);
let fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
const result = await cache.get(fullKey);
@ -271,8 +278,8 @@ class Bundler {
const data = result
? {result, sha1}
: await this._transformer.transform(
module.path,
module.localPath,
filePath,
localPath,
transformCodeOptions,
this._opts.assetExts,
this._opts.assetRegistryPath,
@ -288,7 +295,12 @@ class Bundler {
cache.set(fullKey, data.result);
return data.result;
return {
...data.result,
getSource() {
return fs.readFileSync(filePath, 'utf8');
},
};
}
}

View File

@ -127,7 +127,7 @@ describe('Bundler', function() {
result: {},
});
await bundlerInstance._cachedTransformCode(module, {});
await bundlerInstance.transformFile(module.path, {});
// We got the SHA-1 of the file from the dependency graph.
expect(depGraph.getSha1).toBeCalledWith('/root/foo.js');

View File

@ -44,11 +44,13 @@ type Delta<T> = {
deleted: Set<string>,
};
export type TransformFn<T> = string => Promise<{|
export type TransformResult<T> = {|
dependencies: $ReadOnlyArray<TransformResultDependency>,
output: $ReadOnlyArray<T>,
+getSource: () => string,
|}>;
|};
export type TransformFn<T> = string => Promise<TransformResult<T>>;
export type Options<T> = {|
resolve: (from: string, to: string) => string,

View File

@ -89,7 +89,6 @@ async function getTransformFn(
deltaBundler: DeltaBundler<JsOutput>,
options: BuildGraphOptions,
): Promise<TransformFn<JsOutput>> {
const dependencyGraph = await bundler.getDependencyGraph();
const {inlineRequires, ...transformerOptions} = await calcTransformerOptions(
entryFiles,
bundler,
@ -98,25 +97,13 @@ async function getTransformFn(
);
return async (path: string) => {
const module = dependencyGraph.getModuleForPath(
path,
options.type === 'script',
);
const result = await module.read({
return await bundler.transformFile(path, {
...transformerOptions,
inlineRequires: removeInlineRequiresBlacklistFromOptions(
path,
inlineRequires,
),
});
return {
getSource() {
return result.source;
},
output: result.output,
dependencies: result.dependencies,
};
};
}

View File

@ -22,10 +22,7 @@ type ReadResult = {
+source: string,
};
export type TransformCode = (
module: Module,
transformOptions: WorkerOptions,
) => Promise<TransformedCode>;
export type TransformCode = Function;
export type ConstructorArgs = {
file: string,