refactor: functional filename should be called from webpack (#3788)

@Atinux Improvement for #3787
This commit is contained in:
Clark Du 2018-08-22 16:47:52 +01:00 committed by GitHub
parent 3e027269c0
commit e8414291b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -44,23 +44,25 @@ export default class WebpackBaseConfig {
} }
getFileName(name) { getFileName(name) {
let fileName = this.options.build.filenames[name] const fileName = this.options.build.filenames[name]
if (typeof fileName === 'function') { if (typeof fileName === 'function') {
fileName = fileName(name) return name => this.normalizeFileName(fileName(name))
} else {
return this.normalizeFileName(fileName)
} }
}
normalizeFileName(fileName) {
// Don't use hashes when watching // Don't use hashes when watching
// https://github.com/webpack/webpack/issues/1914#issuecomment-174171709 // 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, '') fileName = fileName.replace(/\[(chunkhash|contenthash|hash)(?::(\d+))?\]\./g, '')
} }
// Don't use [name] for production assets // Don't use [name] for production assets
if (!this.options.dev && this.options.build.optimization.splitChunks.name !== true) { if (!this.options.dev && this.options.build.optimization.splitChunks.name !== true) {
fileName = fileName.replace(/\[name\]\./g, '') fileName = fileName.replace(/\[name\]\./g, '')
} }
return fileName return fileName
} }

View File

@ -6,7 +6,7 @@ const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null let builder = null
let transpile = null let transpile = null
let appFileName = null let output = null
describe('basic dev', () => { describe('basic dev', () => {
beforeAll(async () => { beforeAll(async () => {
@ -18,17 +18,18 @@ describe('basic dev', () => {
filenames: { filenames: {
app: () => { app: () => {
return 'test-app.[contenthash].js' return 'test-app.[contenthash].js'
} },
chunk: 'test-[name].[contenthash].js'
}, },
transpile: [ transpile: [
'vue\\.test\\.js', 'vue\\.test\\.js',
/vue-test/ /vue-test/
], ],
extend({ module: { rules }, output: { filename } }, { isClient }) { extend({ module: { rules }, output: wpOutput }, { isClient }) {
if (isClient) { if (isClient) {
const babelLoader = rules.find(loader => loader.test.test('.jsx')) const babelLoader = rules.find(loader => loader.test.test('.jsx'))
transpile = file => !babelLoader.exclude(file) transpile = file => !babelLoader.exclude(file)
appFileName = filename output = wpOutput
} }
} }
} }
@ -52,8 +53,10 @@ describe('basic dev', () => {
expect(transpile('node_modules/test.vue.js')).toBe(true) expect(transpile('node_modules/test.vue.js')).toBe(true)
}) })
test('Config: build.filenames as function', () => { test('Config: build.filenames', () => {
expect(appFileName).toBe('test-app.js') expect(typeof output.filename).toBe('function')
expect(output.filename()).toBe('test-app.js')
expect(output.chunkFilename).toBe('test-[name].js')
}) })
test('/stateless', async () => { test('/stateless', async () => {