BREAKING: Pass parameters to `transformer.transform` as object

Reviewed By: jeanlauliac

Differential Revision: D5037155

fbshipit-source-id: fbd18bd62254d7e1522f659ea62055e66748a951
This commit is contained in:
David Aurelio 2017-05-11 08:32:04 -07:00 committed by Facebook Github Bot
parent 70b7488f97
commit 58337c9032
7 changed files with 50 additions and 33 deletions

View File

@ -34,11 +34,15 @@ module.exports = {
).code; ).code;
} }
return transformer.transform(src, file, { return transformer.transform({
dev: true, filename: file,
inlineRequires: true, options: {
platform: '', dev: true,
projectRoot: '', inlineRequires: true,
platform: '',
projectRoot: '',
},
src,
}).code; }).code;
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": "0.5.1", "version": "0.6.0",
"name": "react-native-packager", "name": "react-native-packager",
"description": "Build native apps with React!", "description": "Build native apps with React!",
"repository": { "repository": {

View File

@ -26,7 +26,7 @@ describe('code transformation worker:', () => {
extractDependencies = extractDependencies =
require('../extract-dependencies').mockReturnValue({}); require('../extract-dependencies').mockReturnValue({});
transformer = { transformer = {
transform: jest.fn((src, filename, options) => ({ transform: jest.fn(({filename, options, src}) => ({
code: src, code: src,
map: {}, map: {},
})), })),
@ -38,22 +38,22 @@ describe('code transformation worker:', () => {
const sourceCode = 'arbitrary(code)'; const sourceCode = 'arbitrary(code)';
const transformOptions = {arbitrary: 'options'}; const transformOptions = {arbitrary: 'options'};
transformCode(transformer, filename, sourceCode, {transform: transformOptions}, () => {}); transformCode(transformer, filename, sourceCode, {transform: transformOptions}, () => {});
expect(transformer.transform).toBeCalledWith( expect(transformer.transform).toBeCalledWith({
sourceCode,
filename, filename,
transformOptions, options: transformOptions,
); src: sourceCode,
});
}); });
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(transformer, filename, sourceCode, {}, () => {}); transformCode(transformer, filename, sourceCode, {}, () => {});
expect(transformer.transform).toBeCalledWith( expect(transformer.transform).toBeCalledWith({
`module.exports=${sourceCode}`,
filename, filename,
undefined, options: undefined,
); src: `module.exports=${sourceCode}`,
});
}); });
it('calls back with the result of the transform in the cache', done => { it('calls back with the result of the transform in the cache', done => {

View File

@ -29,13 +29,13 @@ export type TransformedCode = {
}; };
export type Transformer<ExtraOptions: {} = {}> = { export type Transformer<ExtraOptions: {} = {}> = {
transform: ( transform: ({|
filename: string, filename: string,
sourceCode: string,
options: ExtraOptions & TransformOptions, options: ExtraOptions & TransformOptions,
plugins?: BabelPlugins, plugins?: BabelPlugins,
) => {ast: ?Ast, code: string, map: ?MappingsMap}, src: string,
getCacheKey: TransformOptionsStrict => string, |}) => {ast: ?Ast, code: string, map: ?MappingsMap},
getCacheKey: () => string,
}; };
@ -102,7 +102,11 @@ function transformCode(
let transformed; let transformed;
try { try {
transformed = transformer.transform(sourceCode, filename, options.transform); transformed = transformer.transform({
filename,
options: options.transform,
src: sourceCode,
});
} catch (error) { } catch (error) {
callback(error); callback(error);
return; return;

View File

@ -95,10 +95,16 @@ describe('transforming JS modules:', () => {
const variants = {dev: {dev: true}, prod: {dev: false}}; const variants = {dev: {dev: true}, prod: {dev: false}};
transformModule(sourceCode, options(variants), () => { transformModule(sourceCode, options(variants), () => {
expect(transformer.transform) expect(transformer.transform).toBeCalledWith({
.toBeCalledWith(sourceCode, filename, {...defaults, ...variants.dev}); filename,
expect(transformer.transform) options: {...defaults, ...variants.dev},
.toBeCalledWith(sourceCode, filename, {...defaults, ...variants.prod}); src: sourceCode,
});
expect(transformer.transform).toBeCalledWith({
filename,
options: {...defaults, ...variants.prod},
src: sourceCode,
});
done(); done();
}); });
}, },

View File

@ -70,11 +70,11 @@ function transformModule(
const {filename, transformer, variants = defaultVariants} = options; const {filename, transformer, variants = defaultVariants} = options;
const tasks = {}; const tasks = {};
Object.keys(variants).forEach(name => { Object.keys(variants).forEach(name => {
tasks[name] = asyncify(() => transformer.transform( tasks[name] = asyncify(() => transformer.transform({
code,
filename, filename,
{...defaultTransformOptions, ...variants[name]}, options: {...defaultTransformOptions, ...variants[name]},
) src: code,
})
); );
}); });

View File

@ -112,11 +112,14 @@ function buildBabelConfig(filename, options) {
return Object.assign({}, babelRC, config); return Object.assign({}, babelRC, config);
} }
function transform( type Params = {
src: string,
filename: string, filename: string,
options, options: TransformOptions,
) { plugins?: BabelPlugins,
src: string,
};
function transform({filename, options, src}: Params) {
options = options || {}; options = options || {};
const OLD_BABEL_ENV = process.env.BABEL_ENV; const OLD_BABEL_ENV = process.env.BABEL_ENV;
@ -154,7 +157,7 @@ function transform(
} }
} }
function getCacheKey(options: TransformOptions) { function getCacheKey() {
var key = crypto.createHash('md5'); var key = crypto.createHash('md5');
cacheKeyParts.forEach(part => key.update(part)); cacheKeyParts.forEach(part => key.update(part));
return key.digest('hex'); return key.digest('hex');