Skip to content

Commit f791287

Browse files
fatfiszlependu
authored andcommitted
Fix a mistake in chunk name generation (vercel#4573)
This fixes a missed bug introduced in vercel#4510. Because the regexp was `/-[^-]*/` and not `/-[^-]*$/`, a wrong part of the filename was being removed: ``` bad: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-0123456789abcdef-0123456789abcdef.js' good: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-bar-0123456789abcdef' ``` By a stroke of luck this didn't affect the existing dynamically generated chunks. To prevent regression I've added unit tests for the function that generates the name. Btw. in the original issue (vercel#4433) I used the right regexp, I just used the wrong regexp in vercel#4510. cc @timneutkens
1 parent fa4c787 commit f791287

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

server/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { readdirSync, existsSync } from 'fs'
44
export const IS_BUNDLED_PAGE = /^bundles[/\\]pages.*\.js$/
55
export const MATCH_ROUTE_NAME = /^bundles[/\\]pages[/\\](.*)\.js$/
66

7+
export function getChunkNameFromFilename (filename) {
8+
return filename.replace(/-[^-]*$/, '')
9+
}
10+
711
export function getAvailableChunks (distDir) {
812
const chunksDir = join(distDir, 'chunks')
913
if (!existsSync(chunksDir)) return {}
@@ -13,7 +17,7 @@ export function getAvailableChunks (distDir) {
1317

1418
chunkFiles.forEach(filename => {
1519
if (/\.js$/.test(filename)) {
16-
const chunkName = filename.replace(/-[^-]*/, '')
20+
const chunkName = getChunkNameFromFilename(filename)
1721
chunksMap[chunkName] = filename
1822
}
1923
})

test/unit/server-utils.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* global describe, it, expect */
2+
3+
import { getChunkNameFromFilename } from '../../dist/server/utils'
4+
5+
describe('Server utils', () => {
6+
describe('getChunkNameFromFilename', () => {
7+
it('should strip the hash from the filename', () => {
8+
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
9+
expect(getChunkNameFromFilename(filename)).toBe('foo_bar_0123456789abcdef')
10+
})
11+
12+
it('should only strip the part after the last hyphen in the filename', () => {
13+
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
14+
expect(getChunkNameFromFilename(filename)).toBe('foo-bar-0123456789abcdef')
15+
})
16+
})
17+
})

0 commit comments

Comments
 (0)