From da59258372c299511ef80597afe4b5a0d3888361 Mon Sep 17 00:00:00 2001 From: Chris Hopman Date: Wed, 4 Jan 2017 20:45:55 -0800 Subject: [PATCH] Add --sourcemap-sources-root to RN packager Reviewed By: davidaurelio Differential Revision: D4357122 fbshipit-source-id: dc19474aa54ea4684a6b1c586a9d1e5da0980327 --- local-cli/bundle/bundleCommandLineArgs.js | 3 ++ local-cli/bundle/output/bundle.js | 11 ++++-- local-cli/bundle/output/unbundle/as-assets.js | 16 +++++--- .../bundle/output/unbundle/as-indexed-file.js | 15 +++++--- local-cli/bundle/types.flow.js | 1 + .../src/lib/relativizeSourceMap.js | 38 +++++++++++++++++++ 6 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 packager/react-packager/src/lib/relativizeSourceMap.js diff --git a/local-cli/bundle/bundleCommandLineArgs.js b/local-cli/bundle/bundleCommandLineArgs.js index 5186c4fc93..40f6c42ad2 100644 --- a/local-cli/bundle/bundleCommandLineArgs.js +++ b/local-cli/bundle/bundleCommandLineArgs.js @@ -34,6 +34,9 @@ module.exports = [ }, { command: '--sourcemap-output [string]', description: 'File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map', + }, { + command: '--sourcemap-sources-root [string]', + description: 'Path to make sourcemap\'s sources entries relative to, ex. /root/dir', }, { command: '--assets-dest [string]', description: 'Directory name where to store assets referenced in the bundle', diff --git a/local-cli/bundle/output/bundle.js b/local-cli/bundle/output/bundle.js index 06217ba306..8d753de403 100644 --- a/local-cli/bundle/output/bundle.js +++ b/local-cli/bundle/output/bundle.js @@ -11,6 +11,7 @@ 'use strict'; const meta = require('./meta'); +const relativizeSourceMap = require('../../../packager/react-packager/src/lib/relativizeSourceMap'); const writeFile = require('./writeFile'); import type Bundle from '../../../packager/react-packager/src/Bundler/Bundle'; @@ -24,10 +25,11 @@ function buildBundle(packagerClient: Server, requestOptions: RequestOptions) { }); } -function createCodeWithMap(bundle: Bundle, dev: boolean): * { +function createCodeWithMap(bundle: Bundle, dev: boolean, sourceMapSourcesRoot?: string): * { + const sourceMap = relativizeSourceMap(bundle.getSourceMap({dev}), sourceMapSourcesRoot); return { code: bundle.getSource({dev}), - map: JSON.stringify(bundle.getSourceMap({dev})), + map: JSON.stringify(sourceMap), }; } @@ -40,11 +42,12 @@ function saveBundleAndMap( bundleOutput, bundleEncoding: encoding, dev, - sourcemapOutput + sourcemapOutput, + sourcemapSourcesRoot } = options; log('start'); - const codeWithMap = createCodeWithMap(bundle, !!dev); + const codeWithMap = createCodeWithMap(bundle, !!dev, sourcemapSourcesRoot); log('finish'); log('Writing bundle output to:', bundleOutput); diff --git a/local-cli/bundle/output/unbundle/as-assets.js b/local-cli/bundle/output/unbundle/as-assets.js index ae2d8c1d81..5177dbc432 100644 --- a/local-cli/bundle/output/unbundle/as-assets.js +++ b/local-cli/bundle/output/unbundle/as-assets.js @@ -15,6 +15,7 @@ const MAGIC_UNBUNDLE_NUMBER = require('./magic-number'); const buildSourceMapWithMetaData = require('./build-unbundle-sourcemap-with-metadata'); const mkdirp = require('mkdirp'); const path = require('path'); +const relativizeSourceMap = require('../../../../packager/react-packager/src/lib/relativizeSourceMap'); const writeFile = require('../writeFile'); const writeSourceMap = require('./write-sourcemap'); @@ -42,7 +43,8 @@ function saveAsAssets( const { bundleOutput, bundleEncoding: encoding, - sourcemapOutput + sourcemapOutput, + sourcemapSourcesRoot, } = options; log('start'); @@ -63,10 +65,14 @@ function saveAsAssets( writeUnbundle.then(() => log('Done writing unbundle output')); const sourceMap = - buildSourceMapWithMetaData({ - startupModules: startupModules.concat(), - lazyModules: lazyModules.concat(), - }); + relativizeSourceMap( + buildSourceMapWithMetaData({ + startupModules: startupModules.concat(), + lazyModules: lazyModules.concat(), + }), + sourcemapSourcesRoot + ); + return Promise.all([ writeUnbundle, diff --git a/local-cli/bundle/output/unbundle/as-indexed-file.js b/local-cli/bundle/output/unbundle/as-indexed-file.js index ce768f3ed7..5280158ea3 100644 --- a/local-cli/bundle/output/unbundle/as-indexed-file.js +++ b/local-cli/bundle/output/unbundle/as-indexed-file.js @@ -14,6 +14,7 @@ const MAGIC_UNBUNDLE_FILE_HEADER = require('./magic-number'); const buildSourceMapWithMetaData = require('./build-unbundle-sourcemap-with-metadata'); const fs = require('fs'); +const relativizeSourceMap = require('../../../../packager/react-packager/src/lib/relativizeSourceMap'); const writeSourceMap = require('./write-sourcemap'); const {joinModules} = require('./util'); @@ -39,6 +40,7 @@ function saveAsIndexedFile( bundleOutput, bundleEncoding: encoding, sourcemapOutput, + sourcemapSourcesRoot, } = options; log('start'); @@ -55,11 +57,14 @@ function saveAsIndexedFile( ).then(() => log('Done writing unbundle output')); const sourceMap = - buildSourceMapWithMetaData({ - startupModules: startupModules.concat(), - lazyModules: lazyModules.concat(), - moduleGroups, - }); + relativizeSourceMap( + buildSourceMapWithMetaData({ + startupModules: startupModules.concat(), + lazyModules: lazyModules.concat(), + moduleGroups, + }), + sourcemapSourcesRoot + ); return Promise.all([ writeUnbundle, diff --git a/local-cli/bundle/types.flow.js b/local-cli/bundle/types.flow.js index d0768fef85..b9b0a534ef 100644 --- a/local-cli/bundle/types.flow.js +++ b/local-cli/bundle/types.flow.js @@ -36,6 +36,7 @@ export type OutputOptions = { dev?: boolean, platform: string, sourcemapOutput?: string, + sourcemapSourcesRoot?: string, }; export type RequestOptions = {| diff --git a/packager/react-packager/src/lib/relativizeSourceMap.js b/packager/react-packager/src/lib/relativizeSourceMap.js new file mode 100644 index 0000000000..79b0e8044d --- /dev/null +++ b/packager/react-packager/src/lib/relativizeSourceMap.js @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ + +'use strict'; + +const path = require('path'); + +import type { MixedSourceMap } from './SourceMap'; + +function relativizeSourceMapInternal(sourceMap: any, sourcesRoot: string) { + if (sourceMap.sections) { + for (var i = 0; i < sourceMap.sections.length; i++) { + relativizeSourceMapInternal(sourceMap.sections[i].map, sourcesRoot); + } + } else { + for (var i = 0; i < sourceMap.sources.length; i++) { + sourceMap.sources[i] = path.relative(sourcesRoot, sourceMap.sources[i]); + } + } +} + +function relativizeSourceMap(sourceMap: MixedSourceMap, sourcesRoot?: string): MixedSourceMap { + if (!sourcesRoot) { + return sourceMap; + } + relativizeSourceMapInternal(sourceMap, sourcesRoot); + return sourceMap; +} + +module.exports = relativizeSourceMap;