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', +type: 'asset',
+details: AssetFile, +details: AssetFile,
|}
| {|
+type: 'unknown',
|}; |};
export type LibraryOptions = {| export type LibraryOptions = {|

View File

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

View File

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

View File

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