Last active
March 17, 2019 17:16
-
-
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
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 <[email protected]>", | |
"license": "MIT", | |
"dependencies": { | |
"loader-utils": "~0.2.6" | |
}, | |
"keywords": [ | |
"webpack", | |
"loader", | |
"apply" | |
] | |
} |
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)
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
Don't you want to release it as npm package and add to official list of loaders?