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'), + ); + }); });