From a8ce77604400e784023ed91bd4de408ffae792d6 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Fri, 20 Apr 2018 09:17:37 -0700 Subject: [PATCH] Fix minification of JSON files Summary: When moving the minification to the worker, I forgot to also minify JSON files (they have an early return). This diff fixes this :) Reviewed By: davidaurelio Differential Revision: D7707317 fbshipit-source-id: 1114017b811861a6f2caebe79e1d79e6ad9a7b1e --- packages/metro/src/JSTransformer/worker.js | 55 +++++++++++++------ .../worker/__tests__/worker-test.js | 29 ++++++++++ 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/packages/metro/src/JSTransformer/worker.js b/packages/metro/src/JSTransformer/worker.js index dbedd8b4..d107fec6 100644 --- a/packages/metro/src/JSTransformer/worker.js +++ b/packages/metro/src/JSTransformer/worker.js @@ -33,10 +33,8 @@ const { import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies'; import type {LocalPath} from '../node-haste/lib/toLocalPath'; import type {Ast} from '@babel/core'; -import type {BabelSourceMap} from '@babel/core'; import type {Plugins as BabelPlugins} from 'babel-core'; import type {LogEntry} from 'metro-core/src/Logger'; -import type {ResultWithMap} from 'metro-minify-uglify'; import type {MetroSourceMapSegmentTuple} from 'metro-source-map'; export type TransformedCode = { @@ -153,15 +151,26 @@ async function transformCode( .digest('hex'); if (filename.endsWith('.json')) { - const code = JsFileWrapping.wrapJson(sourceCode); + let code = JsFileWrapping.wrapJson(sourceCode); + let map = []; const transformFileEndLogEntry = getEndLogEntry( transformFileStartLogEntry, filename, ); + if (options.minify) { + ({map, code} = await minifyCode( + filename, + code, + sourceCode, + map, + minifierPath, + )); + } + return { - result: {dependencies: [], code, map: []}, + result: {dependencies: [], code, map}, sha1, transformFileStartLogEntry, transformFileEndLogEntry, @@ -262,19 +271,13 @@ async function transformCode( let code = result.code; if (options.minify) { - const sourceMap = fromRawMappings([ - {code, source: sourceCode, map, path: filename}, - ]).toMap(undefined, {}); - - const minified = await minifyCode( + ({map, code} = await minifyCode( filename, result.code, - sourceMap, + sourceCode, + map, minifierPath, - ); - - code = minified.code; - map = minified.map ? toBabelSegments(minified.map).map(toSegmentTuple) : []; + )); } return { @@ -285,15 +288,31 @@ async function transformCode( }; } -function minifyCode( +async function minifyCode( filename: string, code: string, - sourceMap: BabelSourceMap, + source: string, + map: Array, minifierPath: string, -): ResultWithMap | Promise { +): Promise<{ + code: string, + map: Array, +}> { + const sourceMap = fromRawMappings([ + {code, source, map, path: filename}, + ]).toMap(undefined, {}); + const minify = getMinifier(minifierPath); + try { - return minify.withSourceMap(code, sourceMap, filename); + const minified = minify.withSourceMap(code, sourceMap, filename); + + return { + code: minified.code, + map: minified.map + ? toBabelSegments(minified.map).map(toSegmentTuple) + : [], + }; } catch (error) { if (error.constructor.name === 'JS_Parse_Error') { throw new Error( diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index 8f2333b5..c3e2568c 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -252,4 +252,33 @@ describe('code transformation worker:', () => { ].join('\n'), ); }); + + it('minifies a JSON file', async () => { + expect( + (await transformCode( + '/root/node_modules/bar/file.json', + `node_modules/bar/file.js`, + 'arbitrary(code);', + path.join(__dirname, '../../../transformer.js'), + false, + { + dev: true, + minify: true, + transform: {}, + enableBabelRCLookup: false, + }, + [], + '', + 'minifyModulePath', + 'asyncRequire', + 'throwAtRuntime', + )).result.code, + ).toBe( + [ + '__d(function(global, require, module, exports) {', + ' module.exports = minified(code);;', + '});', + ].join('\n'), + ); + }); });