Skip to content

Commit f33424a

Browse files
fix: crash when filename and algorithm options are functions (#241)
1 parent 44c16e2 commit f33424a

14 files changed

+958
-1078
lines changed

package-lock.json

+371-629
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@
5757
"cross-env": "^7.0.2",
5858
"del": "^6.0.0",
5959
"del-cli": "^3.0.1",
60-
"eslint": "^7.16.0",
60+
"eslint": "^7.17.0",
6161
"eslint-config-prettier": "^7.1.0",
6262
"eslint-plugin-import": "^2.22.1",
6363
"file-loader": "^6.2.0",
64-
"husky": "^4.3.6",
64+
"husky": "^4.3.7",
6565
"jest": "^26.6.3",
6666
"lint-staged": "^10.5.1",
6767
"memfs": "^3.2.0",
6868
"npm-run-all": "^4.1.5",
6969
"prettier": "^2.1.2",
70-
"standard-version": "^9.0.0",
71-
"webpack": "^5.11.0",
72-
"webpack-stats-plugin": "^1.0.2",
70+
"standard-version": "^9.1.0",
71+
"webpack": "^5.12.3",
72+
"webpack-stats-plugin": "^1.0.3",
7373
"workbox-webpack-plugin": "^6.0.2"
7474
},
7575
"keywords": [

src/index.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import path from "path";
7+
import crypto from "crypto";
78

89
import { validate } from "schema-utils";
910
import serialize from "serialize-javascript";
@@ -126,15 +127,27 @@ class CompressionPlugin {
126127
let relatedName;
127128

128129
if (typeof this.options.algorithm === "function") {
129-
let filenameForRelatedName = this.options.filename;
130-
131-
const index = filenameForRelatedName.indexOf("?");
130+
if (typeof this.options.filename === "function") {
131+
relatedName = `compression-function-${crypto
132+
.createHash("md5")
133+
.update(serialize(this.options.filename))
134+
.digest("hex")}`;
135+
} else {
136+
let filenameForRelatedName = this.options.filename;
137+
138+
const index = filenameForRelatedName.indexOf("?");
139+
140+
if (index >= 0) {
141+
filenameForRelatedName = filenameForRelatedName.substr(
142+
0,
143+
index
144+
);
145+
}
132146

133-
if (index >= 0) {
134-
filenameForRelatedName = filenameForRelatedName.substr(0, index);
147+
relatedName = `${path
148+
.extname(filenameForRelatedName)
149+
.slice(1)}ed`;
135150
}
136-
137-
relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
138151
} else if (this.options.algorithm === "gzip") {
139152
relatedName = "gzipped";
140153
} else {

0 commit comments

Comments
 (0)