Support resourceQuery in context dependencies.

This commit is contained in:
Anuraag Agrawal 2017-11-16 15:20:50 +09:00
parent b7c746d73f
commit ce266cb6f6
9 changed files with 69 additions and 5 deletions

View File

@ -22,8 +22,20 @@ class ContextModule extends Module {
// Info from Factory
this.resolveDependencies = resolveDependencies;
this.options = options;
this.context = options.resource;
let resource, resourceQuery;
const queryIdx = options.resource.indexOf("?");
if(queryIdx >= 0) {
resource = options.resource.substr(0, queryIdx);
resourceQuery = options.resource.substr(queryIdx);
} else {
resource = options.resource;
resourceQuery = "";
}
this.options = Object.assign({}, options, {
resource: resource,
resourceQuery: resourceQuery
});
this.context = this.options.resource;
// Info from Build
this.builtTime = undefined;

View File

@ -89,12 +89,14 @@ module.exports = class ContextModuleFactory extends Tapable {
resolveDependencies(fs, options, callback) {
const cmf = this;
let resource = options.resource;
let resourceQuery = options.resourceQuery;
let recursive = options.recursive;
let regExp = options.regExp;
let include = options.include;
let exclude = options.exclude;
if(!regExp || !resource)
return callback(null, []);
const addDirectory = (directory, callback) => {
fs.readdir(directory, (err, files) => {
if(err) return callback(err);
@ -131,7 +133,7 @@ module.exports = class ContextModuleFactory extends Tapable {
this.applyPluginsAsyncWaterfall("alternatives", [obj], (err, alternatives) => {
if(err) return callback(err);
alternatives = alternatives.filter(obj => regExp.test(obj.request)).map(obj => {
const dep = new ContextElementDependency(obj.request);
const dep = new ContextElementDependency(obj.request + resourceQuery, obj.request);
dep.optional = true;
return dep;
});

View File

@ -101,7 +101,7 @@ const createResolveDependenciesFromContextMap = (createContextMap) => {
createContextMap(fs, (err, map) => {
if(err) return callback(err);
const dependencies = Object.keys(map).map((key) => {
return new ContextElementDependency(map[key], key);
return new ContextElementDependency(map[key] + options.resourceQuery, key);
});
callback(null, dependencies);
});

View File

@ -2,6 +2,11 @@ it("should be able to load a file with the require.context method", function() {
require.context("./templates")("./tmpl").should.be.eql("test template");
(require.context("./././templates"))("./tmpl").should.be.eql("test template");
(require.context("././templates/.")("./tmpl")).should.be.eql("test template");
require.context("./loaders/queryloader?dog=bark!./templates?cat=meow")("./tmpl").should.be.eql({
resourceQuery: "?cat=meow",
query: "?dog=bark",
prev: "module.exports = \"test template\";"
});
require . context ( "." + "/." + "/" + "templ" + "ates" ) ( "./subdir/tmpl.js" ).should.be.eql("subdir test template");
require.context("./templates", true, /./)("xyz").should.be.eql("xyz");
});
@ -43,4 +48,4 @@ it("should execute an empty context", function() {
(function() {
context("");
}).should.throw();
});
});

View File

@ -0,0 +1,7 @@
module.exports = function(content) {
return "module.exports = " + JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content
});
};

View File

@ -0,0 +1,10 @@
it("should replace a context with resource query and manual map", function() {
function rqInContext(x) {
return require(x);
}
rqInContext("a").should.be.eql({
resourceQuery: "?cats=meow",
query: "?lions=roar",
prev: "module.exports = \"a\";\n",
});
});

View File

@ -0,0 +1 @@
module.exports = "a";

View File

@ -0,0 +1,7 @@
module.exports = function(content) {
return "module.exports = " + JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content
});
};

View File

@ -0,0 +1,20 @@
var path = require("path");
var webpack = require("../../../../");
module.exports = {
module: {
rules: [
{
test: /a\.js$/,
use: [
"./queryloader?lions=roar"
]
}
]
},
plugins: [
new webpack.ContextReplacementPlugin(/context-replacement.d$/, path.resolve(__dirname, "modules?cats=meow"), {
"a": "./a",
})
]
};