Recommended way to retrieve the options of a loader invocation:
// inside your loaderconstoptions=loaderUtils.getOptions(this);
If this.query is a string:
Tries to parse the query string and returns a new object
Throws if it's not a valid query string
If this.query is object-like, it just returns this.query
In any other case, it just returns null
Please note: The returned options object is read-only. It may be re-used across multiple invocations. If you pass it on to another library, make sure to make a deep copy of it:
constoptions=Object.assign( {}, defaultOptions,loaderUtils.getOptions(this) // it is safe to pass null to Object.assign());// don't forget nested objects or arraysoptions.obj =Object.assign({},options.obj); options.arr =options.arr.slice();someLibrary(options);
clone is a good library to make a deep copy of the options.
Options as query strings
If the loader options have been passed as loader query string (loader?some¶ms), the string is parsed by using parseQuery.
parseQuery
Parses a passed string (e.g. loaderContext.resourceQuery) as a query string, and returns an object.
constparams=loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo`if (params.param1 ==="foo") {// do something}
Turns a request into a string that can be used inside require() or import while avoiding absolute paths. Use it instead of JSON.stringify(...) if you're generating code inside a loader.
Why is this necessary? Since webpack calculates the hash before module paths are translated into module ids, we must avoid absolute paths to ensure consistent hashes across different compilations.
This function:
resolves absolute requests into relative requests if the request and the module are on the same hard drive
replaces \ with / if the request and the module are on the same hard drive
won't change the path at all if the request and the module are on different hard drives
applies JSON.stringify to the result
loaderUtils.stringifyRequest(this,"./test.js");// "\"./test.js\""loaderUtils.stringifyRequest(this,".\\test.js");// "\"./test.js\""loaderUtils.stringifyRequest(this,"test");// "\"test\""loaderUtils.stringifyRequest(this,"test/lib/index.js");// "\"test/lib/index.js\""loaderUtils.stringifyRequest(this,"otherLoader?andConfig!test?someConfig");// "\"otherLoader?andConfig!test?someConfig\""loaderUtils.stringifyRequest(this,require.resolve("test"));// "\"../node_modules/some-loader/lib/test.js\""loaderUtils.stringifyRequest(this,"C:\\module\\test.js");// "\"../../test.js\"" (on Windows, in case the module and the request are on the same drive)loaderUtils.stringifyRequest(this,"C:\\module\\test.js");// "\"C:\\module\\test.js\"" (on Windows, in case the module and the request are on different drives)loaderUtils.stringifyRequest(this,"\\\\network-drive\\test.js");// "\"\\\\network-drive\\\\test.js\"" (on Windows, in case the module and the request are on different drives)
urlToRequest
Converts some resource URL to a webpack module request.
i Before call urlToRequest you need call isUrlRequest to ensure it is requestable url
consturl="path/to/module.js";if (loaderUtils.isUrlRequest(url)) {// Logic for requestable urlconstrequest=loaderUtils.urlToRequest(url);} else {// Logic for not requestable url}
Interpolates a filename template using multiple placeholders and/or a regular expression. The template and regular expression are set as query params called name and regExp on the current loader's context.