mirror of https://github.com/webpack/webpack.git
				
				
				
			Support resourceQuery in context dependencies.
This commit is contained in:
		
							parent
							
								
									b7c746d73f
								
							
						
					
					
						commit
						ce266cb6f6
					
				|  | @ -22,8 +22,20 @@ class ContextModule extends Module { | ||||||
| 
 | 
 | ||||||
| 		// Info from Factory
 | 		// Info from Factory
 | ||||||
| 		this.resolveDependencies = resolveDependencies; | 		this.resolveDependencies = resolveDependencies; | ||||||
| 		this.options = options; | 		let resource, resourceQuery; | ||||||
| 		this.context = options.resource; | 		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
 | 		// Info from Build
 | ||||||
| 		this.builtTime = undefined; | 		this.builtTime = undefined; | ||||||
|  |  | ||||||
|  | @ -89,12 +89,14 @@ module.exports = class ContextModuleFactory extends Tapable { | ||||||
| 	resolveDependencies(fs, options, callback) { | 	resolveDependencies(fs, options, callback) { | ||||||
| 		const cmf = this; | 		const cmf = this; | ||||||
| 		let resource = options.resource; | 		let resource = options.resource; | ||||||
|  | 		let resourceQuery = options.resourceQuery; | ||||||
| 		let recursive = options.recursive; | 		let recursive = options.recursive; | ||||||
| 		let regExp = options.regExp; | 		let regExp = options.regExp; | ||||||
| 		let include = options.include; | 		let include = options.include; | ||||||
| 		let exclude = options.exclude; | 		let exclude = options.exclude; | ||||||
| 		if(!regExp || !resource) | 		if(!regExp || !resource) | ||||||
| 			return callback(null, []); | 			return callback(null, []); | ||||||
|  | 
 | ||||||
| 		const addDirectory = (directory, callback) => { | 		const addDirectory = (directory, callback) => { | ||||||
| 			fs.readdir(directory, (err, files) => { | 			fs.readdir(directory, (err, files) => { | ||||||
| 				if(err) return callback(err); | 				if(err) return callback(err); | ||||||
|  | @ -131,7 +133,7 @@ module.exports = class ContextModuleFactory extends Tapable { | ||||||
| 								this.applyPluginsAsyncWaterfall("alternatives", [obj], (err, alternatives) => { | 								this.applyPluginsAsyncWaterfall("alternatives", [obj], (err, alternatives) => { | ||||||
| 									if(err) return callback(err); | 									if(err) return callback(err); | ||||||
| 									alternatives = alternatives.filter(obj => regExp.test(obj.request)).map(obj => { | 									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; | 										dep.optional = true; | ||||||
| 										return dep; | 										return dep; | ||||||
| 									}); | 									}); | ||||||
|  |  | ||||||
|  | @ -101,7 +101,7 @@ const createResolveDependenciesFromContextMap = (createContextMap) => { | ||||||
| 		createContextMap(fs, (err, map) => { | 		createContextMap(fs, (err, map) => { | ||||||
| 			if(err) return callback(err); | 			if(err) return callback(err); | ||||||
| 			const dependencies = Object.keys(map).map((key) => { | 			const dependencies = Object.keys(map).map((key) => { | ||||||
| 				return new ContextElementDependency(map[key], key); | 				return new ContextElementDependency(map[key] + options.resourceQuery, key); | ||||||
| 			}); | 			}); | ||||||
| 			callback(null, dependencies); | 			callback(null, dependencies); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|  | @ -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("./././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 ( "." + "/." + "/" + "templ" + "ates" ) ( "./subdir/tmpl.js" ).should.be.eql("subdir test template"); | ||||||
| 	require.context("./templates", true, /./)("xyz").should.be.eql("xyz"); | 	require.context("./templates", true, /./)("xyz").should.be.eql("xyz"); | ||||||
| }); | }); | ||||||
|  | @ -43,4 +48,4 @@ it("should execute an empty context", function() { | ||||||
| 	(function() { | 	(function() { | ||||||
| 		context(""); | 		context(""); | ||||||
| 	}).should.throw(); | 	}).should.throw(); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,7 @@ | ||||||
|  | module.exports = function(content) { | ||||||
|  | 	return "module.exports = " + JSON.stringify({ | ||||||
|  | 		resourceQuery: this.resourceQuery, | ||||||
|  | 		query: this.query, | ||||||
|  | 		prev: content | ||||||
|  | 	}); | ||||||
|  | }; | ||||||
|  | @ -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", | ||||||
|  | 	}); | ||||||
|  | }); | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | module.exports = "a"; | ||||||
|  | @ -0,0 +1,7 @@ | ||||||
|  | module.exports = function(content) { | ||||||
|  | 	return "module.exports = " + JSON.stringify({ | ||||||
|  | 		resourceQuery: this.resourceQuery, | ||||||
|  | 		query: this.query, | ||||||
|  | 		prev: content | ||||||
|  | 	}); | ||||||
|  | }; | ||||||
|  | @ -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", | ||||||
|  | 		}) | ||||||
|  | 	] | ||||||
|  | }; | ||||||
		Loading…
	
		Reference in New Issue