From d016b2e9e45c64eef7cded65cccaedd2212133a0 Mon Sep 17 00:00:00 2001 From: Lukas Piatkowski Date: Wed, 3 May 2017 03:45:04 -0700 Subject: [PATCH] Add additional post process of SourceMap after the minification of JS Reviewed By: davidaurelio Differential Revision: D4955229 fbshipit-source-id: ac4e5f917839d43d73b80d98b4813d8ccf1d41ef --- packages/metro-bundler/react-packager.js | 3 ++- packages/metro-bundler/src/Bundler/index.js | 8 ++++++++ packages/metro-bundler/src/Resolver/index.js | 6 +++++- packages/metro-bundler/src/Server/index.js | 5 ++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/metro-bundler/react-packager.js b/packages/metro-bundler/react-packager.js index 36459e85..079b42ac 100644 --- a/packages/metro-bundler/react-packager.js +++ b/packages/metro-bundler/react-packager.js @@ -17,7 +17,7 @@ const debug = require('debug'); const invariant = require('fbjs/lib/invariant'); import type Server from './src/Server'; -import type {PostProcessModules} from './src/Bundler'; +import type {PostProcessModules, PostMinifyProcess} from './src/Bundler'; import type {GlobalTransformCache} from './src/lib/GlobalTransformCache'; import type {Reporter} from './src/lib/reporting'; import type {HasteImpl} from './src/node-haste/Module'; @@ -30,6 +30,7 @@ type Options = { globalTransformCache: ?GlobalTransformCache, nonPersistent?: boolean, postProcessModules?: PostProcessModules, + postMinifyProcess?: PostMinifyProcess, projectRoots: Array, reporter?: Reporter, watch?: boolean, diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index d9d652bd..32b38c2e 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -40,6 +40,7 @@ const VERSION = require('../../package.json').version; import type AssetServer from '../AssetServer'; import type Module, {HasteImpl} from '../node-haste/Module'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; +import type {MappingsMap} from '../lib/SourceMap'; import type {Options as JSTransformerOptions} from '../JSTransformer/worker/worker'; import type {Reporter} from '../lib/reporting'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; @@ -112,6 +113,11 @@ export type PostProcessModules = ( options: PostProcessModulesOptions, ) => Array; +export type PostMinifyProcess = ({ + code: string, + map: MappingsMap, +}) => {code: string, map: MappingsMap}; + type Options = {| +allowBundleUpdates: boolean, +assetExts: Array, @@ -125,6 +131,7 @@ type Options = {| +platforms: Array, +polyfillModuleNames: Array, +postProcessModules?: PostProcessModules, + +postMinifyProcess?: PostMinifyProcess, +projectRoots: Array, +providesModuleNodeModules?: Array, +reporter: Reporter, @@ -206,6 +213,7 @@ class Bundler { hasteImpl: opts.hasteImpl, maxWorkerCount, minifyCode: this._transformer.minify, + postMinifyProcess: this._opts.postMinifyProcess, platforms: new Set(opts.platforms), polyfillModuleNames: opts.polyfillModuleNames, projectRoots: opts.projectRoots, diff --git a/packages/metro-bundler/src/Resolver/index.js b/packages/metro-bundler/src/Resolver/index.js index f96ab082..11091396 100644 --- a/packages/metro-bundler/src/Resolver/index.js +++ b/packages/metro-bundler/src/Resolver/index.js @@ -19,6 +19,7 @@ const pathJoin = require('path').join; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type Module, {HasteImpl, TransformCode} from '../node-haste/Module'; import type {MappingsMap} from '../lib/SourceMap'; +import type {PostMinifyProcess} from '../Bundler'; import type {Options as JSTransformerOptions} from '../JSTransformer/worker/worker'; import type {Reporter} from '../lib/reporting'; import type {GetTransformCacheKey} from '../lib/TransformCache'; @@ -38,6 +39,7 @@ type Options = {| +hasteImpl?: HasteImpl, +maxWorkerCount: number, +minifyCode: MinifyCode, + +postMinifyProcess?: PostMinifyProcess, +platforms: Set, +polyfillModuleNames?: Array, +projectRoots: Array, @@ -52,10 +54,12 @@ class Resolver { _depGraph: DependencyGraph; _minifyCode: MinifyCode; + _postMinifyProcess: ?PostMinifyProcess; _polyfillModuleNames: Array; constructor(opts: Options, depGraph: DependencyGraph) { this._minifyCode = opts.minifyCode; + this._postMinifyProcess = opts.postMinifyProcess; this._polyfillModuleNames = opts.polyfillModuleNames || []; this._depGraph = depGraph; } @@ -222,7 +226,7 @@ class Resolver { } return minify - ? this._minifyCode(module.path, code, map) + ? this._minifyCode(module.path, code, map).then(this._postMinifyProcess) : Promise.resolve({code, map}); } diff --git a/packages/metro-bundler/src/Server/index.js b/packages/metro-bundler/src/Server/index.js index d4e832cc..b450506c 100644 --- a/packages/metro-bundler/src/Server/index.js +++ b/packages/metro-bundler/src/Server/index.js @@ -32,7 +32,7 @@ import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionRes import type Bundle from '../Bundler/Bundle'; import type HMRBundle from '../Bundler/HMRBundle'; import type {Reporter} from '../lib/reporting'; -import type {GetTransformOptions, PostProcessModules} from '../Bundler'; +import type {GetTransformOptions, PostProcessModules, PostMinifyProcess} from '../Bundler'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {SourceMap, Symbolicate} from './symbolicate'; @@ -68,6 +68,7 @@ type Options = { platforms?: Array, polyfillModuleNames?: Array, postProcessModules?: PostProcessModules, + postMinifyProcess?: PostMinifyProcess, projectRoots: Array, providesModuleNodeModules?: Array, reporter: Reporter, @@ -122,6 +123,7 @@ class Server { platforms: Array, polyfillModuleNames: Array, postProcessModules?: PostProcessModules, + postMinifyProcess?: PostMinifyProcess, projectRoots: Array, providesModuleNodeModules?: Array, reporter: Reporter, @@ -158,6 +160,7 @@ class Server { platforms: options.platforms || defaults.platforms, polyfillModuleNames: options.polyfillModuleNames || [], postProcessModules: options.postProcessModules, + postMinifyProcess: options.postMinifyProcess, projectRoots: options.projectRoots, providesModuleNodeModules: options.providesModuleNodeModules, reporter: options.reporter,