Skip to content

Commit

Permalink
extend existing sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper committed May 12, 2018
1 parent db07fa5 commit acf5655
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/assets/JSAsset.js
Expand Up @@ -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/;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/integration/sourcemap-existing/index.js
@@ -0,0 +1,5 @@
const sum = require('./sum');

module.exports = function() {
return sum(1, 2);
}
7 changes: 7 additions & 0 deletions test/integration/sourcemap-existing/sum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions 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"
]
}
36 changes: 36 additions & 0 deletions test/sourcemaps.js
Expand Up @@ -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);
});
});

0 comments on commit acf5655

Please sign in to comment.