Skip to content

Commit 9959b05

Browse files
committed
support in-memory windows-like filesystems, fixes webpack#78
1 parent b720229 commit 9959b05

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

lib/MemoryOutputFileSystem.js

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,102 @@ function isFile(item) {
1818
return !item[""];
1919
}
2020

21-
MemoryOutputFileSystem.prototype.mkdirp = function(path, callback) {
22-
if(path == "/") return callback();
23-
path = path.split("/");
24-
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
21+
function pathToArray(path) {
22+
var nix = /^\//.test(path);
23+
if(!nix) {
24+
if(!/^[A-Za-z]:\\/.test(path)) return;
25+
path = path.replace(/\\/g, "/");
26+
}
27+
path = path.replace(/\/+/g, "/"); // multi slashs
28+
path = (nix ? path.substr(1) : path).split("/");
29+
if(!path[path.length-1]) path.pop();
30+
return path;
31+
}
32+
33+
MemoryOutputFileSystem.prototype.mkdirp = function(_path, callback) {
34+
var path = pathToArray(_path);
35+
if(!path) return callback(new Error("Invalid path " + _path));
36+
if(path.length == 0) return callback();
2537
var current = this.data;
26-
for(var i = 1; i < path.length; i++) {
38+
for(var i = 0; i < path.length; i++) {
2739
if(isFile(current[path[i]]))
28-
return callback(new Error("Path is a file " + path.join("/")));
40+
return callback(new Error("Path is a file " + _path));
2941
else if(!isDir(current[path[i]]))
3042
current[path[i]] = {"":true};
3143
current = current[path[i]];
3244
}
3345
return callback();
3446
};
3547

36-
MemoryOutputFileSystem.prototype.mkdir = function(path, callback) {
37-
path = path.split("/");
38-
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
48+
MemoryOutputFileSystem.prototype.mkdir = function(_path, callback) {
49+
var path = pathToArray(_path);
50+
if(!path) return callback(new Error("Invalid path " + _path));
51+
if(path.length == 0) return callback();
3952
var current = this.data;
40-
for(var i = 1; i < path.length - 1; i++) {
53+
for(var i = 0; i < path.length - 1; i++) {
4154
if(!isDir(current[path[i]]))
42-
return callback(new Error("Path doesn't exists " + path.join("/")));
55+
return callback(new Error("Path doesn't exists " + _path));
4356
current = current[path[i]];
4457
}
4558
if(isDir(current[path[i]]))
46-
return callback(new Error("Directory already exist " + path.join("/")));
59+
return callback(new Error("Directory already exist " + _path));
4760
else if(isFile(current[path[i]]))
48-
return callback(new Error("Cannot mkdir on file " + path.join("/")));
61+
return callback(new Error("Cannot mkdir on file " + _path));
4962
current[path[i]] = {"":true};
5063
return callback();
5164
};
52-
MemoryOutputFileSystem.prototype.rmdir = function(path, callback) {
53-
path = path.split("/");
54-
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
65+
66+
MemoryOutputFileSystem.prototype.rmdir = function(_path, callback) {
67+
var path = pathToArray(_path);
68+
if(!path) return callback(new Error("Invalid path " + _path));
69+
if(path.length == 0) return callback(new Error("Path cannot be removed " + _path));
5570
var current = this.data;
56-
for(var i = 1; i < path.length - 1; i++) {
71+
for(var i = 0; i < path.length - 1; i++) {
5772
if(!isDir(current[path[i]]))
58-
return callback(new Error("Path doesn't exists " + path.join("/")));
73+
return callback(new Error("Path doesn't exists " + _path));
5974
current = current[path[i]];
6075
}
6176
if(!isDir(current[path[i]]))
62-
return callback(new Error("Directory doesn't exist " + path.join("/")));
77+
return callback(new Error("Directory doesn't exist " + _path));
6378
delete current[path[i]];
6479
return callback();
6580
};
66-
MemoryOutputFileSystem.prototype.unlink = function(path, callback) {
67-
path = path.split("/");
68-
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
81+
82+
MemoryOutputFileSystem.prototype.unlink = function(_path, callback) {
83+
var path = pathToArray(_path);
84+
if(!path) return callback(new Error("Invalid path " + _path));
85+
if(path.length == 0) return callback(new Error("Path cannot be unlinked " + _path));
6986
var current = this.data;
70-
for(var i = 1; i < path.length - 1; i++) {
87+
for(var i = 0; i < path.length - 1; i++) {
7188
if(!isDir(current[path[i]]))
72-
return callback(new Error("Path doesn't exists " + path.join("/")));
89+
return callback(new Error("Path doesn't exists " + _path));
7390
current = current[path[i]];
7491
}
7592
if(!isFile(current[path[i]]))
76-
return callback(new Error("File doesn't exist " + path.join("/")));
93+
return callback(new Error("File doesn't exist " + _path));
7794
delete current[path[i]];
7895
return callback();
7996
};
80-
MemoryOutputFileSystem.prototype.writeFile = function(path, content, callback) {
97+
98+
MemoryOutputFileSystem.prototype.writeFile = function(_path, content, callback) {
8199
if(!content) return callback(new Error("No content"));
82-
path = path.split("/");
83-
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
100+
var path = pathToArray(_path);
101+
if(!path) return callback(new Error("Invalid path " + _path));
102+
if(path.length == 0) return callback(new Error("Path is not a file " + _path));
84103
var current = this.data;
85-
for(var i = 1; i < path.length - 1; i++) {
104+
for(var i = 0; i < path.length - 1; i++) {
86105
if(!isDir(current[path[i]]))
87-
return callback(new Error("Path doesn't exists " + path.join("/")));
106+
return callback(new Error("Path doesn't exists " + _path));
88107
current = current[path[i]];
89108
}
90109
if(isDir(current[path[i]]))
91-
return callback(new Error("Cannot writeFile on directory " + path.join("/")));
110+
return callback(new Error("Cannot writeFile on directory " + _path));
92111
current[path[i]] = content;
93112
return callback();
94113
};
114+
95115
MemoryOutputFileSystem.prototype.join = function(a, b) {
96-
if(a == "/") return "/" + b;
116+
if(a[a.length-1] == "/") return a + b;
117+
if(a[a.length-1] == "\\") return a + b;
97118
return a + "/" + b;
98119
};

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.10.0-beta7",
3+
"version": "0.10.0-beta8",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD/Labeled Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jade, coffee, css, less, ... and your custom stuff.",
66
"dependencies": {

0 commit comments

Comments
 (0)