Skip to content

Commit dd43247

Browse files
committed
Initial commit
0 parents  commit dd43247

12 files changed

+239
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
test/output/*

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# source map loader for webpack
2+
3+
Extracts SourceMaps for source files that as added as `sourceMappingURL` comment.
4+
5+
## Usage
6+
7+
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
8+
9+
10+
### example webpack config
11+
12+
``` javascript
13+
module.exports = {
14+
module: {
15+
preLoaders: [
16+
{
17+
test: /\.js$/,
18+
loader: "source-map-loader"
19+
}
20+
]
21+
}
22+
};
23+
```
24+
25+
This extracts all SourceMaps from all files. That's no so performance-wise so you may only want to apply the loader to relevant files.
26+
27+
## License
28+
29+
MIT (http://www.opensource.org/licenses/mit-license.php)

index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var SourceMap = require("source-map");
6+
var fs = require("fs");
7+
var path = require("path");
8+
var async = require("async");
9+
var loaderUtils = require("loader-utils");
10+
11+
var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*(.*)",
12+
// Matches /* ... */ comments
13+
regex1 = new RegExp("/\\*"+baseRegex+"\\s*\\*/"),
14+
// Matches // .... comments
15+
regex2 = new RegExp("//"+baseRegex+"($|\n|\r\n?)"),
16+
// Matches DataUrls
17+
regexDataUrl = /data:[^;\n]+;base64,(.*)/;
18+
19+
module.exports = function(input, inputMap) {
20+
this.cacheable && this.cacheable();
21+
var resolve = this.resolve;
22+
var addDependency = this.addDependency;
23+
var match = input.match(regex1) || input.match(regex2);
24+
if(match) {
25+
var url = match[1];
26+
var dataUrlMatch = regexDataUrl.exec(url);
27+
var callback = this.async();
28+
if(dataUrlMatch) {
29+
processMap(JSON.parse((new Buffer(dataUrlMatch[1], "base64")).toString()), this.context, callback);
30+
} else {
31+
resolve(this.context, loaderUtils.urlToRequest(url), function(err, result) {
32+
if(err) return callback(err);
33+
addDependency(result);
34+
fs.readFile(result, "utf-8", function(err, content) {
35+
if(err) return callback(err);
36+
processMap(JSON.parse(content), path.dirname(result), callback);
37+
});
38+
}.bind(this));
39+
return;
40+
}
41+
} else {
42+
this.callback(null, input, inputMap);
43+
}
44+
function processMap(map, context, callback) {
45+
if(!map.sourcesContent || map.sourcesContent.length < map.sources.length) {
46+
var missingSources = map.sourcesContent ? map.sources.slice(map.sourcesContent.length) : map.sources;
47+
async.map(missingSources, function(source, callback) {
48+
resolve(context, loaderUtils.urlToRequest(source), function(err, result) {
49+
if(err) return callback(null, null);
50+
addDependency(result);
51+
fs.readFile(result, "utf-8", function(err, content) {
52+
if(err) return callback(null, null);
53+
callback(null, content);
54+
});
55+
});
56+
}, function(err, sourcesContent) {
57+
map.sourcesContent = map.sourcesContent ? map.sourcesContent.concat(sourcesContent) : sourcesContent;
58+
processMap(map, context, callback);
59+
});
60+
return;
61+
}
62+
async.map(map.sources, function(url, callback) {
63+
resolve(context, loaderUtils.urlToRequest(url), callback);
64+
}, function(err, sources) {
65+
map.sources = sources;
66+
callback(null, input.replace(match[0], match[2]), map);
67+
});
68+
}
69+
}

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "source-map-loader",
3+
"version": "0.1.0",
4+
"author": "Tobias Koppers @sokra",
5+
"description": "extracts inlined source map and offers it to webpack",
6+
"scripts": {
7+
"test": "mocha -R spec"
8+
},
9+
"devDependencies": {
10+
"should": "^3.3.1",
11+
"mocha": "^1.18.2"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git://github.com/webpack/source-map-loader.git"
16+
},
17+
"licenses": [
18+
{
19+
"type": "MIT",
20+
"url": "http://www.opensource.org/licenses/mit-license.php"
21+
}
22+
],
23+
"dependencies": {
24+
"loader-utils": "~0.2.2",
25+
"source-map": "~0.1.33",
26+
"async": "^0.9.0"
27+
}
28+
}

test/fixtures/data/external-source-map2.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/external-source-map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=external-source-map.map
3+
// comment

test/fixtures/external-source-map.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/external-source-map2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
//#sourceMappingURL=data/external-source-map2.map
3+
// comment
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
with SourceMap

test/fixtures/inline-source-map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
// @ sourceMappingURL = data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==
3+
// comment

0 commit comments

Comments
 (0)