From 2c0d44a9021149fa51bb258a258d7f4f818532a5 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Mon, 12 Sep 2016 13:50:20 -0700 Subject: [PATCH] Reverted commit D3841557 Reviewed By: davidaurelio Differential Revision: D3841557 fbshipit-source-id: c6098f0d85aa5c56b4109cd4f1ffe622379ab457 --- packager/react-packager/src/Bundler/Bundle.js | 80 ++++++++++++++++++- .../src/Bundler/__tests__/Bundle-test.js | 44 ++++++++++ .../src/Bundler/__tests__/Bundler-test.js | 2 +- packager/react-packager/src/Bundler/index.js | 5 +- packager/transformer.js | 1 - 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/packager/react-packager/src/Bundler/Bundle.js b/packager/react-packager/src/Bundler/Bundle.js index 50086f21a..acca6649a 100644 --- a/packager/react-packager/src/Bundler/Bundle.js +++ b/packager/react-packager/src/Bundler/Bundle.js @@ -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;'; diff --git a/packager/react-packager/src/Bundler/__tests__/Bundle-test.js b/packager/react-packager/src/Bundler/__tests__/Bundle-test.js index d86847598..1f6f68ffb 100644 --- a/packager/react-packager/src/Bundler/__tests__/Bundle-test.js +++ b/packager/react-packager/src/Bundler/__tests__/Bundle-test.js @@ -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'}); diff --git a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js index b9a01aa9e..dbc00f759 100644 --- a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -220,7 +220,7 @@ describe('Bundler', function() { transform: { dev: true, hot: false, - generateSourceMaps: true, + generateSourceMaps: false, projectRoots, } }, diff --git a/packager/react-packager/src/Bundler/index.js b/packager/react-packager/src/Bundler/index.js index cc2a66d73..20993e4b5 100644 --- a/packager/react-packager/src/Bundler/index.js +++ b/packager/react-packager/src/Bundler/index.js @@ -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, }) { diff --git a/packager/transformer.js b/packager/transformer.js index 165df3adf..0a972982e 100644 --- a/packager/transformer.js +++ b/packager/transformer.js @@ -74,7 +74,6 @@ function buildBabelConfig(filename, options) { const extraConfig = { filename, sourceFileName: filename, - sourceMaps: options.generateSourceMaps, }; let config = Object.assign({}, babelRC, extraConfig);