-
-
Save karlfloersch/7f628c5a1f551a3a84d952c65c5a9b7b to your computer and use it in GitHub Desktop.
Simple apply-loader module for webpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loaderUtils = require('loader-utils'); | |
module.exports = function(source) { | |
this.cacheable && this.cacheable(); | |
var query = loaderUtils.parseQuery(this.query); | |
var args = []; | |
// apply?config=key => sourceFn(require('webpack.config').key) | |
if (typeof query.config === 'string') { | |
if (!query.config in this.options) | |
throw new Error("apply-loader: '"+query.config+"' property not present in webpack config"); | |
args.push(this.options[query.config]); | |
} | |
// apply?obj={a: 1, b:2} => sourceFn({a: 1, b:2}) | |
if (query.obj) | |
args.push(query.obj); | |
// apply?args[]=1&args[]=2 => sourceFn(1, 2) | |
if (Array.isArray(query.args)) | |
args.push.apply(args, query.args); | |
source += "\n\nmodule.exports = module.exports.apply(module, " | |
+ JSON.stringify(args) | |
+ ")"; | |
return source; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "apply-loader", | |
"version": "0.1.0", | |
"description": "Apply loader for webpack", | |
"main": "index.js", | |
"scripts": { | |
}, | |
"author": "Victor Hallberg <victor@hallberg.cc>", | |
"license": "MIT", | |
"dependencies": { | |
"loader-utils": "~0.2.6" | |
}, | |
"keywords": [ | |
"webpack", | |
"loader", | |
"apply" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment