Skip to content

Commit 0ea4d42

Browse files
authored
fix: Issue when hash length is set (#182)
Fix issue when hash length is set.
1 parent 948da4a commit 0ea4d42

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

.changeset/strong-readers-count.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@codecov/bundler-plugin-core": patch
3+
"@codecov/bundle-analyzer": patch
4+
"@codecov/nextjs-webpack-plugin": patch
5+
"@codecov/nuxt-plugin": patch
6+
"@codecov/remix-vite-plugin": patch
7+
"@codecov/rollup-plugin": patch
8+
"@codecov/solidstart-plugin": patch
9+
"@codecov/sveltekit-plugin": patch
10+
"@codecov/vite-plugin": patch
11+
"@codecov/webpack-plugin": patch
12+
---
13+
14+
Fix issue with normalizing paths with a custom hash length is set

codecov.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
comment:
22
layout: "condensed_header, condensed_files, components, condensed_footer"
33

4-
ai_pr_review:
5-
enabled: true
6-
74
component_management:
85
individual_components:
96
- component_id: package_core

integration-tests/fixtures/generate-bundle-stats/rollup/__snapshots__/rollup-plugin.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ exports[`Generating rollup stats 4 {"format":"amd","expected":"amd"} matches the
776776
{
777777
"gzipSize": 98808,
778778
"name": "main-H2_1FSsQ.js",
779-
"normalized": "main-H2_1FSsQ.js",
779+
"normalized": "main-*.js",
780780
"size": 577073,
781781
},
782782
],

integration-tests/fixtures/generate-bundle-stats/vite/__snapshots__/vite-plugin.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ exports[`Generating vite stats v5 {"format":"cjs","expected":"cjs"} matches the
10071007
{
10081008
"gzipSize": 26812,
10091009
"name": "assets/index-_9bu_Rar.js",
1010-
"normalized": "assets/index-_9bu_Rar.js",
1010+
"normalized": "assets/index-*.js",
10111011
"size": 72342,
10121012
},
10131013
],

integration-tests/fixtures/generate-bundle-stats/webpack/__snapshots__/webpack-plugin.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ exports[`Generating webpack stats 5 {"format":"commonjs","expected":"cjs"} match
150150
}
151151
`;
152152
153-
exports[`Generating webpack stats 5 source maps are enabled does not include any source maps 1`] = `
153+
exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches the snapshot 1`] = `
154154
{
155155
"assets": [
156156
{
@@ -161,7 +161,7 @@ exports[`Generating webpack stats 5 source maps are enabled does not include any
161161
},
162162
],
163163
"builtAt": Any<Number>,
164-
"bundleName": StringNotContaining ".map",
164+
"bundleName": StringContaining "test-webpack-v5-esm",
165165
"bundler": {
166166
"name": "webpack",
167167
"version": "5.90.0",
@@ -225,7 +225,7 @@ exports[`Generating webpack stats 5 source maps are enabled does not include any
225225
}
226226
`;
227227
228-
exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches the snapshot 1`] = `
228+
exports[`Generating webpack stats 5 source maps are enabled does not include any source maps 1`] = `
229229
{
230230
"assets": [
231231
{
@@ -236,7 +236,7 @@ exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches
236236
},
237237
],
238238
"builtAt": Any<Number>,
239-
"bundleName": StringContaining "test-webpack-v5-esm",
239+
"bundleName": StringNotContaining ".map",
240240
"bundler": {
241241
"name": "webpack",
242242
"version": "5.90.0",

packages/bundler-plugin-core/src/utils/__tests__/normalizePath.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ const tests: Test[] = [
8383
},
8484
expected: "test.*",
8585
},
86+
{
87+
name: "should replace '[hash:22]' with '*'",
88+
input: {
89+
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
90+
format: "[name].[hash:22].chunk.js",
91+
},
92+
expected: "test.*.chunk.js",
93+
},
94+
{
95+
name: "should replace '[contenthash:22]' with '*'",
96+
input: {
97+
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
98+
format: "[name].[contenthash:22].chunk.js",
99+
},
100+
expected: "test.*.chunk.js",
101+
},
102+
{
103+
name: "should replace '[fullhash:22]' with '*'",
104+
input: {
105+
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
106+
format: "[name].[fullhash:22].chunk.js",
107+
},
108+
expected: "test.*.chunk.js",
109+
},
110+
{
111+
name: "should replace '[chunkhash:22]' with '*'",
112+
input: {
113+
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
114+
format: "[name].[chunkhash:22].chunk.js",
115+
},
116+
expected: "test.*.chunk.js",
117+
},
86118
];
87119

88120
describe("normalizePath", () => {

packages/bundler-plugin-core/src/utils/normalizePath.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
const HASH_REGEX = /[a-f0-9]{8,}/i;
2-
const POTENTIAL_HASHES = [
3-
"[hash]",
4-
"[contenthash]",
5-
"[fullhash]",
6-
"[chunkhash]",
7-
];
2+
const POTENTIAL_HASHES = ["[contenthash", "[fullhash", "[chunkhash", "[hash"];
83

94
const escapeRegex = (string: string): string =>
105
string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
@@ -33,9 +28,10 @@ export const normalizePath = (path: string, format: string): string => {
3328
leadingDelimiter,
3429
)})`;
3530

31+
const closingBracketIndex = format.slice(match.hashIndex).indexOf("]");
3632
// grab the ending delimiter and create a regex group for it
3733
let endingDelimiter =
38-
format.at(match.hashIndex + match.hashString.length) ?? "";
34+
format.at(match.hashIndex + closingBracketIndex + 1) ?? "";
3935

4036
// If the ending delimiter is `[extname]` there won't be a
4137
// `.<file-extension>` so we need to replace it with a `.` for the
@@ -48,8 +44,8 @@ export const normalizePath = (path: string, format: string): string => {
4844

4945
// create a regex that will match the hash
5046
// potential values gathered from: https://en.wikipedia.org/wiki/Base64
51-
// added in `\-` to account for the `-` character which seems to be used by Rollup through testing
52-
const regexString = `(${leadingRegex}(?<hash>[0-9a-zA-Z\/+=-]+)${endingRegex})`;
47+
// added in `\-` and `\_` to account for the `-` `_` as they are included in the potential hashes: https://rollupjs.org/configuration-options/#output-hashcharacters
48+
const regexString = `(${leadingRegex}(?<hash>[0-9a-zA-Z/\+=_\/+=-]+)${endingRegex})`;
5349
const HASH_REPLACE_REGEX = new RegExp(regexString, "i");
5450

5551
// replace the hash with a wildcard and the delimiters

0 commit comments

Comments
 (0)