Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for runtime inline source maps #118

Merged
merged 5 commits into from
Oct 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 ?
Expand Down
39 changes: 37 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\)$/,
]);
});

Expand Down Expand Up @@ -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.
Expand Down