Skip to content

Instantly share code, notes, and snippets.

@mogelbrod
Last active March 17, 2019 17:16
Show Gist options
  • Save mogelbrod/20e65bf18c710e6b808a to your computer and use it in GitHub Desktop.
Save mogelbrod/20e65bf18c710e6b808a to your computer and use it in GitHub Desktop.
Simple apply-loader module for webpack, published @ https://www.npmjs.com/package/apply-loader
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 <[email protected]>",
"license": "MIT",
"dependencies": {
"loader-utils": "~0.2.6"
},
"keywords": [
"webpack",
"loader",
"apply"
]
}
@Kreozot
Copy link

Kreozot commented Sep 9, 2015

Don't you want to release it as npm package and add to official list of loaders?

@Kreozot
Copy link

Kreozot commented Sep 9, 2015

And please change comment apply?obj={a: 1, b:2} to apply?{obj: {a: 1, b:2}}. Because the first one doesn't working. (According to documentation of loaderUtils)

@mogelbrod
Copy link
Author

Thanks for the feedback Kreozot! I changed the comment and published the package to npm:
https://www.npmjs.com/package/apply-loader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment