From 0151519a42007c32ec0d7a91e9797ca9e2bc67bd Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 12 Jan 2018 04:31:51 -0800 Subject: [PATCH] metro-buck: do not transform non-source files Reviewed By: davidaurelio Differential Revision: D6666127 fbshipit-source-id: ba20664da81361fadb8119bfeb858c70a7500c14 --- packages/metro/src/ModuleGraph/types.flow.js | 3 +++ .../worker/__tests__/optimize-module-test.js | 7 +++++-- .../worker/__tests__/transform-module-test.js | 20 ++++++++++++++----- .../ModuleGraph/worker/transform-module.js | 14 ++++++++----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/metro/src/ModuleGraph/types.flow.js b/packages/metro/src/ModuleGraph/types.flow.js index 2df3bc39..02967e84 100644 --- a/packages/metro/src/ModuleGraph/types.flow.js +++ b/packages/metro/src/ModuleGraph/types.flow.js @@ -228,6 +228,9 @@ export type TransformedSourceFile = | {| +type: 'asset', +details: AssetFile, + |} + | {| + +type: 'unknown', |}; export type LibraryOptions = {| diff --git a/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js b/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js index 650c529c..e56060e7 100644 --- a/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js +++ b/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js @@ -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)); }); diff --git a/packages/metro/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro/src/ModuleGraph/worker/__tests__/transform-module-test.js index 64b38488..73b42dee 100644 --- a/packages/metro/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -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}}; diff --git a/packages/metro/src/ModuleGraph/worker/transform-module.js b/packages/metro/src/ModuleGraph/worker/transform-module.js index 3738cdd0..d0d17137 100644 --- a/packages/metro/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro/src/ModuleGraph/worker/transform-module.js @@ -42,6 +42,7 @@ export type TransformOptions = {| filename: string, hasteImpl?: HasteImpl, polyfill?: boolean, + +sourceExts: Set, transformer: Transformer, 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} = {};