Skip to content

Commit 514ad01

Browse files
committed
Shorten sourceURLs fixes webpack#6
1 parent c95d4ca commit 514ad01

7 files changed

Lines changed: 54 additions & 29 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ You can also save this options object in a JSON file and use it with the shell c
361361
outputPostfix: ".chunk.js", // default: "." + output
362362
// postfix appended to id of lazy loaded chunks
363363

364+
context: "/home/node/stuff",
365+
// default: [context] parameter if Programmatically Usage
366+
// default: process.cwd() if Shell Usage
367+
// paths in stats and debug sourceUrl are shortened to this base directory
368+
364369
ouputJsonpFunction: "myJsonp", // default: "webpackJsonp"
365370
// jsonp function used for lazy loaded chunks,
366371
// should be unique for all instances of webpack in a page

bin/webpack.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ if(!output) {
156156
if(!options.outputDirectory) options.outputDirectory = path.dirname(output);
157157
if(!options.output) options.output = path.basename(output);
158158
if(!options.outputPostfix) options.outputPostfix = "." + path.basename(output);
159+
if(!options.context) options.context = process.cwd();
159160

160161
// some listeners for the progress display
161162
if(argv.progress) {
@@ -213,7 +214,8 @@ if(!output) {
213214
console.log(formatOutput(stats, {
214215
colors: argv.colors,
215216
"by-size": argv["by-size"],
216-
verbose: argv.verbose
217+
verbose: argv.verbose,
218+
context: options.context
217219
}));
218220
}
219221
});

lib/createFilenameShortener.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var path = require("path");
6+
7+
module.exports = function(cwd) {
8+
var cwd = cwd;
9+
var cwdParent = path.dirname(cwd);
10+
var buildins = path.join(__dirname, "..");
11+
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
12+
cwd = new RegExp("^" + cwd + "|(!)" + cwd, "g");
13+
var buildinsAsModule = cwd.test(buildins);
14+
cwdParent = cwdParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
15+
cwdParent = new RegExp("^" + cwdParent + "|(!)" + cwdParent, "g");
16+
buildins = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
17+
buildins = new RegExp("^" + buildins + "|(!)" + buildins, "g");
18+
var node_modulesRegExpA = /\/node_modules\//g;
19+
var node_modulesRegExpB = /\\node_modules\\/g;
20+
var index_jsRegExp = /[\\\/]index.js!/g;
21+
function compressFilename(filename) {
22+
if(!filename)
23+
return filename;
24+
if(buildinsAsModule)
25+
filename = filename.replace(buildins, "!(webpack)");
26+
filename = filename.replace(cwd, "!.");
27+
if(!buildinsAsModule)
28+
filename = filename.replace(buildins, "!(webpack)");
29+
filename = filename.replace(node_modulesRegExpA, "/~/");
30+
filename = filename.replace(node_modulesRegExpB, "\\~\\");
31+
filename = filename.replace(index_jsRegExp, "!");
32+
return filename.replace(/^!|!$/, "");
33+
}
34+
return compressFilename;
35+
}

lib/formatOutput.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
15
var sprintf = require("sprintf").sprintf;
6+
var createFilenameShortener = require("./createFilenameShortener");
27
var path = require("path");
38
module.exports = function(stats, options) {
49
var buf = [];
@@ -18,32 +23,7 @@ module.exports = function(stats, options) {
1823
Object.keys(stats.fileSizes).forEach(function(file) {
1924
buf.push(c("\033[1m") + sprintf("%" + (3 + Object.keys(stats.fileSizes)[0].length) + "s", file) + c("\033[22m")+": "+c("\033[1m") + sprintf("%8d", stats.fileSizes[file]) + c("\033[22m") + " characters");
2025
});
21-
var cwd = process.cwd();
22-
var cwdParent = path.dirname(cwd);
23-
var buildins = path.join(__dirname, "..");
24-
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
25-
cwd = new RegExp("^" + cwd + "|(!)" + cwd, "g");
26-
var buildinsAsModule = cwd.test(buildins);
27-
cwdParent = cwdParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
28-
cwdParent = new RegExp("^" + cwdParent + "|(!)" + cwdParent, "g");
29-
buildins = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
30-
buildins = new RegExp("^" + buildins + "|(!)" + buildins, "g");
31-
var node_modulesRegExpA = /\/node_modules\//g;
32-
var node_modulesRegExpB = /\\node_modules\\/g;
33-
var index_jsRegExp = /[\\\/]index.js!/g;
34-
function compressFilename(filename) {
35-
if(!filename)
36-
return filename;
37-
if(buildinsAsModule)
38-
filename = filename.replace(buildins, "!(webpack)");
39-
filename = filename.replace(cwd, "!.");
40-
if(!buildinsAsModule)
41-
filename = filename.replace(buildins, "!(webpack)");
42-
filename = filename.replace(node_modulesRegExpA, "/~/");
43-
filename = filename.replace(node_modulesRegExpB, "\\~\\");
44-
filename = filename.replace(index_jsRegExp, "!");
45-
return filename.replace(/^!|!$/, "");
46-
}
26+
var compressFilename = options.context ? createFilenameShortener(options.context) : function(f) { return f };
4727
if(stats.fileModules) {
4828
buf.push("");
4929
buf.push(" <id> <size> <filename>");

lib/webpack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ module.exports = function(context, moduleName, options, callback) {
8181
options.scriptSrcPrefix = options.scriptSrcPrefix || ""; // DEPRECATED
8282
options.publicPrefix = options.publicPrefix || options.scriptSrcPrefix
8383

84+
options.context = options.context || context;
85+
8486
if(options.output) {
8587
if(!options.outputDirectory) {
8688
options.outputDirectory = path.dirname(options.output);

lib/writeSource.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
205205
}
206206
if(options.debug) {
207207
// create a cool eval for debug mode
208+
var shortenFilename = require("./createFilenameShortener")(options.context);
208209
result = [
209210
"eval(",
210211
JSON.stringify([
@@ -213,7 +214,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
213214
"// module.id = ", module.id, "\n",
214215
"// module.realId = ", module.realId, "\n",
215216
"// module.chunks = ", module.chunks.join(", "), "\n",
216-
"//@ sourceURL=webpack-module:///", encodeURI(module.filename || module.dirname).replace(/%5C|%2F/g, "/").replace(/^\//, "")
217+
"//@ sourceURL=webpack-module:///", encodeURI(shortenFilename(module.filename || module.dirname)).replace(/%5C|%2F/g, "/").replace(/^\//, "")
217218
].join("")),
218219
");"].join("");
219220
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.4.15",
3+
"version": "0.4.16",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loading of js, json, jade, coffee, css, ... out of the box and more with custom loaders.",
66
"dependencies": {

0 commit comments

Comments
 (0)