What is loader-utils?
The loader-utils package provides utility functions for use with webpack loaders. It offers a variety of helper methods to make writing loaders easier and more standardized. These utilities include parsing query strings, hashing, getting options from loaders, and more.
What are loader-utils's main functionalities?
getOptions
This function is used to parse the options passed to a loader. It simplifies the process of handling loader options within a webpack configuration.
const loaderUtils = require('loader-utils');
const options = loaderUtils.getOptions(this);
stringifyRequest
This method helps to convert a module request into a string that can be used inside of a loader. It's useful for ensuring that the request is correctly formatted and can be resolved by webpack.
const loaderUtils = require('loader-utils');
const stringifiedRequest = loaderUtils.stringifyRequest(this, require.resolve('./file.js'));
getHashDigest
This utility generates a hash digest of the given content. It's commonly used for cache busting or to generate unique identifiers for file contents.
const loaderUtils = require('loader-utils');
const hashDigest = loaderUtils.getHashDigest(Buffer.from('some content'), 'sha512', 'hex', 7);
interpolateName
This function allows for the creation of a custom filename based on placeholders and content. It's often used in file-loader and url-loader to generate names based on the file content.
const loaderUtils = require('loader-utils');
const interpolatedName = loaderUtils.interpolateName(this, '[name]_[hash].[ext]', { content: source });
Other packages similar to loader-utils
schema-utils
Schema-utils validates options for webpack loaders and plugins against a JSON Schema. It is similar to loader-utils in that it helps with managing loader options, but it focuses on validation rather than utility functions.
file-loader
File-loader resolves import/require() on a file into a url and emits the file into the output directory. It's similar to loader-utils' interpolateName feature but is a standalone loader that focuses on file handling.
url-loader
Url-loader works like file-loader but can return a Data URL if the file is smaller than a byte limit. It shares some functionality with loader-utils, particularly in generating filenames and paths.
loader-utils
Methods
urlToRequest
Converts some resource URL to a webpack module request.
i Before call urlToRequest
you need call isUrlRequest
to ensure it is requestable url
const url = "path/to/module.js";
if (loaderUtils.isUrlRequest(url)) {
const request = loaderUtils.urlToRequest(url);
} else {
}
Simple example:
const url = "path/to/module.js";
const request = loaderUtils.urlToRequest(url);
Module URLs
Any URL containing a ~
will be interpreted as a module request. Anything after the ~
will be considered the request path.
const url = "~path/to/module.js";
const request = loaderUtils.urlToRequest(url);
Root-relative URLs
URLs that are root-relative (start with /
) can be resolved relative to some arbitrary path by using the root
parameter:
const url = "/path/to/module.js";
const root = "./root";
const request = loaderUtils.urlToRequest(url, root);
To convert a root-relative URL into a module URL, specify a root
value that starts with ~
:
const url = "/path/to/module.js";
const root = "~";
const request = loaderUtils.urlToRequest(url, root);
interpolateName
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.
const interpolatedName = loaderUtils.interpolateName(
loaderContext,
name,
options
);
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 the context
query parameter or option.[folder]
the folder the resource is in[query]
the queryof the resource, i.e. ?foo=bar
[contenthash]
the hash of options.content
(Buffer) (by default it's the hex digest of the xxhash64
hash)[<hashType>:contenthash:<digestType>:<length>]
optionally one can configure
- other
hashType
s, i. e. xxhash64
, sha1
, md4
(wasm version), native-md4
(crypto
module version), md5
, sha256
, sha512
- other
digestType
s, i. e. hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
- and
length
the length in chars
[hash]
the hash of options.content
(Buffer) (by default it's the hex digest of the xxhash64
hash)[<hashType>:hash:<digestType>:<length>]
optionally one can configure
- other
hashType
s, i. e. xxhash64
, sha1
, md4
(wasm version), native-md4
(crypto
module version), md5
, sha256
, sha512
- other
digestType
s, i. e. hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
- and
length
the length in chars
[N]
the N-th match obtained from matching the current file name against options.regExp
In loader context [hash]
and [contenthash]
are the same, but we recommend using [contenthash]
for avoid misleading.
Examples
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext][query]", { content: ... });
loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... });
loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... });
loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... });
loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7].[ext]", { content: ... });
loaderUtils.interpolateName(loaderContext, "picture.png");
loaderUtils.interpolateName(loaderContext, "[path][name].[ext]?[hash]", { content: ... });
loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: ... });
loaderUtils.interpolateName(
loaderContext,
(resourcePath, resourceQuery) => {
return "js/[hash].script.[ext]";
},
{ content: ... }
);
getHashDigest
const digestString = loaderUtils.getHashDigest(
buffer,
hashType,
digestType,
maxLength
);
buffer
the content that should be hashedhashType
one of xxhash64
, sha1
, md4
, md5
, sha256
, sha512
or any other node.js supported hash typedigestType
one of hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
maxLength
the maximum length in chars
License
MIT (http://www.opensource.org/licenses/mit-license.php)