Skip to content

Commit

Permalink
feat: support [contenthash]
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 24, 2018
2 parents 6fb379f + 6982934 commit 3b6b8ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -164,20 +164,31 @@ The following tokens are replaced in the `name` parameter:
* `[folder]` the folder of the resource is in.
* `[emoji]` a random emoji representation of `options.content`
* `[emoji:<length>]` same as above, but with a customizable number of emojis
* `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md5 hash)
* `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
* other `hashType`s, i. e. `sha1`, `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 md5 hash)
* `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
* other `hashType`s, i. e. `sha1`, `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

``` javascript
// loaderContext.resourcePath = "/app/js/javascript.js"
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
// => js/9473fdd0d880a43c21b7778d34872157.script.js

// loaderContext.resourcePath = "/app/js/javascript.js"
loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... });
// => js/9473fdd0d880a43c21b7778d34872157.script.js

// loaderContext.resourcePath = "/app/page.html"
loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... });
// => html-9473fd.html
Expand Down
4 changes: 3 additions & 1 deletion lib/interpolateName.js
Expand Up @@ -67,8 +67,10 @@ function interpolateName(loaderContext, name, options) {
if(content) {
// Match hash template
url = url
// `hash` and `contenthash` are same in `loader-utils` context
// let's keep `hash` for backward compatibility
.replace(
/\[(?:([^:]+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig,
/\[(?:([^:]+):)?(?:hash||contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/ig,
(all, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
)
.replace(
Expand Down
5 changes: 5 additions & 0 deletions test/interpolateName.test.js
Expand Up @@ -24,10 +24,15 @@ describe("interpolateName()", () => {

[
["/app/js/javascript.js", "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
["/app/js/javascript.js", "js/[contenthash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
["/app/page.html", "html-[hash:6].html", "test content", "html-9473fd.html"],
["/app/page.html", "html-[contenthash:6].html", "test content", "html-9473fd.html"],
["/app/flash.txt", "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
["/app/flash.txt", "[contenthash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
["/app/img/image.png", "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"],
["/app/img/image.png", "[sha512:contenthash:base64:7].[ext]", "test content", "2BKDTjl.png"],
["/app/dir/file.png", "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
["/app/dir/file.png", "[path][name].[ext]?[contenthash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
["/vendor/test/images/loading.gif", path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"],
["/pathWith.period/filename.js", "js/[name].[ext]", "test content", "js/filename.js"],
["/pathWith.period/filenameWithoutExt", "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"]
Expand Down

0 comments on commit 3b6b8ff

Please sign in to comment.