Skip to content

Commit

Permalink
fix(webpack): avoid grouping with default name (#7808)
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
pi0 committed Jul 30, 2020
1 parent 0a4aacc commit 2e1025f
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 67 deletions.
8 changes: 2 additions & 6 deletions packages/config/src/config/build.js
Expand Up @@ -62,12 +62,8 @@ export default () => ({
minimizer: undefined,
splitChunks: {
chunks: 'all',
name: undefined,
cacheGroups: {
default: {
name: undefined
}
}
automaticNameDelimiter: '/',
cacheGroups: {}
}
},
splitChunks: {
Expand Down
8 changes: 2 additions & 6 deletions packages/config/test/__snapshots__/options.test.js.snap
Expand Up @@ -113,13 +113,9 @@ Object {
"minimizer": undefined,
"runtimeChunk": "single",
"splitChunks": Object {
"cacheGroups": Object {
"default": Object {
"name": undefined,
},
},
"automaticNameDelimiter": "/",
"cacheGroups": Object {},
"chunks": "all",
"name": undefined,
},
},
"optimizeCSS": false,
Expand Down
16 changes: 4 additions & 12 deletions packages/config/test/config/__snapshots__/index.test.js.snap
Expand Up @@ -89,13 +89,9 @@ Object {
"minimizer": undefined,
"runtimeChunk": "single",
"splitChunks": Object {
"cacheGroups": Object {
"default": Object {
"name": undefined,
},
},
"automaticNameDelimiter": "/",
"cacheGroups": Object {},
"chunks": "all",
"name": undefined,
},
},
"optimizeCSS": undefined,
Expand Down Expand Up @@ -469,13 +465,9 @@ Object {
"minimizer": undefined,
"runtimeChunk": "single",
"splitChunks": Object {
"cacheGroups": Object {
"default": Object {
"name": undefined,
},
},
"automaticNameDelimiter": "/",
"cacheGroups": Object {},
"chunks": "all",
"name": undefined,
},
},
"optimizeCSS": undefined,
Expand Down
25 changes: 10 additions & 15 deletions packages/webpack/src/config/client.js
Expand Up @@ -66,14 +66,14 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
cacheGroups.commons = {
test: /node_modules[\\/](vue|vue-loader|vue-router|vuex|vue-meta|core-js|@babel\/runtime|axios|webpack|setimmediate|timers-browserify|process|regenerator-runtime|cookie|js-cookie|is-buffer|dotprop|nuxt\.js)[\\/]/,
chunks: 'all',
priority: 10,
name: true,
automaticNameDelimiter: '/'
name: 'node_modules/commons',
priority: 10
}
}

if (!this.dev && cacheGroups.default && cacheGroups.default.name === undefined) {
cacheGroups.default.name = (_module, chunks) => {
if (!this.dev && splitChunks.name === undefined) {
const nameMap = { default: 'commons', vendors: 'node_modules' }
splitChunks.name = (_module, chunks, cacheGroup) => {
// Map chunks to names
const names = chunks
.map(c => c.name || '')
Expand All @@ -85,23 +85,18 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
.filter(Boolean)
.sort()

// Fixes https://github.com/nuxt/nuxt.js/issues/7665
// TODO: We need a reproduction for this case (test/fixtures/shared-chunk)
if (!names.length) {
return 'commons/default'
}

// Single chunk is not common
if (names.length === 1) {
return names[0]
// Fallback to webpack chunk name or generated cache group key
if (names.length < 2) {
return chunks[0].name
}

// Use compact name for concatinated modules
let compactName = names.join('~')
if (compactName.length > 32) {
compactName = hash(compactName)
}
return 'commons/' + compactName
const prefix = nameMap[cacheGroup || 'default'] || cacheGroup
return prefix + '/' + compactName
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/dev/modern.client.test.js
Expand Up @@ -16,26 +16,26 @@ describe('modern client mode (SSR)', () => {
test('should contain nomodule legacy resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('script nomodule crossorigin="use-credentials" src="/_nuxt/app.js')
expect(response).toContain('script nomodule crossorigin="use-credentials" src="/_nuxt/commons/app.js')
expect(response).toContain('script nomodule crossorigin="use-credentials" src="/_nuxt/node_modules/commons.js')
})

test('should contain module modern resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('<script type="module" crossorigin="use-credentials" src="/_nuxt/app.modern.js"')
expect(response).toContain('<script type="module" crossorigin="use-credentials" src="/_nuxt/commons/app.modern.js"')
expect(response).toContain('<script type="module" crossorigin="use-credentials" src="/_nuxt/node_modules/commons.modern.js"')
})

test('should contain module preload resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/app.modern.js" as="script">')
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/commons/app.modern.js" as="script">')
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/node_modules/commons.modern.js" as="script">')
})

test('should contain module http2 pushed resources', async () => {
const { headers: { link } } = await rp(url('/'))
expect(link).toEqual([
'</_nuxt/runtime.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/commons/app.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/node_modules/commons.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/app.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
`</_nuxt/pages/index.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script`
].join(', '))
Expand Down
8 changes: 4 additions & 4 deletions test/dev/modern.server.test.js
Expand Up @@ -24,13 +24,13 @@ describe('modern server mode', () => {
test('should use legacy resources by default', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('/_nuxt/app.js')
expect(response).toContain('/_nuxt/commons/app.js')
expect(response).toContain('/_nuxt/node_modules/commons.js')
})

test('should use modern resources for modern resources', async () => {
const { body: response } = await rp(url('/'), { headers: { 'user-agent': modernUA } })
expect(response).toContain('/_nuxt/app.modern.js')
expect(response).toContain('/_nuxt/commons/app.modern.js')
expect(response).toContain('/_nuxt/node_modules/commons.modern.js')
})

test('should include es6 syntax in modern resources', async () => {
Expand All @@ -47,7 +47,7 @@ describe('modern server mode', () => {
const { headers: { link } } = await rp(url('/'))
expect(link).toEqual([
'</_nuxt/runtime.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/commons/app.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/node_modules/commons.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/app.js>; rel=preload; crossorigin=use-credentials; as=script',
`</_nuxt/${wChunk('pages/index.js')}>; rel=preload; crossorigin=use-credentials; as=script`
].join(', '))
Expand All @@ -59,7 +59,7 @@ describe('modern server mode', () => {
})
expect(link).toEqual([
'</_nuxt/runtime.modern.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/commons/app.modern.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/node_modules/commons.modern.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/app.modern.js>; rel=preload; crossorigin=use-credentials; as=script',
`</_nuxt/pages/index.modern.js>; rel=preload; crossorigin=use-credentials; as=script`
].join(', '))
Expand Down
12 changes: 6 additions & 6 deletions test/dev/modern.spa.test.js
Expand Up @@ -24,34 +24,34 @@ describe('modern client mode (SPA)', () => {
test('should contain nomodule legacy resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('src="/_nuxt/app.js" crossorigin="use-credentials" nomodule')
expect(response).toContain('src="/_nuxt/commons/app.js" crossorigin="use-credentials" nomodule')
expect(response).toContain('src="/_nuxt/node_modules/commons.js" crossorigin="use-credentials" nomodule')
})

test('should contain module modern resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('<script src="/_nuxt/app.modern.js" type="module" crossorigin="use-credentials"')
expect(response).toContain('<script src="/_nuxt/commons/app.modern.js" type="module" crossorigin="use-credentials"')
expect(response).toContain('<script src="/_nuxt/node_modules/commons.modern.js" type="module" crossorigin="use-credentials"')
})

test('should contain legacy preload resources', async () => {
const { body: response } = await rp(url('/'))
expect(response).toContain('<link rel="preload" crossorigin="use-credentials" href="/_nuxt/app.js" as="script">')
expect(response).toContain('<link rel="preload" crossorigin="use-credentials" href="/_nuxt/commons/app.js" as="script">')
expect(response).toContain('<link rel="preload" crossorigin="use-credentials" href="/_nuxt/node_modules/commons.js" as="script">')
})

test('should contain legacy http2 pushed resources', async () => {
const { headers: { link } } = await rp(url('/'))
expect(link).toEqual([
'</_nuxt/runtime.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/commons/app.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/node_modules/commons.js>; rel=preload; crossorigin=use-credentials; as=script',
'</_nuxt/app.js>; rel=preload; crossorigin=use-credentials; as=script'
].join(', '))
})

test('should contain modern preload resources', async () => {
const { body: response } = await rp(url('/'), { headers: { 'user-agent': modernUA } })
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/app.modern.js" as="script">')
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/commons/app.modern.js" as="script">')
expect(response).toContain('<link rel="modulepreload" crossorigin="use-credentials" href="/_nuxt/node_modules/commons.modern.js" as="script">')
})

test('should contain safari nomodule fix', async () => {
Expand All @@ -63,7 +63,7 @@ describe('modern client mode (SPA)', () => {
const { headers: { link } } = await rp(url('/'), { headers: { 'user-agent': modernUA } })
expect(link).toEqual([
'</_nuxt/runtime.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/commons/app.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/node_modules/commons.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script',
'</_nuxt/app.modern.js>; rel=modulepreload; crossorigin=use-credentials; as=script'
].join(', '))
})
Expand Down
2 changes: 1 addition & 1 deletion test/dev/spa.test.js
Expand Up @@ -35,7 +35,7 @@ function spaTests ({ isHashMode }) {
test('/ (include preload and prefetch resources)', async () => {
const { head } = await renderRoute('/')
expect(head).toMatch('<link rel="preload" href="/_nuxt/runtime.js" as="script">')
expect(head).toMatch('<link rel="preload" href="/_nuxt/commons/app.js" as="script">')
expect(head).toMatch('<link rel="preload" href="/_nuxt/node_modules/commons.js" as="script">')
expect(head).toMatch('<link rel="preload" href="/_nuxt/app.js" as="script">')
expect(head).toMatch(`<link rel="prefetch" href="/_nuxt/${wChunk('pages/custom.js')}">`)
expect(head).toMatch(`<link rel="prefetch" href="/_nuxt/${wChunk('pages/error-handler-async.js')}">`)
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/shared-chunk/components/lazy.vue
Expand Up @@ -3,3 +3,11 @@
Lazy Components
</div>
</template>

<script>
import 'cheerio'
export default {
}
</script>
12 changes: 1 addition & 11 deletions test/fixtures/shared-chunk/components/shared-vendor.vue
Expand Up @@ -5,15 +5,5 @@
</template>

<script>
import _ from 'lodash'
import $ from 'cheerio'
export default {
data () {
$('<a>A</a>')
return {
test: _.startCase('lodash')
}
}
}
import 'lodash'
</script>
3 changes: 1 addition & 2 deletions test/fixtures/shared-chunk/nuxt.config.js
@@ -1,4 +1,3 @@
export default {
components: true,
modern: true
components: true
}

0 comments on commit 2e1025f

Please sign in to comment.