diff --git a/packages/metro-bundler/src/Bundler/Bundle.js b/packages/metro-bundler/src/Bundler/Bundle.js index 06400343..002c816e 100644 --- a/packages/metro-bundler/src/Bundler/Bundle.js +++ b/packages/metro-bundler/src/Bundler/Bundle.js @@ -88,7 +88,11 @@ class Bundle extends BundleBase { */ resolver: { wrapModule: (options: any) => {code: any, map: any}, - minifyModule: ({code: any, map: any, path: any}) => Promise<{ + minifyModule: ( + code: any, + map: any, + path: any, + ) => Promise<{ code: any, map: any, }>, diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index cb61ee84..09209cd7 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -224,7 +224,7 @@ class Bundler { globalTransformCache: opts.globalTransformCache, hasteImpl: opts.hasteImpl, maxWorkers: opts.maxWorkers, - minifyCode: this._transformer.minify, + minifyCode: this._transformer.minify.bind(this._transformer), postMinifyProcess: this._opts.postMinifyProcess, platforms: new Set(opts.platforms), polyfillModuleNames: opts.polyfillModuleNames, diff --git a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js index 54822342..e5d8ce36 100644 --- a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js +++ b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js @@ -15,7 +15,6 @@ const DeltaCalculator = require('./DeltaCalculator'); const createModuleIdFactory = require('../lib/createModuleIdFactory'); -const minify = require('../JSTransformer/worker/minify'); const {EventEmitter} = require('events'); @@ -448,7 +447,11 @@ class DeltaTransformer extends EventEmitter { }; const {code, map} = transformOptions.minify - ? minify.withRawMappings(wrapped.code, wrapped.map, module.path) + ? await this._resolver.minifyModule( + module.path, + wrapped.code, + wrapped.map, + ) : wrapped; const id = this._getModuleId(module); diff --git a/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js b/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js index 539db0d7..e3624716 100644 --- a/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js +++ b/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js @@ -406,7 +406,7 @@ describe('Resolver', function() { dependencies: [module], mainModuleId: id, }); - sourceMap = {version: 3, sources: ['input'], mappings: 'whatever'}; + sourceMap = []; return Resolver.load({ projectRoot: '/root', minifyCode, @@ -419,19 +419,20 @@ describe('Resolver', function() { it('should use minified code', () => { expect.assertions(2); const minifiedCode = 'minified(code)'; - const minifiedMap = {version: 3, file: ['minified']}; + const minifiedMap = { + version: 3, + file: ['minified'], + sources: [], + mappings: '', + }; minifyCode.mockReturnValue( Promise.resolve({code: minifiedCode, map: minifiedMap}), ); return depResolver - .minifyModule({ - path: module.path, - name: id, - code, - }) + .minifyModule(module.path, code, sourceMap) .then(({code, map}) => { expect(code).toEqual(minifiedCode); - expect(map).toEqual(minifiedMap); + expect(map).toEqual([]); }); }); }); diff --git a/packages/metro-bundler/src/Resolver/index.js b/packages/metro-bundler/src/Resolver/index.js index 038cf149..5b1edd69 100644 --- a/packages/metro-bundler/src/Resolver/index.js +++ b/packages/metro-bundler/src/Resolver/index.js @@ -15,6 +15,12 @@ const DependencyGraph = require('../node-haste/DependencyGraph'); const defaults = require('../defaults'); + +const { + compactMapping, + fromRawMappings, + toRawMappings, +} = require('../Bundler/source-map'); const pathJoin = require('path').join; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; @@ -256,17 +262,23 @@ class Resolver { return {code, map}; } - async minifyModule({ - path, - code, - map, - }: { + async minifyModule( path: string, code: string, - map: ?MappingsMap, - }): Promise<{code: string, map: ?MappingsMap}> { - const minified = await this._minifyCode(path, code, map); - return await this._postMinifyProcess(minified); + map: CompactRawMappings, + ): Promise<{code: string, map: CompactRawMappings}> { + const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap( + undefined, + {}, + ); + + const minified = await this._minifyCode(path, code, sourceMap); + const result = await this._postMinifyProcess(minified); + + return { + code: result.code, + map: result.map ? toRawMappings(result.map).map(compactMapping) : [], + }; } getDependencyGraph(): DependencyGraph {