Make the sourcemaps generation module independent of ModuleTransport

Reviewed By: jeanlauliac

Differential Revision: D5789998

fbshipit-source-id: a460ccb0baa62d0edb4e0da2b6f4d4abaa7fe222
This commit is contained in:
Rafael Oleza 2017-09-11 08:22:24 -07:00 committed by Facebook Github Bot
parent 5ee6de217d
commit 2593ea2ad4
4 changed files with 32 additions and 12 deletions

View File

@ -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<string>) {
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 {

View File

@ -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',
},
];

View File

@ -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<ModuleTransport>): Generator {
function fromRawMappings(
modules: Array<{
+map: ?Array<RawMapping>,
+path: string,
+source: string,
+code: string,
}>,
): Generator {
const generator = new Generator();
let carryOver = 0;
@ -43,7 +50,7 @@ function fromRawMappings(modules: Array<ModuleTransport>): 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) {

View File

@ -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<string>,
+dependencyOffsets?: ?Array<number>,
+map?: ?MappingsMap,
+map?: ?(MappingsMap | Array<RawMapping>),
+source: string,
};