From 161e18fab47c54da2d8d224d78ad2ccb28d2aa6a Mon Sep 17 00:00:00 2001 From: fatfisz Date: Thu, 14 Jun 2018 08:52:29 +0200 Subject: [PATCH] Process available chunk names properly in dev mode --- server/export.js | 2 +- server/index.js | 2 +- server/render.js | 2 +- server/utils.js | 9 ++++++--- test/unit/server-utils.test.js | 26 ++++++++++++++++++++------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/server/export.js b/server/export.js index 2e3985ab06bcd..5d31f4c9bfc08 100644 --- a/server/export.js +++ b/server/export.js @@ -91,7 +91,7 @@ export default async function (dir, options, configuration) { dev: false, staticMarkup: false, hotReloader: null, - availableChunks: getAvailableChunks(distDir) + availableChunks: getAvailableChunks(distDir, false) } const {serverRuntimeConfig, publicRuntimeConfig} = nextConfig diff --git a/server/index.js b/server/index.js index f0602ac435b48..cc52dc7d64657 100644 --- a/server/index.js +++ b/server/index.js @@ -54,7 +54,7 @@ export default class Server { distDir: this.distDir, hotReloader: this.hotReloader, buildId: this.buildId, - availableChunks: dev ? {} : getAvailableChunks(this.distDir), + availableChunks: dev ? {} : getAvailableChunks(this.distDir, dev), generateEtags } diff --git a/server/render.js b/server/render.js index c09d9c389c635..90407dda89863 100644 --- a/server/render.js +++ b/server/render.js @@ -238,7 +238,7 @@ function loadChunks ({ dev, distDir, availableChunks }) { } if (dev) { - availableChunks = getAvailableChunks(distDir) + availableChunks = getAvailableChunks(distDir, dev) } for (var chunk of flushedChunks) { diff --git a/server/utils.js b/server/utils.js index d25203863c8d2..d66ddd1602ff2 100644 --- a/server/utils.js +++ b/server/utils.js @@ -4,11 +4,14 @@ 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) { +export function getChunkNameFromFilename (filename, dev) { + if (dev) { + return filename.replace(/.[^.]*$/, '') + } return filename.replace(/-[^-]*$/, '') } -export function getAvailableChunks (distDir) { +export function getAvailableChunks (distDir, dev) { const chunksDir = join(distDir, 'chunks') if (!existsSync(chunksDir)) return {} @@ -17,7 +20,7 @@ export function getAvailableChunks (distDir) { chunkFiles.forEach(filename => { if (/\.js$/.test(filename)) { - const chunkName = getChunkNameFromFilename(filename) + const chunkName = getChunkNameFromFilename(filename, dev) chunksMap[chunkName] = filename } }) diff --git a/test/unit/server-utils.test.js b/test/unit/server-utils.test.js index acced871f94c8..3a00a7dea5718 100644 --- a/test/unit/server-utils.test.js +++ b/test/unit/server-utils.test.js @@ -4,14 +4,28 @@ 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') + describe('development mode (no chunkhash)', () => { + it('should strip the extension from the filename', () => { + const filename = 'foo_bar_0123456789abcdef.js' + expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef') + }) + + it('should only strip the extension even if there\'s a hyphen in the name', () => { + const filename = 'foo-bar-0123456789abcdef.js' + expect(getChunkNameFromFilename(filename, true)).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') + describe('production mode (with chunkhash)', () => { + it('should strip the hash from the filename', () => { + const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js' + expect(getChunkNameFromFilename(filename, false)).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, false)).toBe('foo-bar-0123456789abcdef') + }) }) }) })