Skip to content

Commit

Permalink
fix: Issue when hash length is set (#182)
Browse files Browse the repository at this point in the history
Fix issue when hash length is set.
  • Loading branch information
nicholas-codecov authored Oct 18, 2024
1 parent 948da4a commit 0ea4d42
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
14 changes: 14 additions & 0 deletions .changeset/strong-readers-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@codecov/bundler-plugin-core": patch
"@codecov/bundle-analyzer": patch
"@codecov/nextjs-webpack-plugin": patch
"@codecov/nuxt-plugin": patch
"@codecov/remix-vite-plugin": patch
"@codecov/rollup-plugin": patch
"@codecov/solidstart-plugin": patch
"@codecov/sveltekit-plugin": patch
"@codecov/vite-plugin": patch
"@codecov/webpack-plugin": patch
---

Fix issue with normalizing paths with a custom hash length is set
3 changes: 0 additions & 3 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
comment:
layout: "condensed_header, condensed_files, components, condensed_footer"

ai_pr_review:
enabled: true

component_management:
individual_components:
- component_id: package_core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ exports[`Generating rollup stats 4 {"format":"amd","expected":"amd"} matches the
{
"gzipSize": 98808,
"name": "main-H2_1FSsQ.js",
"normalized": "main-H2_1FSsQ.js",
"normalized": "main-*.js",
"size": 577073,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ exports[`Generating vite stats v5 {"format":"cjs","expected":"cjs"} matches the
{
"gzipSize": 26812,
"name": "assets/index-_9bu_Rar.js",
"normalized": "assets/index-_9bu_Rar.js",
"normalized": "assets/index-*.js",
"size": 72342,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ exports[`Generating webpack stats 5 {"format":"commonjs","expected":"cjs"} match
}
`;
exports[`Generating webpack stats 5 source maps are enabled does not include any source maps 1`] = `
exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches the snapshot 1`] = `
{
"assets": [
{
Expand All @@ -161,7 +161,7 @@ exports[`Generating webpack stats 5 source maps are enabled does not include any
},
],
"builtAt": Any<Number>,
"bundleName": StringNotContaining ".map",
"bundleName": StringContaining "test-webpack-v5-esm",
"bundler": {
"name": "webpack",
"version": "5.90.0",
Expand Down Expand Up @@ -225,7 +225,7 @@ exports[`Generating webpack stats 5 source maps are enabled does not include any
}
`;
exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches the snapshot 1`] = `
exports[`Generating webpack stats 5 source maps are enabled does not include any source maps 1`] = `
{
"assets": [
{
Expand All @@ -236,7 +236,7 @@ exports[`Generating webpack stats 5 {"format":"module","expected":"esm"} matches
},
],
"builtAt": Any<Number>,
"bundleName": StringContaining "test-webpack-v5-esm",
"bundleName": StringNotContaining ".map",
"bundler": {
"name": "webpack",
"version": "5.90.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ const tests: Test[] = [
},
expected: "test.*",
},
{
name: "should replace '[hash:22]' with '*'",
input: {
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
format: "[name].[hash:22].chunk.js",
},
expected: "test.*.chunk.js",
},
{
name: "should replace '[contenthash:22]' with '*'",
input: {
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
format: "[name].[contenthash:22].chunk.js",
},
expected: "test.*.chunk.js",
},
{
name: "should replace '[fullhash:22]' with '*'",
input: {
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
format: "[name].[fullhash:22].chunk.js",
},
expected: "test.*.chunk.js",
},
{
name: "should replace '[chunkhash:22]' with '*'",
input: {
path: "test.CoScjXRp_rD9HKS--kYO73.chunk.js",
format: "[name].[chunkhash:22].chunk.js",
},
expected: "test.*.chunk.js",
},
];

describe("normalizePath", () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/bundler-plugin-core/src/utils/normalizePath.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
const HASH_REGEX = /[a-f0-9]{8,}/i;
const POTENTIAL_HASHES = [
"[hash]",
"[contenthash]",
"[fullhash]",
"[chunkhash]",
];
const POTENTIAL_HASHES = ["[contenthash", "[fullhash", "[chunkhash", "[hash"];

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

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

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

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

// replace the hash with a wildcard and the delimiters
Expand Down

0 comments on commit 0ea4d42

Please sign in to comment.