Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add compiler output options support for hash #120

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/interpolateName.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ function interpolateName(loaderContext, name, options) {
}
}
let url = filename;
const compilationOutputOptions = loaderContext._compilation && loaderContext._compilation.outputOptions || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is private properties and don't use their, you need open issue in webpack with feature request provide this option throw api

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi thanks, i will do it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarefiev good news, thanks for helping

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarefiev can you create issue so that we don't lose this problem if it require time?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(content) {
// Match hash template
url = url
.replace(
/\[(?:([^:]+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig,
(all, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
(all, hashType, digestType, maxLength) => {
const resultHashType = hashType || compilationOutputOptions.hashFunction;
const resultDigestType = digestType || compilationOutputOptions.hashDigest;
const resultMaxLength = maxLength && parseInt(maxLength, 10) || compilationOutputOptions.hashDigestLength;
return getHashDigest(content, resultHashType, resultDigestType, resultMaxLength);
}
)
.replace(
/\[emoji(?::(\d+))?\]/ig,
Expand Down
29 changes: 17 additions & 12 deletions test/interpolateName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ describe("interpolateName()", () => {
}

[
["/app/js/javascript.js", "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
["/app/page.html", "html-[hash:6].html", "test content", "html-9473fd.html"],
["/app/flash.txt", "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
["/app/img/image.png", "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"],
["/app/dir/file.png", "[path][name].[ext]?[hash]", "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"]
[{ resourcePath: "/app/js/javascript.js" }, "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"],
[{ resourcePath: "/app/page.html" }, "html-[hash:6].html", "test content", "html-9473fd.html"],
[{ resourcePath: "/app/flash.txt" }, "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
[{ resourcePath: "/app/img/image.png" }, "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"],
[{ resourcePath: "/app/img/image.png", _compilation: { outputOptions: {
hashFunction: "sha512",
hashDigest: "base64",
hashDigestLength: 7
} } }, "[hash].[ext]", "test content", "2BKDTjl.png"],
[{ resourcePath: "/app/dir/file.png" }, "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
[{ resourcePath: "/vendor/test/images/loading.gif" }, path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"],
[{ resourcePath: "/pathWith.period/filename.js" }, "js/[name].[ext]", "test content", "js/filename.js"],
[{ resourcePath: "/pathWith.period/filenameWithoutExt" }, "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"]
].forEach(test => {
it("should interpolate " + test[0] + " " + test[1], () => {
const interpolatedName = loaderUtils.interpolateName({ resourcePath: test[0] }, test[1], { content: test[2] });
it("should interpolate " + test[0].resourcePath + " " + test[1], () => {
const interpolatedName = loaderUtils.interpolateName(test[0], test[1], { content: test[2] });
assert.equal(interpolatedName, test[3]);
});
});
Expand All @@ -48,12 +53,12 @@ describe("interpolateName()", () => {
assert.throws(
() => {
const interpolatedName = loaderUtils.interpolateName(
{ }, "[" + hashName + ":hash:base64:10]", {content:"a"}
{ }, "[" + hashName + ":hash:base64:10]", { content: "a" }
);
// if for any reason the system we're running on has a hash
// algorithm matching any of our bogus names, at least make sure
// the output is not the unmodified name:
assert(interpolatedName[0] !== '[');
assert(interpolatedName[0] !== "[");
},
/digest method not supported/i
);
Expand Down