Skip to content

Commit

Permalink
Fix a mistake in chunk name generation (#4573)
Browse files Browse the repository at this point in the history
This fixes a missed bug introduced in #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 (#4433) I used the right regexp, I just used the wrong regexp in #4510.

cc @timneutkens
  • Loading branch information
fatfisz authored and timneutkens committed Jun 9, 2018
1 parent 7e8acf3 commit c74ad93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { readdirSync, existsSync } from 'fs'
export const IS_BUNDLED_PAGE = /^bundles[/\\]pages.*\.js$/
export const MATCH_ROUTE_NAME = /^bundles[/\\]pages[/\\](.*)\.js$/

export function getChunkNameFromFilename (filename) {
return filename.replace(/-[^-]*$/, '')
}

export function getAvailableChunks (distDir) {
const chunksDir = join(distDir, 'chunks')
if (!existsSync(chunksDir)) return {}
Expand All @@ -13,7 +17,7 @@ export function getAvailableChunks (distDir) {

chunkFiles.forEach(filename => {
if (/\.js$/.test(filename)) {
const chunkName = filename.replace(/-[^-]*/, '')
const chunkName = getChunkNameFromFilename(filename)
chunksMap[chunkName] = filename
}
})
Expand Down
17 changes: 17 additions & 0 deletions test/unit/server-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* global describe, it, expect */

import { getChunkNameFromFilename } from '../../dist/server/utils'

describe('Server utils', () => {
describe('getChunkNameFromFilename', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename)).toBe('foo_bar_0123456789abcdef')
})

it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename)).toBe('foo-bar-0123456789abcdef')
})
})
})

0 comments on commit c74ad93

Please sign in to comment.