loader-utils
Methods
getOptions
getOptionsRecommended way to retrieve the options of a loader invocation:
// inside your loader
const options = loaderUtils.getOptions(this);If
this.queryis a string:Tries to parse the query string and returns a new object
Throws if it's not a valid query string
If
this.queryis object-like, it just returnsthis.queryIn 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:
const options = Object.assign(
{},
defaultOptions,
loaderUtils.getOptions(this) // it is safe to pass null to Object.assign()
);
// don't forget nested objects or arrays
options.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
parseQueryParses a passed string (e.g. loaderContext.resourceQuery) as a query string, and returns an object.
The string is parsed like this:
stringifyRequest
stringifyRequestTurns 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 drivewon't change the path at all if the request and the module are on different hard drives
applies
JSON.stringifyto the result
urlToRequest
urlToRequestConverts some resource URL to a webpack module request.
i Before call
urlToRequestyou need callisUrlRequestto ensure it is requestable url
Simple example:
Module URLs
Any URL containing a ~ will be interpreted as a module request. Anything after the ~ will be considered the request path.
Root-relative URLs
URLs that are root-relative (start with /) can be resolved relative to some arbitrary path by using the root parameter:
To convert a root-relative URL into a module URL, specify a root value that starts with ~:
interpolateName
interpolateNameInterpolates 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.
The following tokens are replaced in the name parameter:
[ext]the extension of the resource[name]the basename of the resource[path]the path of the resource relative to thecontextquery parameter or option.[folder]the folder of the resource is in.[emoji]a random emoji representation ofoptions.content[emoji:<length>]same as above, but with a customizable number of emojis[contenthash]the hash ofoptions.content(Buffer) (by default it's the hex digest of the md5 hash)[<hashType>:contenthash:<digestType>:<length>]optionally one can configureother
hashTypes, i. e.sha1,md5,sha256,sha512other
digestTypes, i. e.hex,base26,base32,base36,base49,base52,base58,base62,base64and
lengththe length in chars
[hash]the hash ofoptions.content(Buffer) (by default it's the hex digest of the md5 hash)[<hashType>:hash:<digestType>:<length>]optionally one can configureother
hashTypes, i. e.sha1,md5,sha256,sha512other
digestTypes, i. e.hex,base26,base32,base36,base49,base52,base58,base62,base64and
lengththe length in chars
[N]the N-th match obtained from matching the current file name againstoptions.regExp
In loader context [hash] and [contenthash] are the same, but we recommend using [contenthash] for avoid misleading.
Examples
getHashDigest
getHashDigestbufferthe content that should be hashedhashTypeone ofsha1,md5,sha256,sha512or any other node.js supported hash typedigestTypeone ofhex,base26,base32,base36,base49,base52,base58,base62,base64maxLengththe maximum length in chars
License
MIT (http://www.opensource.org/licenses/mit-license.php)
Last updated
Was this helpful?