Reverted commit D3841557

Reviewed By: davidaurelio

Differential Revision: D3841557

fbshipit-source-id: c6098f0d85aa5c56b4109cd4f1ffe622379ab457
This commit is contained in:
Jing Chen 2016-09-12 13:50:20 -07:00 committed by Facebook Github Bot 5
parent 5f7b2e4085
commit 2c0d44a902
5 changed files with 124 additions and 8 deletions

View File

@ -21,6 +21,7 @@ class Bundle extends BundleBase {
super();
this._sourceMap = false;
this._sourceMapUrl = sourceMapUrl;
this._shouldCombineSourceMaps = false;
this._numRequireCalls = 0;
this._dev = dev;
this._minify = minify;
@ -40,6 +41,12 @@ class Bundle extends BundleBase {
minify: this._minify,
dev: this._dev,
}).then(({code, map}) => {
// If we get a map from the transformer we'll switch to a mode
// were we're combining the source maps as opposed to
if (!this._shouldCombineSourceMaps && map != null) {
this._shouldCombineSourceMaps = true;
}
this.replaceModuleAt(
index, new ModuleTransport({...moduleTransport, code, map}));
});
@ -130,8 +137,8 @@ class Bundle extends BundleBase {
this.getModules().forEach(module => {
let map = module.map;
if (module.virtual || !map) {
map = generateMissingSourceMapForModule(module);
if (module.virtual) {
map = generateSourceMapForVirtualModule(module);
}
if (options.excludeSource) {
@ -152,7 +159,23 @@ class Bundle extends BundleBase {
getSourceMap(options) {
super.assertFinalized();
return this._getCombinedSourceMaps(options);
if (this._shouldCombineSourceMaps) {
return this._getCombinedSourceMaps(options);
}
const mappings = this._getMappings();
const modules = this.getModules();
const map = {
file: this._getSourceMapFile(),
sources: modules.map(module => module.sourcePath),
version: 3,
names: [],
mappings: mappings,
sourcesContent: options.excludeSource
? [] : modules.map(module => module.sourceCode)
};
return map;
}
getEtag() {
@ -166,6 +189,53 @@ class Bundle extends BundleBase {
: 'bundle.js';
}
_getMappings() {
const modules = super.getModules();
// The first line mapping in our package is basically the base64vlq code for
// zeros (A).
const firstLine = 'AAAA';
// Most other lines in our mappings are all zeros (for module, column etc)
// except for the lineno mappinp: curLineno - prevLineno = 1; Which is C.
const line = 'AACA';
const moduleLines = Object.create(null);
let mappings = '';
for (let i = 0; i < modules.length; i++) {
const module = modules[i];
const code = module.code;
let lastCharNewLine = false;
moduleLines[module.sourcePath] = 0;
for (let t = 0; t < code.length; t++) {
if (t === 0 && i === 0) {
mappings += firstLine;
} else if (t === 0) {
mappings += 'AC';
// This is the only place were we actually don't know the mapping ahead
// of time. When it's a new module (and not the first) the lineno
// mapping is 0 (current) - number of lines in prev module.
mappings += base64VLQ.encode(
0 - moduleLines[modules[i - 1].sourcePath]
);
mappings += 'A';
} else if (lastCharNewLine) {
moduleLines[module.sourcePath]++;
mappings += line;
}
lastCharNewLine = code[t] === '\n';
if (lastCharNewLine) {
mappings += ';';
}
}
if (i !== modules.length - 1) {
mappings += ';';
}
}
return mappings;
}
getJSModulePaths() {
return this.getModules()
// Filter out non-js files. Like images etc.
@ -202,6 +272,7 @@ class Bundle extends BundleBase {
...super.toJSON(),
sourceMapUrl: this._sourceMapUrl,
numRequireCalls: this._numRequireCalls,
shouldCombineSourceMaps: this._shouldCombineSourceMaps,
};
}
@ -210,6 +281,7 @@ class Bundle extends BundleBase {
bundle._sourceMapUrl = json.sourceMapUrl;
bundle._numRequireCalls = json.numRequireCalls;
bundle._shouldCombineSourceMaps = json.shouldCombineSourceMaps;
BundleBase.fromJSON(bundle, json);
@ -217,7 +289,7 @@ class Bundle extends BundleBase {
}
}
function generateMissingSourceMapForModule(module) {
function generateSourceMapForVirtualModule(module) {
// All lines map 1-to-1
let mappings = 'AAAA;';

View File

@ -138,6 +138,50 @@ describe('Bundle', () => {
});
describe('sourcemap bundle', () => {
pit('should create sourcemap', () => {
const otherBundle = new Bundle({sourceMapUrl: 'test_url'});
return Promise.resolve().then(() => {
return addModule({
bundle: otherBundle,
code: [
'transformed foo',
'transformed foo',
'transformed foo',
].join('\n'),
sourceCode: [
'source foo',
'source foo',
'source foo',
].join('\n'),
sourcePath: 'foo path',
});
}).then(() => {
return addModule({
bundle: otherBundle,
code: [
'transformed bar',
'transformed bar',
'transformed bar',
].join('\n'),
sourceCode: [
'source bar',
'source bar',
'source bar',
].join('\n'),
sourcePath: 'bar path',
});
}).then(() => {
otherBundle.setMainModuleId('foo');
otherBundle.finalize({
runBeforeMainModule: [],
runMainModule: true,
});
const sourceMap = otherBundle.getSourceMap({dev: true});
expect(sourceMap).toEqual(genSourceMap(otherBundle.getModules()));
});
});
pit('should combine sourcemaps', () => {
const otherBundle = new Bundle({sourceMapUrl: 'test_url'});

View File

@ -220,7 +220,7 @@ describe('Bundler', function() {
transform: {
dev: true,
hot: false,
generateSourceMaps: true,
generateSourceMaps: false,
projectRoots,
}
},

View File

@ -385,6 +385,7 @@ class Bundler {
onProgress,
minify,
isolateModuleIDs,
generateSourceMaps: unbundle,
});
}
@ -443,7 +444,7 @@ class Bundler {
dev = true,
minify = !dev,
hot = false,
generateSourceMaps = true,
generateSourceMaps = false,
}) {
return this.getTransformOptions(
entryFile,
@ -481,7 +482,7 @@ class Bundler {
minify = !dev,
hot = false,
recursive = true,
generateSourceMaps = true,
generateSourceMaps = false,
isolateModuleIDs = false,
onProgress,
}) {

View File

@ -74,7 +74,6 @@ function buildBabelConfig(filename, options) {
const extraConfig = {
filename,
sourceFileName: filename,
sourceMaps: options.generateSourceMaps,
};
let config = Object.assign({}, babelRC, extraConfig);