Skip to content

Commit

Permalink
Load missing source contents if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jul 22, 2018
1 parent 2fd004f commit 66e2e4a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/assets/JSAsset.js
Expand Up @@ -119,23 +119,57 @@ class JSAsset extends Asset {
let dataURLMatch = url.match(DATA_URL_RE);

try {
let json;
let json, filename;
if (dataURLMatch) {
filename = this.name;
json = new Buffer(dataURLMatch[1], 'base64').toString();
} else {
json = await fs.readFile(
path.join(path.dirname(this.name), url),
'utf8'
);
filename = path.join(path.dirname(this.name), url);
json = await fs.readFile(filename, 'utf8');

// Add as a dep so we watch the source map for changes.
this.addDependency(filename, {includedInParent: true});
}

this.sourceMap = JSON.parse(json);

// Add as a dep so we watch the source map for changes.
this.addDependency(match[1], {includedInParent: true});
// Attempt to read missing source contents
if (!this.sourceMap.sourcesContent) {
this.sourceMap.sourcesContent = [];
}

let missingSources = this.sourceMap.sources.slice(
this.sourceMap.sourcesContent.length
);
if (missingSources.length) {
let contents = await Promise.all(
missingSources.map(async source => {
try {
let sourceFile = path.join(
path.dirname(filename),
this.sourceMap.sourceRoot || '',
source
);
let result = await fs.readFile(sourceFile, 'utf8');
this.addDependency(sourceFile, {includedInParent: true});
return result;
} catch (err) {
logger.warn(
`Could not load source file "${source}" in source map of "${
this.relativeName
}".`
);
}
})
);

this.sourceMap.sourcesContent = this.sourceMap.sourcesContent.concat(
contents
);
}
} catch (e) {
logger.warn(
`Could not load existing sourcemap of ${this.relativeName}.`
`Could not load existing sourcemap of "${this.relativeName}".`
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/sourcemap-external-contents/index.js
@@ -0,0 +1,5 @@
const sum = require('./sum');

module.exports = function() {
return sum(1, 2);
}
1 change: 1 addition & 0 deletions test/integration/sourcemap-external-contents/sum.coffee
@@ -0,0 +1 @@
module.exports = (a, b) => a + b
11 changes: 11 additions & 0 deletions test/integration/sourcemap-external-contents/sum.js

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

10 changes: 10 additions & 0 deletions test/integration/sourcemap-external-contents/sum.map
@@ -0,0 +1,10 @@
{
"version": 3,
"file": "sum.js",
"sourceRoot": "",
"sources": [
"sum.coffee"
],
"names": [],
"mappings": ";AAAA;EAAA,MAAM,CAAC,OAAP,GAAiB,CAAA,SAAA,KAAA;WAAA,SAAC,CAAD,EAAI,CAAJ;aAAU,CAAA,GAAI;IAAd;EAAA,CAAA,CAAA,CAAA,IAAA;AAAjB"
}
36 changes: 36 additions & 0 deletions test/sourcemaps.js
Expand Up @@ -274,4 +274,40 @@ describe('sourcemaps', function() {
);
mapValidator(jsOutput, map);
});

it('should load referenced contents of sourcemaps', async function() {
let b = await bundle(
__dirname + '/integration/sourcemap-external-contents/index.js'
);

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'sum.js'],
childBundles: [
{
type: 'map'
}
]
});

let jsOutput = await fs.readFile(b.name, 'utf8');

let sourcemapReference = path.join(
__dirname,
'/dist/',
jsOutput.substring(jsOutput.lastIndexOf('//# sourceMappingURL') + 22)
);

assert(
await fs.exists(path.join(sourcemapReference)),
'referenced sourcemap should exist'
);

let map = await fs.readFile(path.join(sourcemapReference), 'utf8');
assert(
map.indexOf('module.exports = (a, b) => a + b') > -1,
'Sourcemap should contain the existing sourcemap'
);
mapValidator(jsOutput, map);
});
});

0 comments on commit 66e2e4a

Please sign in to comment.