feat: new filenames structure (#3789)

* feat: new filenames structure

* refactor: codes format

* refactor: remove [path] in output filename

* refactor: use chunkhash for js files

* refactor: remove normalizeFileName
This commit is contained in:
Clark Du 2018-08-22 22:54:08 +01:00 committed by Sébastien Chopin
parent 22559a838c
commit 567dc860c1
3 changed files with 32 additions and 31 deletions

View File

@ -1,4 +1,5 @@
import path from 'path' import path from 'path'
import consola from 'consola'
import TimeFixPlugin from 'time-fix-plugin' import TimeFixPlugin from 'time-fix-plugin'
import _ from 'lodash' import _ from 'lodash'
@ -22,6 +23,14 @@ export default class WebpackBaseConfig {
this.spinner = builder.spinner this.spinner = builder.spinner
} }
get nuxtEnv() {
return {
isDev: this.options.dev,
isServer: this.isServer,
isClient: !this.isServer
}
}
getBabelOptions() { getBabelOptions() {
const options = _.clone(this.options.build.babel) const options = _.clone(this.options.build.babel)
@ -43,24 +52,16 @@ export default class WebpackBaseConfig {
return options return options
} }
getFileName(name) { getFileName(key) {
const fileName = this.options.build.filenames[name] let fileName = this.options.build.filenames[key]
if (typeof fileName === 'function') { 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) { if (this.options.dev) {
fileName = fileName.replace(/\[(chunkhash|contenthash|hash)(?::(\d+))?\]\./g, '') const hash = /\[(chunkhash|contenthash|hash)(?::(\d+))?\]/.exec(fileName)
} if (hash) {
// Don't use [name] for production assets consola.warn(`Notice: Please do not use ${hash[1]} in dev mode to prevent memory leak`)
if (!this.options.dev && this.options.build.optimization.splitChunks.name !== true) { }
fileName = fileName.replace(/\[name\]\./g, '')
} }
return fileName return fileName
} }
@ -275,11 +276,7 @@ export default class WebpackBaseConfig {
customize(config) { customize(config) {
if (typeof this.options.build.extend === 'function') { if (typeof this.options.build.extend === 'function') {
const extendedConfig = this.options.build.extend.call(this.builder, config, { const extendedConfig = this.options.build.extend.call(this.builder, config, this.nuxtEnv)
isDev: this.options.dev,
isServer: this.isServer,
isClient: !this.isServer
})
// Only overwrite config when something is returned for backwards compatibility // Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) { if (extendedConfig !== undefined) {
return extendedConfig return extendedConfig

View File

@ -51,12 +51,13 @@ export default {
cache: false, cache: false,
publicPath: '/_nuxt/', publicPath: '/_nuxt/',
filenames: { filenames: {
app: '[name].[contenthash].js', // { isDev, isClient, isServer }
chunk: '[name].[contenthash].js', app: ({ isDev }) => isDev ? '[name].js' : '[chunkhash].js',
css: '[name].[contenthash].css', chunk: ({ isDev }) => isDev ? '[name].js' : '[chunkhash].js',
img: 'img/[name].[hash:7].[ext]', css: ({ isDev }) => isDev ? '[name].js' : '[contenthash].css',
font: 'fonts/[name].[hash:7].[ext]', img: ({ isDev }) => isDev ? '[path][name].[ext]' : 'img/[hash:7].[ext]',
video: 'videos/[name].[hash:7].[ext]' font: ({ isDev }) => isDev ? '[path][name].[ext]' : 'fonts/[hash:7].[ext]',
video: ({ isDev }) => isDev ? '[path][name].[ext]' : 'videos/[hash:7].[ext]'
}, },
styleResources: {}, styleResources: {},
plugins: [], plugins: [],

View File

@ -1,3 +1,4 @@
import consola from 'consola'
import { Builder, getPort, loadFixture, Nuxt, rp } from '../utils' import { Builder, getPort, loadFixture, Nuxt, rp } from '../utils'
let port let port
@ -16,8 +17,8 @@ describe('basic dev', () => {
buildDir: '.nuxt-dev', buildDir: '.nuxt-dev',
build: { build: {
filenames: { filenames: {
app: () => { app: ({ isDev }) => {
return 'test-app.[contenthash].js' return isDev ? 'test-app.js' : 'test-app.[contenthash].js'
}, },
chunk: 'test-[name].[contenthash].js' chunk: 'test-[name].[contenthash].js'
}, },
@ -54,9 +55,11 @@ describe('basic dev', () => {
}) })
test('Config: build.filenames', () => { test('Config: build.filenames', () => {
expect(typeof output.filename).toBe('function') expect(output.filename).toBe('test-app.js')
expect(output.filename()).toBe('test-app.js') expect(output.chunkFilename).toBe('test-[name].[contenthash].js')
expect(output.chunkFilename).toBe('test-[name].js') expect(consola.warn).toBeCalledWith(
'Notice: Please do not use contenthash in dev mode to prevent memory leak'
)
}) })
test('/stateless', async () => { test('/stateless', async () => {