metro-buck: do not transform non-source files

Reviewed By: davidaurelio

Differential Revision: D6666127

fbshipit-source-id: ba20664da81361fadb8119bfeb858c70a7500c14
This commit is contained in:
Jean Lauliac 2018-01-12 04:31:51 -08:00 committed by Facebook Github Bot
parent 1687f3766e
commit 0151519a42
4 changed files with 32 additions and 12 deletions

View File

@ -228,6 +228,9 @@ export type TransformedSourceFile =
| {|
+type: 'asset',
+details: AssetFile,
|}
| {|
+type: 'unknown',
|};
export type LibraryOptions = {|

View File

@ -26,6 +26,7 @@ const {objectContaining} = jasmine;
describe('optimizing JS modules', () => {
const filename = 'arbitrary/file.js';
const sourceExts = new Set(['js', 'json']);
const optimizationOptions = {
dev: false,
platform: 'android',
@ -42,7 +43,9 @@ describe('optimizing JS modules', () => {
let transformResult;
beforeAll(() => {
const result = transformModule(originalCode, {filename, transformer});
const trOpts = {filename, sourceExts, transformer};
const result = transformModule(originalCode, trOpts);
invariant(result.type === 'code', 'result must be code');
transformResult = new Buffer(
JSON.stringify({type: 'code', details: result.details}),
'utf8',
@ -53,7 +56,7 @@ describe('optimizing JS modules', () => {
const result = optimizeModule(transformResult, optimizationOptions);
const expected = JSON.parse(transformResult.toString('utf8')).details;
delete expected.transformed;
expect(result.type).toBe('code');
invariant(result.type === 'code', 'result must be code');
expect(result.details).toEqual(objectContaining(expected));
});

View File

@ -31,7 +31,8 @@ jest.mock('image-size', () => buffer => {
});
describe('transforming JS modules:', () => {
const filename = 'arbitrary';
const filename = 'arbitrary.js';
const sourceExts = new Set(['js', 'json']);
let transformer;
@ -47,6 +48,7 @@ describe('transforming JS modules:', () => {
const options = (variants?: TransformVariants) => ({
filename,
sourceExts,
transformer,
variants,
});
@ -57,7 +59,7 @@ describe('transforming JS modules:', () => {
it('passes through file name', () => {
const result = transformModule(sourceCode, options());
expect(result.type).toBe('code');
invariant(result.type === 'code', 'result must be code');
expect(result.details).toEqual(
expect.objectContaining({
file: filename,
@ -69,19 +71,19 @@ describe('transforming JS modules:', () => {
const hasteID = 'TheModule';
const codeWithHasteID = toBuffer(`/** @providesModule ${hasteID} */`);
const result = transformModule(codeWithHasteID, options());
expect(result.type).toBe('code');
invariant(result.type === 'code', 'result must be code');
expect(result.details).toEqual(expect.objectContaining({hasteID}));
});
it('sets `type` to `"module"` by default', () => {
const result = transformModule(sourceCode, options());
expect(result.type).toBe('code');
invariant(result.type === 'code', 'result must be code');
expect(result.details).toEqual(expect.objectContaining({type: 'module'}));
});
it('sets `type` to `"script"` if the input is a polyfill', () => {
const result = transformModule(sourceCode, {...options(), polyfill: true});
expect(result.type).toBe('code');
invariant(result.type === 'code', 'result must be code');
expect(result.details).toEqual(expect.objectContaining({type: 'script'}));
});
@ -240,6 +242,14 @@ describe('transforming JS modules:', () => {
expect(result.details.package).toEqual(pkg);
});
it('does not process non-source files', () => {
const result = transformModule(toBuffer('arbitrary'), {
...options(),
filename: 'some.yy',
});
invariant(result.type === 'unknown', 'result must be code');
});
describe('assets', () => {
it('extract image sizes, platform, scale', () => {
const image = {__size: {width: 30, height: 20}};

View File

@ -42,6 +42,7 @@ export type TransformOptions<ExtraOptions> = {|
filename: string,
hasteImpl?: HasteImpl,
polyfill?: boolean,
+sourceExts: Set<string>,
transformer: Transformer<ExtraOptions>,
variants?: TransformVariants,
|};
@ -63,15 +64,18 @@ function transformModule(
content: Buffer,
options: TransformOptions<{+retainLines?: boolean}>,
): TransformedSourceFile {
if (ASSET_EXTENSIONS.has(path.extname(options.filename).substr(1))) {
const ext = path.extname(options.filename).substr(1);
if (ASSET_EXTENSIONS.has(ext)) {
return transformAsset(content, options.filename);
}
if (ext === 'json') {
return transformJSON(content.toString('utf8'), options);
}
if (!options.sourceExts.has(ext)) {
return {type: 'unknown'};
}
const code = content.toString('utf8');
if (options.filename.endsWith('.json')) {
return transformJSON(code, options);
}
const {filename, transformer, polyfill, variants = defaultVariants} = options;
const transformed: {[key: string]: TransformResult} = {};