ContextModuleFactory.js
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const asyncLib = require("async");
const path = require("path");
const Tapable = require("tapable");
const ContextModule = require("./ContextModule");
const ContextElementDependency = require("./dependencies/ContextElementDependency");
module.exports = class ContextModuleFactory extends Tapable {
constructor(resolvers) {
super();
this.resolvers = resolvers;
}
create(data, callback) {
const context = data.context;
const dependencies = data.dependencies;
const dependency = dependencies[0];
this.applyPluginsAsyncWaterfall("before-resolve", {
context: context,
request: dependency.request,
recursive: dependency.recursive,
regExp: dependency.regExp,
async: dependency.async,
dependencies: dependencies
}, (err, result) => {
if(err) return callback(err);
// Ignored
if(!result) return callback();
const context = result.context;
const request = result.request;
const recursive = result.recursive;
const regExp = result.regExp;
const asyncContext = result.async;
const dependencies = result.dependencies;
let loaders, resource, loadersPrefix = "";
const idx = request.lastIndexOf("!");
if(idx >= 0) {
loaders = request.substr(0, idx + 1);
let i;
for(i = 0; i < loaders.length && loaders[i] === "!"; i++) {
loadersPrefix += "!";
}
loaders = loaders.substr(i).replace(/!+$/, "").replace(/!!+/g, "!");
if(loaders === "") loaders = [];
else loaders = loaders.split("!");
resource = request.substr(idx + 1);
} else {
loaders = [];
resource = request;
}
const resolvers = this.resolvers;
asyncLib.parallel([
function(callback) {
resolvers.context.resolve({}, context, resource, function(err, result) {
if(err) return callback(err);
callback(null, result);
});
},
function(callback) {
asyncLib.map(loaders, function(loader, callback) {
resolvers.loader.resolve({}, context, loader, function(err, result) {
if(err) return callback(err);
callback(null, result);
});
}, callback);
}
], (err, result) => {
if(err) return callback(err);
this.applyPluginsAsyncWaterfall("after-resolve", {
loaders: loadersPrefix + result[1].join("!") + (result[1].length > 0 ? "!" : ""),
resource: result[0],
recursive: recursive,
regExp: regExp,
async: asyncContext,
dependencies: dependencies,
resolveDependencies: this.resolveDependencies.bind(this)
}, function(err, result) {
if(err) return callback(err);
// Ignored
if(!result) return callback();
return callback(null, new ContextModule(result.resolveDependencies, result.resource, result.recursive, result.regExp, result.loaders, result.async, dependency.chunkName));
});
});
});
}
resolveDependencies(fs, resource, recursive, regExp, callback) {
const cmf = this;
if(!regExp || !resource)
return callback(null, []);
(function addDirectory(directory, callback) {
fs.readdir(directory, (err, files) => {
if(err) return callback(err);
files = cmf.applyPluginsWaterfall("context-module-files", files);
if(!files || files.length === 0) return callback(null, []);
asyncLib.map(files.filter(function(p) {
return p.indexOf(".") !== 0;
}), (seqment, callback) => {
const subResource = path.join(directory, seqment);
fs.stat(subResource, (err, stat) => {
if(err) {
if(err.code === "ENOENT") {
// ENOENT is ok here because the file may have been deleted between
// the readdir and stat calls.
return callback();
} else {
return callback(err);
}
}
if(stat.isDirectory()) {
if(!recursive) return callback();
addDirectory.call(this, subResource, callback);
} else if(stat.isFile()) {
const obj = {
context: resource,
request: "." + subResource.substr(resource.length).replace(/\\/g, "/")
};
this.applyPluginsAsyncWaterfall("alternatives", [obj], (err, alternatives) => {
if(err) return callback(err);
alternatives = alternatives.filter(function(obj) {
return regExp.test(obj.request);
}).map(function(obj) {
const dep = new ContextElementDependency(obj.request);
dep.optional = true;
return dep;
});
callback(null, alternatives);
});
} else callback();
});
}, (err, result) => {
if(err) return callback(err);
if(!result) return callback(null, []);
callback(null, result.filter(function(i) {
return !!i;
}).reduce(function(a, i) {
return a.concat(i);
}, []));
});
});
}.call(this, resource, callback));
}
};