use babel sourcemaps in bundle server

Reviewed By: davidaurelio

Differential Revision: D3841557

fbshipit-source-id: a6d40cf224ba7c2fd0a8eb0f0e2f7cc4bf222bcb
This commit is contained in:
Charles Dick 2016-09-12 09:14:45 -07:00 committed by Facebook Github Bot 2
parent a105d9e317
commit ab8b8ce9af
5 changed files with 8 additions and 124 deletions

View File

@ -21,7 +21,6 @@ class Bundle extends BundleBase {
super(); super();
this._sourceMap = false; this._sourceMap = false;
this._sourceMapUrl = sourceMapUrl; this._sourceMapUrl = sourceMapUrl;
this._shouldCombineSourceMaps = false;
this._numRequireCalls = 0; this._numRequireCalls = 0;
this._dev = dev; this._dev = dev;
this._minify = minify; this._minify = minify;
@ -41,12 +40,6 @@ class Bundle extends BundleBase {
minify: this._minify, minify: this._minify,
dev: this._dev, dev: this._dev,
}).then(({code, map}) => { }).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( this.replaceModuleAt(
index, new ModuleTransport({...moduleTransport, code, map})); index, new ModuleTransport({...moduleTransport, code, map}));
}); });
@ -137,8 +130,8 @@ class Bundle extends BundleBase {
this.getModules().forEach(module => { this.getModules().forEach(module => {
let map = module.map; let map = module.map;
if (module.virtual) { if (module.virtual || !map) {
map = generateSourceMapForVirtualModule(module); map = generateMissingSourceMapForModule(module);
} }
if (options.excludeSource) { if (options.excludeSource) {
@ -159,23 +152,7 @@ class Bundle extends BundleBase {
getSourceMap(options) { getSourceMap(options) {
super.assertFinalized(); 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() { getEtag() {
@ -189,53 +166,6 @@ class Bundle extends BundleBase {
: 'bundle.js'; : '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() { getJSModulePaths() {
return this.getModules() return this.getModules()
// Filter out non-js files. Like images etc. // Filter out non-js files. Like images etc.
@ -272,7 +202,6 @@ class Bundle extends BundleBase {
...super.toJSON(), ...super.toJSON(),
sourceMapUrl: this._sourceMapUrl, sourceMapUrl: this._sourceMapUrl,
numRequireCalls: this._numRequireCalls, numRequireCalls: this._numRequireCalls,
shouldCombineSourceMaps: this._shouldCombineSourceMaps,
}; };
} }
@ -281,7 +210,6 @@ class Bundle extends BundleBase {
bundle._sourceMapUrl = json.sourceMapUrl; bundle._sourceMapUrl = json.sourceMapUrl;
bundle._numRequireCalls = json.numRequireCalls; bundle._numRequireCalls = json.numRequireCalls;
bundle._shouldCombineSourceMaps = json.shouldCombineSourceMaps;
BundleBase.fromJSON(bundle, json); BundleBase.fromJSON(bundle, json);
@ -289,7 +217,7 @@ class Bundle extends BundleBase {
} }
} }
function generateSourceMapForVirtualModule(module) { function generateMissingSourceMapForModule(module) {
// All lines map 1-to-1 // All lines map 1-to-1
let mappings = 'AAAA;'; let mappings = 'AAAA;';

View File

@ -138,50 +138,6 @@ describe('Bundle', () => {
}); });
describe('sourcemap 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', () => { pit('should combine sourcemaps', () => {
const otherBundle = new Bundle({sourceMapUrl: 'test_url'}); const otherBundle = new Bundle({sourceMapUrl: 'test_url'});

View File

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

View File

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

View File

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