diff --git a/packages/metro/src/Bundler.js b/packages/metro/src/Bundler.js index de6707fa..e38c81b7 100644 --- a/packages/metro/src/Bundler.js +++ b/packages/metro/src/Bundler.js @@ -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 { + ): Promise> { 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'); + }, + }; } } diff --git a/packages/metro/src/Bundler/__tests__/Bundler-test.js b/packages/metro/src/Bundler/__tests__/Bundler-test.js index 567c9349..e377735b 100644 --- a/packages/metro/src/Bundler/__tests__/Bundler-test.js +++ b/packages/metro/src/Bundler/__tests__/Bundler-test.js @@ -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'); diff --git a/packages/metro/src/DeltaBundler/traverseDependencies.js b/packages/metro/src/DeltaBundler/traverseDependencies.js index 8a5069f5..c2fa4f36 100644 --- a/packages/metro/src/DeltaBundler/traverseDependencies.js +++ b/packages/metro/src/DeltaBundler/traverseDependencies.js @@ -44,11 +44,13 @@ type Delta = { deleted: Set, }; -export type TransformFn = string => Promise<{| +export type TransformResult = {| dependencies: $ReadOnlyArray, output: $ReadOnlyArray, +getSource: () => string, -|}>; +|}; + +export type TransformFn = string => Promise>; export type Options = {| resolve: (from: string, to: string) => string, diff --git a/packages/metro/src/lib/transformHelpers.js b/packages/metro/src/lib/transformHelpers.js index 75d58b29..8d3510ac 100644 --- a/packages/metro/src/lib/transformHelpers.js +++ b/packages/metro/src/lib/transformHelpers.js @@ -89,7 +89,6 @@ async function getTransformFn( deltaBundler: DeltaBundler, options: BuildGraphOptions, ): Promise> { - 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, - }; }; } diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index 307748ad..857fec7c 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -22,10 +22,7 @@ type ReadResult = { +source: string, }; -export type TransformCode = ( - module: Module, - transformOptions: WorkerOptions, -) => Promise; +export type TransformCode = Function; export type ConstructorArgs = { file: string,