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;
}
return transformer.transform(src, file, {
dev: true,
inlineRequires: true,
platform: '',
projectRoot: '',
return transformer.transform({
filename: file,
options: {
dev: true,
inlineRequires: true,
platform: '',
projectRoot: '',
},
src,
}).code;
},

View File

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

View File

@ -26,7 +26,7 @@ describe('code transformation worker:', () => {
extractDependencies =
require('../extract-dependencies').mockReturnValue({});
transformer = {
transform: jest.fn((src, filename, options) => ({
transform: jest.fn(({filename, options, src}) => ({
code: src,
map: {},
})),
@ -38,22 +38,22 @@ describe('code transformation worker:', () => {
const sourceCode = 'arbitrary(code)';
const transformOptions = {arbitrary: 'options'};
transformCode(transformer, filename, sourceCode, {transform: transformOptions}, () => {});
expect(transformer.transform).toBeCalledWith(
sourceCode,
expect(transformer.transform).toBeCalledWith({
filename,
transformOptions,
);
options: transformOptions,
src: sourceCode,
});
});
it('prefixes JSON files with an assignment to module.exports to make the code valid', function() {
const filename = 'arbitrary/file.json';
const sourceCode = '{"arbitrary":"property"}';
transformCode(transformer, filename, sourceCode, {}, () => {});
expect(transformer.transform).toBeCalledWith(
`module.exports=${sourceCode}`,
expect(transformer.transform).toBeCalledWith({
filename,
undefined,
);
options: undefined,
src: `module.exports=${sourceCode}`,
});
});
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: {} = {}> = {
transform: (
transform: ({|
filename: string,
sourceCode: string,
options: ExtraOptions & TransformOptions,
plugins?: BabelPlugins,
) => {ast: ?Ast, code: string, map: ?MappingsMap},
getCacheKey: TransformOptionsStrict => string,
src: string,
|}) => {ast: ?Ast, code: string, map: ?MappingsMap},
getCacheKey: () => string,
};
@ -102,7 +102,11 @@ function transformCode(
let transformed;
try {
transformed = transformer.transform(sourceCode, filename, options.transform);
transformed = transformer.transform({
filename,
options: options.transform,
src: sourceCode,
});
} catch (error) {
callback(error);
return;

View File

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

View File

@ -70,11 +70,11 @@ function transformModule(
const {filename, transformer, variants = defaultVariants} = options;
const tasks = {};
Object.keys(variants).forEach(name => {
tasks[name] = asyncify(() => transformer.transform(
code,
tasks[name] = asyncify(() => transformer.transform({
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);
}
function transform(
src: string,
type Params = {
filename: string,
options,
) {
options: TransformOptions,
plugins?: BabelPlugins,
src: string,
};
function transform({filename, options, src}: Params) {
options = options || {};
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');
cacheKeyParts.forEach(part => key.update(part));
return key.digest('hex');