packager worker/index.js: do not write to cache directly

Reviewed By: davidaurelio

Differential Revision: D4159970

fbshipit-source-id: 133ce5797e94a39fe60cbb9983edd561ce7d818e
This commit is contained in:
Jean Lauliac 2016-11-10 09:06:59 -08:00 committed by Facebook Github Bot
parent ea8c7ec385
commit ee637ee0eb
6 changed files with 52 additions and 76 deletions

View File

@ -202,7 +202,6 @@ class Bundler {
module.path, module.path,
code, code,
transformCodeOptions, transformCodeOptions,
transformCacheKey,
), ),
transformCacheKey, transformCacheKey,
}); });

View File

@ -27,7 +27,6 @@ const {any} = jasmine;
describe('Transformer', function() { describe('Transformer', function() {
let options, workers, Cache; let options, workers, Cache;
const fileName = '/an/arbitrary/file.js'; const fileName = '/an/arbitrary/file.js';
const transformCacheKey = 'abcdef';
const transformModulePath = __filename; const transformModulePath = __filename;
beforeEach(function() { beforeEach(function() {
@ -48,13 +47,12 @@ describe('Transformer', function() {
' and options to the worker farm when transforming', () => { ' and options to the worker farm when transforming', () => {
const transformOptions = {arbitrary: 'options'}; const transformOptions = {arbitrary: 'options'};
const code = 'arbitrary(code)'; const code = 'arbitrary(code)';
new Transformer(options).transformFile(fileName, code, transformOptions, transformCacheKey); new Transformer(options).transformFile(fileName, code, transformOptions);
expect(workers.transformAndExtractDependencies).toBeCalledWith( expect(workers.transformAndExtractDependencies).toBeCalledWith(
transformModulePath, transformModulePath,
fileName, fileName,
code, code,
transformOptions, transformOptions,
transformCacheKey,
any(Function), any(Function),
); );
}); });
@ -65,7 +63,7 @@ describe('Transformer', function() {
var snippet = 'snippet'; var snippet = 'snippet';
workers.transformAndExtractDependencies.mockImpl( workers.transformAndExtractDependencies.mockImpl(
function(transformPath, filename, code, opts, transfCacheKey, callback) { function(transformPath, filename, code, opts, callback) {
var babelError = new SyntaxError(message); var babelError = new SyntaxError(message);
babelError.type = 'SyntaxError'; babelError.type = 'SyntaxError';
babelError.description = message; babelError.description = message;
@ -78,7 +76,7 @@ describe('Transformer', function() {
}, },
); );
return transformer.transformFile(fileName, '', {}, transformCacheKey) return transformer.transformFile(fileName, '', {})
.catch(function(error) { .catch(function(error) {
expect(error.type).toEqual('TransformError'); expect(error.type).toEqual('TransformError');
expect(error.message).toBe('SyntaxError ' + message); expect(error.message).toBe('SyntaxError ' + message);

View File

@ -108,13 +108,13 @@ class Transformer {
this._workers && workerFarm.end(this._workers); this._workers && workerFarm.end(this._workers);
} }
transformFile(fileName, code, options, transformCacheKey) { transformFile(fileName, code, options) {
if (!this._transform) { if (!this._transform) {
return Promise.reject(new Error('No transform module')); return Promise.reject(new Error('No transform module'));
} }
debug('transforming file', fileName); debug('transforming file', fileName);
return this return this
._transform(this._transformModulePath, fileName, code, options, transformCacheKey) ._transform(this._transformModulePath, fileName, code, options)
.then(data => { .then(data => {
Logger.log(data.transformFileStartLogEntry); Logger.log(data.transformFileStartLogEntry);
Logger.log(data.transformFileEndLogEntry); Logger.log(data.transformFileEndLogEntry);

View File

@ -13,13 +13,11 @@ jest.mock('../constant-folding');
jest.mock('../extract-dependencies'); jest.mock('../extract-dependencies');
jest.mock('../inline'); jest.mock('../inline');
jest.mock('../minify'); jest.mock('../minify');
jest.mock('../../../lib/TransformCache');
const {any, objectContaining} = jasmine; const {any, objectContaining} = jasmine;
describe('code transformation worker:', () => { describe('code transformation worker:', () => {
let transformCode; let transformCode;
let TransformCache;
let extractDependencies, transform; let extractDependencies, transform;
beforeEach(() => { beforeEach(() => {
@ -28,14 +26,13 @@ describe('code transformation worker:', () => {
extractDependencies = extractDependencies =
require('../extract-dependencies').mockReturnValue({}); require('../extract-dependencies').mockReturnValue({});
transform = jest.fn(); transform = jest.fn();
TransformCache = require('../../../lib/TransformCache');
}); });
it('calls the transform with file name, source code, and transform options', function() { it('calls the transform with file name, source code, and transform options', function() {
const filename = 'arbitrary/file.js'; const filename = 'arbitrary/file.js';
const sourceCode = 'arbitrary(code)'; const sourceCode = 'arbitrary(code)';
const transformOptions = {arbitrary: 'options'}; const transformOptions = {arbitrary: 'options'};
transformCode(transform, filename, sourceCode, {transform: transformOptions}, ''); transformCode(transform, filename, sourceCode, {transform: transformOptions});
expect(transform).toBeCalledWith( expect(transform).toBeCalledWith(
{filename, sourceCode, options: transformOptions}, any(Function)); {filename, sourceCode, options: transformOptions}, any(Function));
}); });
@ -43,7 +40,7 @@ describe('code transformation worker:', () => {
it('prefixes JSON files with an assignment to module.exports to make the code valid', function() { it('prefixes JSON files with an assignment to module.exports to make the code valid', function() {
const filename = 'arbitrary/file.json'; const filename = 'arbitrary/file.json';
const sourceCode = '{"arbitrary":"property"}'; const sourceCode = '{"arbitrary":"property"}';
transformCode(transform, filename, sourceCode, {}, ''); transformCode(transform, filename, sourceCode, {});
expect(transform).toBeCalledWith( expect(transform).toBeCalledWith(
{filename, sourceCode: `module.exports=${sourceCode}`}, any(Function)); {filename, sourceCode: `module.exports=${sourceCode}`}, any(Function));
}); });
@ -56,10 +53,9 @@ describe('code transformation worker:', () => {
transform.mockImplementation((_, callback) => transform.mockImplementation((_, callback) =>
callback(null, result)); callback(null, result));
transformCode(transform, 'filename', 'code', {}, '', error => { transformCode(transform, 'filename', 'code', {}, (error, data) => {
expect(error).toBeNull(); expect(error).toBeNull();
expect(TransformCache.mock.lastWrite.result) expect(data.result).toEqual(objectContaining(result));
.toEqual(objectContaining(result));
done(); done();
}); });
}); });
@ -71,14 +67,14 @@ describe('code transformation worker:', () => {
const result = { const result = {
code: 'p.exports={a:1,b:2}', code: 'p.exports={a:1,b:2}',
}; };
transform.mockImplementation((_, callback) => transform.mockImplementation((_, callback) => callback(null, result));
callback(null, result)); const filePath = 'arbitrary/file.json';
transformCode(transform, filePath, 'b', {}, (error, data) => {
transformCode(transform, 'arbitrary/file.json', 'b', {}, '', error => { expect(error).toBeNull();
expect(error).toBeNull(); expect(data.result.code).toEqual('{a:1,b:2}');
expect(TransformCache.mock.lastWrite.result.code).toEqual('{a:1,b:2}'); done();
done(); },
}); );
} }
); );
@ -88,9 +84,10 @@ describe('code transformation worker:', () => {
code: `${shebang} \n arbitrary(code)`, code: `${shebang} \n arbitrary(code)`,
}; };
transform.mockImplementation((_, callback) => callback(null, result)); transform.mockImplementation((_, callback) => callback(null, result));
transformCode(transform, 'arbitrary/file.js', 'b', {}, '', error => { const filePath = 'arbitrary/file.js';
transformCode(transform, filePath, 'b', {}, (error, data) => {
expect(error).toBeNull(); expect(error).toBeNull();
const code = TransformCache.mock.lastWrite.result.code; const {code} = data.result;
expect(code).not.toContain(shebang); expect(code).not.toContain(shebang);
expect(code.split('\n').length).toEqual(result.code.split('\n').length); expect(code.split('\n').length).toEqual(result.code.split('\n').length);
done(); done();
@ -100,7 +97,7 @@ describe('code transformation worker:', () => {
it('calls back with any error yielded by the transform', done => { it('calls back with any error yielded by the transform', done => {
const error = Error('arbitrary error'); const error = Error('arbitrary error');
transform.mockImplementation((_, callback) => callback(error)); transform.mockImplementation((_, callback) => callback(error));
transformCode(transform, 'filename', 'code', {}, '', e => { transformCode(transform, 'filename', 'code', {}, e => {
expect(e).toBe(error); expect(e).toBe(error);
done(); done();
}); });
@ -117,7 +114,7 @@ describe('code transformation worker:', () => {
it('passes the transformed code the `extractDependencies`', done => { it('passes the transformed code the `extractDependencies`', done => {
code = 'arbitrary(code)'; code = 'arbitrary(code)';
transformCode(transform, 'filename', 'code', {}, '', (error) => { transformCode(transform, 'filename', 'code', {}, (error) => {
expect(error).toBeNull(); expect(error).toBeNull();
expect(extractDependencies).toBeCalledWith(code); expect(extractDependencies).toBeCalledWith(code);
done(); done();
@ -134,45 +131,31 @@ describe('code transformation worker:', () => {
}; };
extractDependencies.mockReturnValue(dependencyData); extractDependencies.mockReturnValue(dependencyData);
transformCode(transform, 'filename', 'code', {}, '', error => { transformCode(transform, 'filename', 'code', {}, (error, data) => {
expect(error).toBeNull(); expect(error).toBeNull();
const data = TransformCache.mock.lastWrite.result; expect(data.result).toEqual(objectContaining(dependencyData));
expect(data).toEqual(objectContaining(dependencyData));
done(); done();
}); });
} }
); );
it('does not extract requires if files are marked as "extern"', done => { it('does not extract requires if files are marked as "extern"', done => {
transformCode( const opts = {extern: true};
transform, transformCode(transform, 'filename', 'code', opts, (error, data) => {
'filename',
'code',
{extern: true},
'',
error => {
expect(error).toBeNull(); expect(error).toBeNull();
const {dependencies, dependencyOffsets} = const {dependencies, dependencyOffsets} = data.result;
TransformCache.mock.lastWrite.result;
expect(extractDependencies).not.toBeCalled(); expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]); expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]); expect(dependencyOffsets).toEqual([]);
done(); done();
} });
);
}); });
it('does not extract requires of JSON files', done => { it('does not extract requires of JSON files', done => {
transformCode( const jsonStr = '{"arbitrary":"json"}';
transform, transformCode(transform, 'arbitrary.json', jsonStr, {}, (error, data) => {
'arbitrary.json',
'{"arbitrary":"json"}',
{},
'',
error => {
expect(error).toBeNull(); expect(error).toBeNull();
const {dependencies, dependencyOffsets} = const {dependencies, dependencyOffsets} = data.result;
TransformCache.mock.lastWrite.result;
expect(extractDependencies).not.toBeCalled(); expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]); expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]); expect(dependencyOffsets).toEqual([]);
@ -210,7 +193,7 @@ describe('code transformation worker:', () => {
it('passes the transform result to `inline` for constant inlining', done => { it('passes the transform result to `inline` for constant inlining', done => {
transformResult = {map: {version: 3}, code: 'arbitrary(code)'}; transformResult = {map: {version: 3}, code: 'arbitrary(code)'};
transformCode(transform, filename, 'code', options, '', () => { transformCode(transform, filename, 'code', options, () => {
expect(inline).toBeCalledWith(filename, transformResult, options); expect(inline).toBeCalledWith(filename, transformResult, options);
done(); done();
}); });
@ -219,30 +202,30 @@ describe('code transformation worker:', () => {
it('passes the result obtained from `inline` on to `constant-folding`', done => { it('passes the result obtained from `inline` on to `constant-folding`', done => {
const inlineResult = {map: {version: 3, sources: []}, ast: {}}; const inlineResult = {map: {version: 3, sources: []}, ast: {}};
inline.mockReturnValue(inlineResult); inline.mockReturnValue(inlineResult);
transformCode(transform, filename, 'code', options, '', () => { transformCode(transform, filename, 'code', options, () => {
expect(constantFolding).toBeCalledWith(filename, inlineResult); expect(constantFolding).toBeCalledWith(filename, inlineResult);
done(); done();
}); });
}); });
it('Uses the code obtained from `constant-folding` to extract dependencies', done => { it('Uses the code obtained from `constant-folding` to extract dependencies', done => {
transformCode(transform, filename, 'code', options, '', () => { transformCode(transform, filename, 'code', options, () => {
expect(extractDependencies).toBeCalledWith(foldedCode); expect(extractDependencies).toBeCalledWith(foldedCode);
done(); done();
}); });
}); });
it('uses the dependencies obtained from the optimized result', done => { it('uses the dependencies obtained from the optimized result', done => {
transformCode(transform, filename, 'code', options, '', () => { transformCode(transform, filename, 'code', options, (_, data) => {
const result = TransformCache.mock.lastWrite.result; const result = data.result;
expect(result.dependencies).toEqual(dependencyData.dependencies); expect(result.dependencies).toEqual(dependencyData.dependencies);
done(); done();
}); });
}); });
it('uses data produced by `constant-folding` for the result', done => { it('uses data produced by `constant-folding` for the result', done => {
transformCode(transform, 'filename', 'code', options, '', () => { transformCode(transform, 'filename', 'code', options, (_, data) => {
expect(TransformCache.mock.lastWrite.result) expect(data.result)
.toEqual(objectContaining({code: foldedCode, map: foldedMap})); .toEqual(objectContaining({code: foldedCode, map: foldedMap}));
done(); done();
}); });

View File

@ -8,12 +8,6 @@
*/ */
'use strict'; 'use strict';
require('../../../../babelRegisterOnly')([
/packager\/react-packager\/src\/lib\/TransformCache/,
]);
const TransformCache = require('../../lib/TransformCache');
const constantFolding = require('./constant-folding'); const constantFolding = require('./constant-folding');
const extractDependencies = require('./extract-dependencies'); const extractDependencies = require('./extract-dependencies');
const inline = require('./inline'); const inline = require('./inline');
@ -26,7 +20,7 @@ function makeTransformParams(filename, sourceCode, options) {
return {filename, sourceCode, options}; return {filename, sourceCode, options};
} }
function transformCode(transform, filename, sourceCode, options, transformCacheKey, callback) { function transformCode(transform, filename, sourceCode, options, callback) {
const params = makeTransformParams(filename, sourceCode, options.transform); const params = makeTransformParams(filename, sourceCode, options.transform);
const isJson = filename.endsWith('.json'); const isJson = filename.endsWith('.json');
@ -79,13 +73,6 @@ function transformCode(transform, filename, sourceCode, options, transformCacheK
result.code = code; result.code = code;
result.map = map; result.map = map;
TransformCache.writeSync({
filePath: filename,
sourceCode,
transformCacheKey,
transformOptions: options,
result,
});
return callback(null, { return callback(null, {
result, result,
transformFileStartLogEntry, transformFileStartLogEntry,
@ -99,10 +86,9 @@ exports.transformAndExtractDependencies = (
filename, filename,
sourceCode, sourceCode,
options, options,
transformCacheKey,
callback callback
) => { ) => {
transformCode(require(transform), filename, sourceCode, options || {}, transformCacheKey, callback); transformCode(require(transform), filename, sourceCode, options || {}, callback);
}; };
exports.minify = (filename, code, sourceMap, callback) => { exports.minify = (filename, code, sourceMap, callback) => {

View File

@ -225,13 +225,23 @@ class Module {
transformOptions: mixed, transformOptions: mixed,
callback: (error: ?Error, result: ?TransformedCode) => void, callback: (error: ?Error, result: ?TransformedCode) => void,
) { ) {
const {_transformCode} = this; const {_transformCode, _transformCacheKey} = this;
// AssetModule_DEPRECATED doesn't provide transformCode, but these should // AssetModule_DEPRECATED doesn't provide transformCode, but these should
// never be transformed anyway. // never be transformed anyway.
invariant(_transformCode != null, 'missing code transform funtion'); invariant(_transformCode != null, 'missing code transform funtion');
invariant(_transformCacheKey != null, 'missing cache key');
this._readSourceCode().then(sourceCode => { this._readSourceCode().then(sourceCode => {
return _transformCode(this, sourceCode, transformOptions) return _transformCode(this, sourceCode, transformOptions)
.then(freshResult => callback(undefined, freshResult)); .then(freshResult => {
TransformCache.writeSync({
filePath: this.path,
sourceCode,
transformCacheKey: _transformCacheKey,
transformOptions,
result: freshResult,
});
callback(undefined, freshResult);
});
}, callback); }, callback);
} }