Merge pull request #118 from kpdecker/runtime-inline

Add support for runtime inline source maps
This commit is contained in:
Linus Unnebäck 2016-10-16 13:53:18 +02:00 committed by GitHub
commit 904bf87c78
3 changed files with 65 additions and 3 deletions

View File

@ -101,6 +101,17 @@ require('source-map-support').install({
});
```
To support files with inline source maps, the `hookRequire` options can be specified, which will monitor all source files for inline source maps.
```js
require('source-map-support').install({
hookRequire: true
});
```
This monkey patches the `require` module loading chain, so is not enabled by default and is not recommended for any sort of production usage.
## Demos
#### Basic Demo

View File

@ -136,7 +136,7 @@ retrieveMapHandlers.push(function(source) {
// Support source map URL as a data url
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
sourceMapData = new Buffer(rawData, "base64").toString();
sourceMappingURL = null;
sourceMappingURL = source;
} else {
// Support source map URLs relative to the source URL
sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
@ -455,6 +455,22 @@ exports.install = function(options) {
retrieveMapHandlers.unshift(options.retrieveSourceMap);
}
// Support runtime transpilers that include inline source maps
if (options.hookRequire && !isInBrowser()) {
var Module = require('module');
var $compile = Module.prototype._compile;
if (!$compile.__sourceMapSupport) {
Module.prototype._compile = function(content, filename) {
fileContentsCache[filename] = content;
sourceMapCache[filename] = undefined;
return $compile.call(this, content, filename);
};
Module.prototype._compile.__sourceMapSupport = true;
}
}
// Configure options
if (!emptyCacheBetweenOperations) {
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?

39
test.js
View File

@ -247,8 +247,6 @@ it('function constructor', function() {
'throw new Function(")");'
], [
'SyntaxError: Unexpected token )',
/^ at (?:Object\.)?Function \((?:unknown source|<anonymous>|native)\)$/,
/^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/,
]);
});
@ -459,6 +457,43 @@ it('should consult all retrieve source map providers', function(done) {
]);
});
it('should allow for runtime inline source maps', function(done) {
var sourceMap = createMultiLineSourceMapWithSourcesContent();
fs.writeFileSync('.generated.jss', 'foo');
compareStdout(function(err) {
fs.unlinkSync('.generated.jss');
done(err);
}, createSingleLineSourceMap(), [
'require("./source-map-support").install({',
' hookRequire: true',
'});',
'require.extensions[".jss"] = function(module, filename) {',
' module._compile(',
JSON.stringify([
'',
'var count = 0;',
'function foo() {',
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
'}',
'process.nextTick(foo);',
'process.nextTick(foo);',
'process.nextTick(function() { console.log(count); });',
'//@ sourceMappingURL=data:application/json;charset=utf8;base64,' + new Buffer(sourceMap.toString()).toString('base64')
].join('\n')),
', filename);',
'};',
'require("./.generated.jss");',
], [
'Error: this is the error',
/^ at foo \(.*\/original.js:1004:5\)$/,
'Error: this is the error',
/^ at foo \(.*\/original.js:1004:5\)$/,
'0', // The retrieval should only be attempted once
]);
});
/* The following test duplicates some of the code in
* `compareStackTrace` but appends a charset to the
* source mapping url.