Skip to content

Instantly share code, notes, and snippets.

@karlfloersch
Forked from mogelbrod/index.js
Created April 4, 2016 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlfloersch/7f628c5a1f551a3a84d952c65c5a9b7b to your computer and use it in GitHub Desktop.
Save karlfloersch/7f628c5a1f551a3a84d952c65c5a9b7b to your computer and use it in GitHub Desktop.
Simple apply-loader module for webpack
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;
};
{
"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