diff --git a/lib/builder/webpack/base.js b/lib/builder/webpack/base.js index 429147a947..877a5aa9e3 100644 --- a/lib/builder/webpack/base.js +++ b/lib/builder/webpack/base.js @@ -1,4 +1,5 @@ import path from 'path' +import consola from 'consola' import TimeFixPlugin from 'time-fix-plugin' import _ from 'lodash' @@ -22,6 +23,14 @@ export default class WebpackBaseConfig { this.spinner = builder.spinner } + get nuxtEnv() { + return { + isDev: this.options.dev, + isServer: this.isServer, + isClient: !this.isServer + } + } + getBabelOptions() { const options = _.clone(this.options.build.babel) @@ -43,24 +52,16 @@ export default class WebpackBaseConfig { return options } - getFileName(name) { - const fileName = this.options.build.filenames[name] - + getFileName(key) { + let fileName = this.options.build.filenames[key] if (typeof fileName === 'function') { - return name => this.normalizeFileName(fileName(name)) + fileName = fileName(this.nuxtEnv) } - return this.normalizeFileName(fileName) - } - - normalizeFileName(fileName) { - // Don't use hashes when watching - // https://github.com/webpack/webpack/issues/1914#issuecomment-174171709 if (this.options.dev) { - fileName = fileName.replace(/\[(chunkhash|contenthash|hash)(?::(\d+))?\]\./g, '') - } - // Don't use [name] for production assets - if (!this.options.dev && this.options.build.optimization.splitChunks.name !== true) { - fileName = fileName.replace(/\[name\]\./g, '') + const hash = /\[(chunkhash|contenthash|hash)(?::(\d+))?\]/.exec(fileName) + if (hash) { + consola.warn(`Notice: Please do not use ${hash[1]} in dev mode to prevent memory leak`) + } } return fileName } @@ -275,11 +276,7 @@ export default class WebpackBaseConfig { customize(config) { if (typeof this.options.build.extend === 'function') { - const extendedConfig = this.options.build.extend.call(this.builder, config, { - isDev: this.options.dev, - isServer: this.isServer, - isClient: !this.isServer - }) + const extendedConfig = this.options.build.extend.call(this.builder, config, this.nuxtEnv) // Only overwrite config when something is returned for backwards compatibility if (extendedConfig !== undefined) { return extendedConfig diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index aea4834b6b..243a83507a 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -51,12 +51,13 @@ export default { cache: false, publicPath: '/_nuxt/', filenames: { - app: '[name].[contenthash].js', - chunk: '[name].[contenthash].js', - css: '[name].[contenthash].css', - img: 'img/[name].[hash:7].[ext]', - font: 'fonts/[name].[hash:7].[ext]', - video: 'videos/[name].[hash:7].[ext]' + // { isDev, isClient, isServer } + app: ({ isDev }) => isDev ? '[name].js' : '[chunkhash].js', + chunk: ({ isDev }) => isDev ? '[name].js' : '[chunkhash].js', + css: ({ isDev }) => isDev ? '[name].js' : '[contenthash].css', + img: ({ isDev }) => isDev ? '[path][name].[ext]' : 'img/[hash:7].[ext]', + font: ({ isDev }) => isDev ? '[path][name].[ext]' : 'fonts/[hash:7].[ext]', + video: ({ isDev }) => isDev ? '[path][name].[ext]' : 'videos/[hash:7].[ext]' }, styleResources: {}, plugins: [], diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 479d94ac22..5a6a181658 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -1,3 +1,4 @@ +import consola from 'consola' import { Builder, getPort, loadFixture, Nuxt, rp } from '../utils' let port @@ -16,8 +17,8 @@ describe('basic dev', () => { buildDir: '.nuxt-dev', build: { filenames: { - app: () => { - return 'test-app.[contenthash].js' + app: ({ isDev }) => { + return isDev ? 'test-app.js' : 'test-app.[contenthash].js' }, chunk: 'test-[name].[contenthash].js' }, @@ -54,9 +55,11 @@ describe('basic dev', () => { }) test('Config: build.filenames', () => { - expect(typeof output.filename).toBe('function') - expect(output.filename()).toBe('test-app.js') - expect(output.chunkFilename).toBe('test-[name].js') + expect(output.filename).toBe('test-app.js') + expect(output.chunkFilename).toBe('test-[name].[contenthash].js') + expect(consola.warn).toBeCalledWith( + 'Notice: Please do not use contenthash in dev mode to prevent memory leak' + ) }) test('/stateless', async () => {