From 2593ea2ad48f7d376e5a0c3ed616dd9c1443f600 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Mon, 11 Sep 2017 08:22:24 -0700 Subject: [PATCH] Make the sourcemaps generation module independent of ModuleTransport Reviewed By: jeanlauliac Differential Revision: D5789998 fbshipit-source-id: a460ccb0baa62d0edb4e0da2b6f4d4abaa7fe222 --- packages/metro-bundler/src/Bundler/Bundle.js | 15 +++++++++++++-- .../source-map/__tests__/source-map-test.js | 13 +++++++------ .../src/Bundler/source-map/source-map.js | 13 ++++++++++--- packages/metro-bundler/src/node-haste/Module.js | 3 ++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/metro-bundler/src/Bundler/Bundle.js b/packages/metro-bundler/src/Bundler/Bundle.js index c360c160..4acd4a86 100644 --- a/packages/metro-bundler/src/Bundler/Bundle.js +++ b/packages/metro-bundler/src/Bundler/Bundle.js @@ -272,7 +272,7 @@ class Bundle extends BundleBase { return this._sourceMapFormat === 'indexed' ? this._getCombinedSourceMaps(options) - : fromRawMappings(this.getModules()).toMap(undefined, options); + : this._fromRawMappings().toMap(undefined, options); } getSourceMapString(options: {excludeSource?: boolean}): string { @@ -287,7 +287,7 @@ class Bundle extends BundleBase { let map = this._sourceMap; if (map == null) { debug('Start building flat source map'); - map = this._sourceMap = fromRawMappings(this.getModules()).toString( + map = this._sourceMap = this._fromRawMappings().toString( undefined, options, ); @@ -353,6 +353,17 @@ class Bundle extends BundleBase { setRamGroups(ramGroups: ?Array) { this._ramGroups = ramGroups; } + + _fromRawMappings() { + return fromRawMappings( + this.getModules().map(module => ({ + map: Array.isArray(module.map) ? module.map : undefined, + path: module.sourcePath, + source: module.sourceCode, + code: module.code, + })), + ); + } } function generateSourceMapForVirtualModule(module): MappingsMap { diff --git a/packages/metro-bundler/src/Bundler/source-map/__tests__/source-map-test.js b/packages/metro-bundler/src/Bundler/source-map/__tests__/source-map-test.js index 7c1c037a..648c769c 100644 --- a/packages/metro-bundler/src/Bundler/source-map/__tests__/source-map-test.js +++ b/packages/metro-bundler/src/Bundler/source-map/__tests__/source-map-test.js @@ -6,6 +6,7 @@ * 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. * + * @emails oncal+javascript_foundation * @format */ @@ -57,14 +58,14 @@ describe('build map from raw mappings', () => { [7, 8, 9, 10], [11, 12, 13, 14, 'pears'], ], - sourceCode: 'code1', - sourcePath: 'path1', + source: 'code1', + path: 'path1', }, { code: lines(3), map: [[1, 2], [3, 4, 15, 16, 'bananas']], - sourceCode: 'code2', - sourcePath: 'path2', + source: 'code2', + path: 'path2', }, { code: lines(23), @@ -74,8 +75,8 @@ describe('build map from raw mappings', () => { [17, 18, 19, 110], [21, 112, 113, 114, 'pears'], ], - sourceCode: 'code3', - sourcePath: 'path3', + source: 'code3', + path: 'path3', }, ]; diff --git a/packages/metro-bundler/src/Bundler/source-map/source-map.js b/packages/metro-bundler/src/Bundler/source-map/source-map.js index d2a01df2..ec131f00 100644 --- a/packages/metro-bundler/src/Bundler/source-map/source-map.js +++ b/packages/metro-bundler/src/Bundler/source-map/source-map.js @@ -31,7 +31,14 @@ export type RawMapping = * tuples with either 2, 4, or 5 elements: * generated line, generated column, source line, source line, symbol name. */ -function fromRawMappings(modules: Array): Generator { +function fromRawMappings( + modules: Array<{ + +map: ?Array, + +path: string, + +source: string, + +code: string, + }>, +): Generator { const generator = new Generator(); let carryOver = 0; @@ -43,7 +50,7 @@ function fromRawMappings(modules: Array): Generator { addMappingsForFile(generator, map, module, carryOver); } else if (map != null) { throw new Error( - `Unexpected module with full source map found: ${module.sourcePath}`, + `Unexpected module with full source map found: ${module.path}`, ); } @@ -69,7 +76,7 @@ function compactMapping(mapping: BabelRawMapping): RawMapping { } function addMappingsForFile(generator, mappings, module, carryOver) { - generator.startFile(module.sourcePath, module.sourceCode); + generator.startFile(module.path, module.source); const columnOffset = module.code.indexOf('{') + 1; for (let i = 0, n = mappings.length; i < n; ++i) { diff --git a/packages/metro-bundler/src/node-haste/Module.js b/packages/metro-bundler/src/node-haste/Module.js index b5f7bab3..b5692c3c 100644 --- a/packages/metro-bundler/src/node-haste/Module.js +++ b/packages/metro-bundler/src/node-haste/Module.js @@ -21,6 +21,7 @@ const jsonStableStringify = require('json-stable-stringify'); const {join: joinPath, relative: relativePath, extname} = require('path'); +import type {RawMapping} from '../Bundler/source-map'; import type { TransformedCode, Options as WorkerOptions, @@ -41,7 +42,7 @@ export type ReadResult = { +code: string, +dependencies: Array, +dependencyOffsets?: ?Array, - +map?: ?MappingsMap, + +map?: ?(MappingsMap | Array), +source: string, };