Skip to content

Commit 6982934

Browse files
committedDec 24, 2018
feat: support [contenthash]
1 parent 6fb379f commit 6982934

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed
 

‎README.md

+11
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,31 @@ The following tokens are replaced in the `name` parameter:
164164
* `[folder]` the folder of the resource is in.
165165
* `[emoji]` a random emoji representation of `options.content`
166166
* `[emoji:<length>]` same as above, but with a customizable number of emojis
167+
* `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md5 hash)
168+
* `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
169+
* other `hashType`s, i. e. `sha1`, `md5`, `sha256`, `sha512`
170+
* other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
171+
* and `length` the length in chars
167172
* `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md5 hash)
168173
* `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
169174
* other `hashType`s, i. e. `sha1`, `md5`, `sha256`, `sha512`
170175
* other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
171176
* and `length` the length in chars
172177
* `[N]` the N-th match obtained from matching the current file name against `options.regExp`
173178

179+
In loader context `[hash]` and `[contenthash]` are the same, but we recommend using `[contenthash]` for avoid misleading.
180+
174181
Examples
175182

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

188+
// loaderContext.resourcePath = "/app/js/javascript.js"
189+
loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... });
190+
// => js/9473fdd0d880a43c21b7778d34872157.script.js
191+
181192
// loaderContext.resourcePath = "/app/page.html"
182193
loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... });
183194
// => html-9473fd.html

‎lib/interpolateName.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ function interpolateName(loaderContext, name, options) {
6767
if(content) {
6868
// Match hash template
6969
url = url
70+
// `hash` and `contenthash` are same in `loader-utils` context
71+
// let's keep `hash` for backward compatibility
7072
.replace(
71-
/\[(?:([^:]+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig,
73+
/\[(?:([^:]+):)?(?:hash||contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/ig,
7274
(all, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
7375
)
7476
.replace(

‎test/interpolateName.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ describe("interpolateName()", () => {
2424

2525
[
2626
["/app/js/javascript.js", "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
27+
["/app/js/javascript.js", "js/[contenthash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
2728
["/app/page.html", "html-[hash:6].html", "test content", "html-9473fd.html"],
29+
["/app/page.html", "html-[contenthash:6].html", "test content", "html-9473fd.html"],
2830
["/app/flash.txt", "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
31+
["/app/flash.txt", "[contenthash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
2932
["/app/img/image.png", "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"],
33+
["/app/img/image.png", "[sha512:contenthash:base64:7].[ext]", "test content", "2BKDTjl.png"],
3034
["/app/dir/file.png", "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
35+
["/app/dir/file.png", "[path][name].[ext]?[contenthash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
3136
["/vendor/test/images/loading.gif", path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"],
3237
["/pathWith.period/filename.js", "js/[name].[ext]", "test content", "js/filename.js"],
3338
["/pathWith.period/filenameWithoutExt", "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"]

0 commit comments

Comments
 (0)
Please sign in to comment.