2017-05-03 15:35:00 +02:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-08-14 05:01:11 +02:00
|
|
|
"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;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-10-14 18:40:54 +02:00
|
|
|
const resolvers = this.resolvers;
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
asyncLib.parallel([
|
|
|
|
function(callback) {
|
|
|
|
resolvers.context.resolve({}, context, resource, function(err, result) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if(err) return callback(err);
|
|
|
|
callback(null, result);
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
},
|
|
|
|
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) => {
|
2017-05-03 15:35:00 +02:00
|
|
|
if(err) return callback(err);
|
|
|
|
|
2017-10-14 18:40:54 +02:00
|
|
|
this.applyPluginsAsyncWaterfall("after-resolve", {
|
2017-08-14 05:01:11 +02:00
|
|
|
loaders: loadersPrefix + result[1].join("!") + (result[1].length > 0 ? "!" : ""),
|
|
|
|
resource: result[0],
|
|
|
|
recursive: recursive,
|
|
|
|
regExp: regExp,
|
|
|
|
async: asyncContext,
|
|
|
|
dependencies: dependencies,
|
2017-10-14 18:40:54 +02:00
|
|
|
resolveDependencies: this.resolveDependencies.bind(this)
|
2017-08-14 05:01:11 +02:00
|
|
|
}, function(err, result) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
|
|
|
|
// Ignored
|
|
|
|
if(!result) return callback();
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return callback(null, new ContextModule(result.resolveDependencies, result.resource, result.recursive, result.regExp, result.loaders, result.async, dependency.chunkName));
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
resolveDependencies(fs, resource, recursive, regExp, callback) {
|
2017-10-14 18:40:54 +02:00
|
|
|
const cmf = this;
|
2017-08-14 05:01:11 +02:00
|
|
|
if(!regExp || !resource)
|
|
|
|
return callback(null, []);
|
|
|
|
(function addDirectory(directory, callback) {
|
|
|
|
fs.readdir(directory, (err, files) => {
|
|
|
|
if(err) return callback(err);
|
2017-10-14 18:40:54 +02:00
|
|
|
files = cmf.applyPluginsWaterfall("context-module-files", files);
|
2017-08-14 05:01:11 +02:00
|
|
|
if(!files || files.length === 0) return callback(null, []);
|
|
|
|
asyncLib.map(files.filter(function(p) {
|
|
|
|
return p.indexOf(".") !== 0;
|
|
|
|
}), (seqment, callback) => {
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const subResource = path.join(directory, seqment);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
fs.stat(subResource, (err, stat) => {
|
2017-12-10 21:51:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if(stat.isDirectory()) {
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if(!recursive) return callback();
|
|
|
|
addDirectory.call(this, subResource, callback);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if(stat.isFile()) {
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const obj = {
|
|
|
|
context: resource,
|
|
|
|
request: "." + subResource.substr(resource.length).replace(/\\/g, "/")
|
|
|
|
};
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
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);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
} else callback();
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
}, (err, result) => {
|
|
|
|
if(err) return callback(err);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if(!result) return callback(null, []);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
callback(null, result.filter(function(i) {
|
|
|
|
return !!i;
|
|
|
|
}).reduce(function(a, i) {
|
|
|
|
return a.concat(i);
|
|
|
|
}, []));
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
}.call(this, resource, callback));
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
};
|