From acf5655ad0e53ee23b852520d573844b914433d4 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Sat, 12 May 2018 11:48:40 +0200 Subject: [PATCH] extend existing sourcemaps --- src/assets/JSAsset.js | 22 ++++++++++++ test/integration/sourcemap-existing/index.js | 5 +++ test/integration/sourcemap-existing/sum.js | 7 ++++ test/integration/sourcemap-existing/sum.map | 19 +++++++++++ test/sourcemaps.js | 36 ++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 test/integration/sourcemap-existing/index.js create mode 100644 test/integration/sourcemap-existing/sum.js create mode 100644 test/integration/sourcemap-existing/sum.map diff --git a/src/assets/JSAsset.js b/src/assets/JSAsset.js index 55220ff739a..26f1ee137ab 100644 --- a/src/assets/JSAsset.js +++ b/src/assets/JSAsset.js @@ -12,6 +12,9 @@ const babel = require('../transforms/babel'); const generate = require('babel-generator').default; const uglify = require('../transforms/uglify'); const SourceMap = require('../SourceMap'); +const path = require('path'); +const fs = require('../utils/fs'); +const logger = require('../Logger'); const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/; const ENV_RE = /\b(?:process\.env)\b/; @@ -94,6 +97,25 @@ class JSAsset extends Asset { } async pretransform() { + // Get original sourcemap if there is any + let sourceMapLine = this.contents.lastIndexOf('//# sourceMappingURL='); + if (sourceMapLine > -1) { + let sourceMapReference = this.contents.substring(sourceMapLine + 21); + this.contents = this.contents.substring(0, sourceMapLine); + + try { + this.sourceMap = JSON.parse( + await fs.readFile( + path.join(path.dirname(this.name), sourceMapReference) + ) + ); + } catch (e) { + logger.warn( + `Could not load existing sourcemap of ${this.relativeName}.` + ); + } + } + await babel(this); // Inline environment variables diff --git a/test/integration/sourcemap-existing/index.js b/test/integration/sourcemap-existing/index.js new file mode 100644 index 00000000000..66b67d1dbc5 --- /dev/null +++ b/test/integration/sourcemap-existing/index.js @@ -0,0 +1,5 @@ +const sum = require('./sum'); + +module.exports = function() { + return sum(1, 2); +} \ No newline at end of file diff --git a/test/integration/sourcemap-existing/sum.js b/test/integration/sourcemap-existing/sum.js new file mode 100644 index 00000000000..634f219de04 --- /dev/null +++ b/test/integration/sourcemap-existing/sum.js @@ -0,0 +1,7 @@ +function sum(a, b) { + return a + b; +} + +module.exports = sum; + +//# sourceMappingURL=/sum.map \ No newline at end of file diff --git a/test/integration/sourcemap-existing/sum.map b/test/integration/sourcemap-existing/sum.map new file mode 100644 index 00000000000..6334648a7cf --- /dev/null +++ b/test/integration/sourcemap-existing/sum.map @@ -0,0 +1,19 @@ +{ + "version": 3, + "sources": [ + "sum.js" + ], + "names": [ + "sum", + "a", + "b", + "module", + "exports" + ], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,GAAT,CAAaC,CAAb,EAAgBC,CAAhB,EAAmB;AACjB,SAAOD,IAAIC,CAAX;AACD;;AAEDC,OAAOC,OAAP,GAAiBJ,GAAjB", + "file": "sum.map", + "sourceRoot": '', + "sourcesContent": [ + "function sum(a, b) {\n return a + b;\n}\n\nmodule.exports = sum;\n\n" + ] +} \ No newline at end of file diff --git a/test/sourcemaps.js b/test/sourcemaps.js index f9218a5e172..5df2e2a76c0 100644 --- a/test/sourcemaps.js +++ b/test/sourcemaps.js @@ -204,4 +204,40 @@ describe('sourcemaps', function() { let map = fs.readFileSync(path.join(sourcemapReference)).toString(); mapValidator(jsOutput, map); }); + + it('should load existing sourcemaps of libraries', async function() { + let b = await bundle( + __dirname + '/integration/sourcemap-existing/index.js' + ); + + assertBundleTree(b, { + name: 'index.js', + assets: ['index.js', 'sum.js'], + childBundles: [ + { + type: 'map' + } + ] + }); + + let jsOutput = fs.readFileSync(b.name).toString(); + + let sourcemapReference = path.join( + __dirname, + '/dist/', + jsOutput.substring(jsOutput.lastIndexOf('//# sourceMappingURL') + 22) + ); + + assert( + fs.existsSync(path.join(sourcemapReference)), + 'referenced sourcemap should exist' + ); + + let map = fs.readFileSync(path.join(sourcemapReference)).toString(); + assert( + map.indexOf('return a + b;') > -1, + 'Sourcemap should contain the existing sourcemap' + ); + mapValidator(jsOutput, map); + }); });