Skip to content

Commit

Permalink
Remove absolute path parameter from transformers
Browse files Browse the repository at this point in the history
Reviewed By: mjesun

Differential Revision: D9195147

fbshipit-source-id: e95c5c29272eec7f1a48af7cede4c6d6b4a8ffb7
  • Loading branch information
rafeca authored and facebook-github-bot committed Sep 24, 2018
1 parent b940ef7 commit 75d9991
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 144 deletions.
10 changes: 5 additions & 5 deletions packages/metro/src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const WorkerFarm = require('./DeltaBundler/WorkerFarm');
const assert = require('assert');
const fs = require('fs');
const getTransformCacheKeyFn = require('./lib/getTransformCacheKeyFn');
const toLocalPath = require('./node-haste/lib/toLocalPath');
const path = require('path');

const {Cache, stableHash} = require('metro-cache');

Expand Down Expand Up @@ -128,14 +128,14 @@ class Bundler {
}
}

const localPath = toLocalPath(this._opts.watchFolders, filePath);
const filename = path.relative(this._opts.projectRoot, filePath);

const partialKey = stableHash([
// This is the hash related to the global Bundler config.
this._baseHash,

// Path.
localPath,
filename,

// We cannot include "transformCodeOptions" because of "projectRoot".
assetPlugins,
Expand Down Expand Up @@ -165,8 +165,8 @@ class Bundler {
const data = result
? {result, sha1}
: await this._transformer.transform(
filePath,
localPath,
filename,
_projectRoot,
this._opts.transformerPath,
workerOptions,
);
Expand Down
9 changes: 4 additions & 5 deletions packages/metro/src/DeltaBundler/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@

const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

import type {WorkerOptions as JsWorkerOptions} from '../JSTransformer/worker';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {MixedOutput, TransformResultDependency} from './types.flow';
import type {LogEntry} from 'metro-core/src/Logger';

export type WorkerOptions = JsWorkerOptions;
export type WorkerFn = typeof transform;
export type TransformerFn<T: MixedOutput> = (
string,
LocalPath,
Buffer,
WorkerOptions,
) => Promise<Result<T>>;
Expand All @@ -41,7 +40,7 @@ type Data<T: MixedOutput> = {

async function transform<T: MixedOutput>(
filename: string,
localPath: LocalPath,
projectRoot: string,
transformerPath: string,
transformerOptions: WorkerOptions,
): Promise<Data<T>> {
Expand All @@ -53,7 +52,7 @@ async function transform<T: MixedOutput>(
start_timestamp: process.hrtime(),
};

const data = fs.readFileSync(filename);
const data = fs.readFileSync(path.resolve(projectRoot, filename));
const sha1 = crypto
.createHash('sha1')
.update(data)
Expand All @@ -65,7 +64,7 @@ async function transform<T: MixedOutput>(
transform: TransformerFn<T>,
});

const result = await transform(filename, localPath, data, transformerOptions);
const result = await transform(filename, data, transformerOptions);

const transformFileEndLogEntry = getEndLogEntry(
transformFileStartLogEntry,
Expand Down
4 changes: 2 additions & 2 deletions packages/metro/src/DeltaBundler/WorkerFarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ class WorkerFarm {

async transform(
filename: string,
localPath: string,
projectRoot: string,
transformerPath: string,
options: WorkerOptions,
): Promise<TransformerResult> {
try {
const data = await this._worker.transform(
filename,
localPath,
projectRoot,
transformerPath,
options,
);
Expand Down
28 changes: 13 additions & 15 deletions packages/metro/src/DeltaBundler/__tests__/WorkerFarm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const {Readable} = require('stream');
describe('Worker Farm', function() {
let api;
let WorkerFarm;
const fileName = '/an/arbitrary/file.js';
const localPath = 'arbitrary/file.js';
const fileName = 'arbitrary/file.js';
const rootFolder = '/root';
const config = getDefaultConfig();

const opts = {
Expand Down Expand Up @@ -70,14 +70,14 @@ describe('Worker Farm', function() {

await new WorkerFarm(opts).transform(
fileName,
localPath,
rootFolder,
config.transformerPath,
transformOptions,
);

expect(api.transform).toBeCalledWith(
fileName,
localPath,
rootFolder,
config.transformerPath,
transformOptions,
);
Expand All @@ -88,26 +88,24 @@ describe('Worker Farm', function() {
const message = 'message';
const snippet = 'snippet';

api.transform.mockImplementation(
(filename, localPth, transformPath, opts) => {
const babelError = new SyntaxError(message);
api.transform.mockImplementation((filename, transformPath, opts) => {
const babelError = new SyntaxError(message);

babelError.type = 'SyntaxError';
babelError.loc = {line: 2, column: 15};
babelError.codeFrame = snippet;
babelError.type = 'SyntaxError';
babelError.loc = {line: 2, column: 15};
babelError.codeFrame = snippet;

return Promise.reject(babelError);
},
);
return Promise.reject(babelError);
});

expect.assertions(6);

return workerFarm
.transform(fileName, localPath, '', true, {})
.transform(fileName, rootFolder, '', {})
.catch(function(error) {
expect(error.type).toEqual('TransformError');
expect(error.message).toBe(
'SyntaxError in /an/arbitrary/file.js: ' + message,
'SyntaxError in arbitrary/file.js: ' + message,
);
expect(error.lineNumber).toBe(2);
expect(error.column).toBe(15);
Expand Down
8 changes: 2 additions & 6 deletions packages/metro/src/JSTransformer/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ const {

import type {TransformResultDependency} from 'metro/src/DeltaBundler';
import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {Ast} from '@babel/core';
import type {Plugins as BabelPlugins} from 'babel-core';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';

export type TransformArgs = {|
filename: string,
localPath: string,
options: TransformOptions,
plugins?: BabelPlugins,
src: string,
Expand Down Expand Up @@ -124,7 +122,6 @@ function getDynamicDepsBehavior(

async function transform(
filename: string,
localPath: LocalPath,
data: Buffer,
options: WorkerOptions,
): Promise<Result> {
Expand Down Expand Up @@ -160,7 +157,6 @@ async function transform(

const transformerArgs = {
filename,
localPath,
options: {
...options.transformOptions,
// Inline requires are now performed at a secondary step. We cannot
Expand Down Expand Up @@ -229,7 +225,7 @@ async function transform(
configFile: false,
comments: false,
compact: false,
filename: localPath,
filename,
plugins,
sourceMaps: false,
}));
Expand Down Expand Up @@ -283,7 +279,7 @@ async function transform(
{
comments: false,
compact: false,
filename: localPath,
filename,
retainLines: false,
sourceFileName: filename,
sourceMaps: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`code transformation worker: reports filename when encountering unsupported dynamic dependency 1`] = `"/root/local/file.js:Invalid call at line 3: require(a)"`;
exports[`code transformation worker: reports filename when encountering unsupported dynamic dependency 1`] = `"local/file.js:Invalid call at line 3: require(a)"`;

exports[`code transformation worker: transforms a module with dependencies 1`] = `
Array [
Expand Down
Loading

0 comments on commit 75d9991

Please sign in to comment.