From 976a181a1c082fcd3e151daf4ac1ba67b5706990 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 --- local-cli/core/index.js | 8 +++++++- local-cli/server/runServer.js | 1 + packager/react-packager.js | 3 ++- packager/src/Bundler/index.js | 8 ++++++++ packager/src/Resolver/index.js | 6 +++++- packager/src/Server/index.js | 5 ++++- 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/local-cli/core/index.js b/local-cli/core/index.js index f58eb5204..8f362146b 100644 --- a/local-cli/core/index.js +++ b/local-cli/core/index.js @@ -15,7 +15,7 @@ const Config = require('../util/Config'); const defaultConfig = require('./default.config'); const minimist = require('minimist'); -import type {GetTransformOptions, PostProcessModules} from '../../packager/src/Bundler'; +import type {GetTransformOptions, PostProcessModules, PostMinifyProcess} from '../../packager/src/Bundler'; import type {HasteImpl} from '../../packager/src/node-haste/Module'; import type {CommandT} from '../commands'; @@ -74,6 +74,12 @@ export type ConfigT = { */ postProcessModules?: PostProcessModules, + /** + * An optional function that can modify the code and source map of bundle + * after the minifaction took place. + */ + postMinifyProcess?: PostMinifyProcess, + /** * A module that exports: * - a `getHasteName(filePath)` method that returns `hasteName` for module at diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index e0163a1ce..870bf0b99 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -111,6 +111,7 @@ function getPackagerServer(args, config) { hasteImpl: config.hasteImpl, platforms: defaultPlatforms.concat(args.platforms), postProcessModules: config.postProcessModules, + postMinifyProcess: config.postMinifyProcess, projectRoots: args.projectRoots, providesModuleNodeModules: providesModuleNodeModules, reporter: new LogReporter(), diff --git a/packager/react-packager.js b/packager/react-packager.js index 36459e852..079b42ac8 100644 --- a/packager/react-packager.js +++ b/packager/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/packager/src/Bundler/index.js b/packager/src/Bundler/index.js index d9d652bd6..32b38c2ee 100644 --- a/packager/src/Bundler/index.js +++ b/packager/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/packager/src/Resolver/index.js b/packager/src/Resolver/index.js index f96ab0822..110913966 100644 --- a/packager/src/Resolver/index.js +++ b/packager/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/packager/src/Server/index.js b/packager/src/Server/index.js index d4e832ccb..b450506c0 100644 --- a/packager/src/Server/index.js +++ b/packager/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,