packager ModuleTransport.js: @flow

Reviewed By: cpojer

Differential Revision: D4166900

fbshipit-source-id: 0b8c1730f00f9a9d4a17586967edae8cbd4f296b
This commit is contained in:
Jean Lauliac 2016-11-11 09:41:05 -08:00 committed by Facebook Github Bot
parent 2bf0c037ec
commit 6e3e0ff6d7
4 changed files with 100 additions and 23 deletions

View File

@ -18,6 +18,7 @@ const _ = require('lodash');
const base64VLQ = require('./base64-vlq'); const base64VLQ = require('./base64-vlq');
const crypto = require('crypto'); const crypto = require('crypto');
import type {SourceMap, CombinedSourceMap, MixedSourceMap} from '../lib/SourceMap';
import type {GetSourceOptions, FinalizeOptions} from './BundleBase'; import type {GetSourceOptions, FinalizeOptions} from './BundleBase';
const SOURCEMAPPING_URL = '\n\/\/# sourceMappingURL='; const SOURCEMAPPING_URL = '\n\/\/# sourceMappingURL=';
@ -53,14 +54,18 @@ class Bundle extends BundleBase {
} }
addModule( addModule(
/**
* $FlowFixMe: this code is inherently incorrect, because it modifies the
* signature of the base class function "addModule". That means callsites
* using an instance typed as the base class would be broken. This must be
* refactored.
*/
resolver: {wrapModule: (options: any) => Promise<{code: any, map: any}>}, resolver: {wrapModule: (options: any) => Promise<{code: any, map: any}>},
resolutionResponse: mixed, resolutionResponse: mixed,
module: mixed, module: mixed,
/* $FlowFixMe: erroneous change of signature. */
moduleTransport: ModuleTransport, moduleTransport: ModuleTransport,
/* $FlowFixMe: this code is inherently incorrect, because it modifies the /* $FlowFixMe: erroneous change of signature. */
* signature of the base class function "addModule", originally returning
* a number. That means callsites using an instance typed as the base class
* would be broken. This must be refactored. */
): Promise<void> { ): Promise<void> {
const index = super.addModule(moduleTransport); const index = super.addModule(moduleTransport);
return resolver.wrapModule({ return resolver.wrapModule({
@ -170,7 +175,7 @@ class Bundle extends BundleBase {
* that makes use of of the `sections` field to combine sourcemaps by adding * that makes use of of the `sections` field to combine sourcemaps by adding
* an offset. This is supported only by Chrome for now. * an offset. This is supported only by Chrome for now.
*/ */
_getCombinedSourceMaps(options) { _getCombinedSourceMaps(options): CombinedSourceMap {
const result = { const result = {
version: 3, version: 3,
file: this._getSourceMapFile(), file: this._getSourceMapFile(),
@ -186,6 +191,7 @@ class Bundle extends BundleBase {
} }
if (options.excludeSource) { if (options.excludeSource) {
/* $FlowFixMe: assume the map is not empty if we got here. */
if (map.sourcesContent && map.sourcesContent.length) { if (map.sourcesContent && map.sourcesContent.length) {
map = Object.assign({}, map, {sourcesContent: []}); map = Object.assign({}, map, {sourcesContent: []});
} }
@ -193,6 +199,7 @@ class Bundle extends BundleBase {
result.sections.push({ result.sections.push({
offset: { line: line, column: 0 }, offset: { line: line, column: 0 },
/* $FlowFixMe: assume the map is not empty if we got here. */
map: map, map: map,
}); });
line += module.code.split('\n').length; line += module.code.split('\n').length;
@ -201,7 +208,7 @@ class Bundle extends BundleBase {
return result; return result;
} }
getSourceMap(options: {excludeSource?: boolean}) { getSourceMap(options: {excludeSource?: boolean}): MixedSourceMap {
super.assertFinalized(); super.assertFinalized();
if (this._shouldCombineSourceMaps) { if (this._shouldCombineSourceMaps) {
@ -336,11 +343,12 @@ class Bundle extends BundleBase {
BundleBase.fromJSON(bundle, json); BundleBase.fromJSON(bundle, json);
/* $FlowFixMe: this modifies BundleBase#fromJSON() signature. */
return bundle; return bundle;
} }
} }
function generateSourceMapForVirtualModule(module) { function generateSourceMapForVirtualModule(module): SourceMap {
// All lines map 1-to-1 // All lines map 1-to-1
let mappings = 'AAAA;'; let mappings = 'AAAA;';
@ -379,6 +387,7 @@ function * filter(iterator, predicate) {
function * subtree(moduleTransport: ModuleTransport, moduleTransportsByPath, seen = new Set()) { function * subtree(moduleTransport: ModuleTransport, moduleTransportsByPath, seen = new Set()) {
seen.add(moduleTransport.id); seen.add(moduleTransport.id);
/* $FlowFixMe: there may not be a `meta` object */
for (const [, {path}] of moduleTransport.meta.dependencyPairs || []) { for (const [, {path}] of moduleTransport.meta.dependencyPairs || []) {
const dependency = moduleTransportsByPath.get(path); const dependency = moduleTransportsByPath.get(path);
if (dependency && !seen.has(dependency.id)) { if (dependency && !seen.has(dependency.id)) {
@ -420,6 +429,7 @@ function createGroups(ramGroups: Array<string>, lazyModules) {
return [ return [
root.id, root.id,
// `subtree` yields the IDs of all transitive dependencies of a module // `subtree` yields the IDs of all transitive dependencies of a module
/* $FlowFixMe: assumes the module is always in the Map */
new Set(subtree(byPath.get(root.sourcePath), byPath)), new Set(subtree(byPath.get(root.sourcePath), byPath)),
]; ];
}) })

View File

@ -329,7 +329,7 @@ class Bundler {
}; };
const finalizeBundle = ({bundle: finalBundle, transformedModules, response, modulesByName}: { const finalizeBundle = ({bundle: finalBundle, transformedModules, response, modulesByName}: {
bundle: Bundle, bundle: Bundle,
transformedModules: Array<{module: Module, transformed: {}}>, transformedModules: Array<{module: Module, transformed: ModuleTransport}>,
response: ResolutionResponse, response: ResolutionResponse,
modulesByName: {[name: string]: Module}, modulesByName: {[name: string]: Module},
}) => }) =>

View File

@ -5,30 +5,64 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
function ModuleTransport(data) { import type {SourceMap} from './SourceMap';
this.name = data.name;
assertExists(data, 'id'); type Metadata = {
this.id = data.id; dependencyPairs?: Array<[mixed, {path: string}]>,
preloaded?: boolean,
};
assertExists(data, 'code'); class ModuleTransport {
this.code = data.code;
assertExists(data, 'sourceCode'); name: string;
this.sourceCode = data.sourceCode; id: string | number;
code: string;
sourceCode: string;
sourcePath: string;
virtual: ?boolean;
meta: ?Metadata;
polyfill: ?boolean;
map: ?SourceMap;
assertExists(data, 'sourcePath'); constructor(data: {
this.sourcePath = data.sourcePath; name: string,
id: string | number,
code: string,
sourceCode: string,
sourcePath: string,
virtual?: ?boolean,
meta?: ?Metadata,
polyfill?: ?boolean,
map?: ?SourceMap,
}) {
this.name = data.name;
this.virtual = data.virtual; assertExists(data, 'id');
this.meta = data.meta; this.id = data.id;
this.polyfill = data.polyfill;
this.map = data.map; assertExists(data, 'code');
this.code = data.code;
assertExists(data, 'sourceCode');
this.sourceCode = data.sourceCode;
assertExists(data, 'sourcePath');
this.sourcePath = data.sourcePath;
this.virtual = data.virtual;
this.meta = data.meta;
this.polyfill = data.polyfill;
this.map = data.map;
Object.freeze(this);
}
Object.freeze(this);
} }
module.exports = ModuleTransport; module.exports = ModuleTransport;

33
react-packager/src/lib/SourceMap.js vendored Normal file
View File

@ -0,0 +1,33 @@
/**
* 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';
type SourceMapBase = {
version: number,
file: string,
};
export type SourceMap = SourceMapBase & {
sources: Array<string>,
names: Array<string>,
mappings: string,
sourcesContent: Array<string>,
};
export type CombinedSourceMap = SourceMapBase & {
sections: Array<{
offset: {line: number, column: number},
map: SourceMap,
}>,
};
export type MixedSourceMap = SourceMap | CombinedSourceMap;