Commit 3970a26b authored by Alex Castle's avatar Alex Castle Committed by Joe Haddad

Tweaks and test for library chunks fixes (#8482)

* Adjust lib chunk naming algorithm and prevent duplicate react-dom

* Remove alias for react-dom and update separator replacement logic

* Add comment to webpack-config

* Add integration test for react-dom duplication

* Switch to using hash for lib chunk names

* remove extra file from PR

* Remove another extraneous change

* Remove test based on lib chunk name

* Update index.test.js
parent a9325f12
...@@ -234,23 +234,12 @@ export default async function getBaseWebpackConfig( ...@@ -234,23 +234,12 @@ export default async function getBaseWebpackConfig(
/node_modules[/\\]/.test(module.identifier()) /node_modules[/\\]/.test(module.identifier())
) )
}, },
name(module: { identifier: Function; rawRequest: string }): string { name(module: { libIdent: Function }): string {
const identifier = module.identifier() return crypto
// Remove everything up through '/node_modules/' .createHash('sha1')
const trimmedIdentifier = /(?:^|[/\\])node_modules[/\\](.*)/.exec( .update(module.libIdent({ context: dir }))
identifier .digest('hex')
) .substring(0, 8)
const processedIdentifier =
trimmedIdentifier &&
// Remove the file extension(s)
/[\w-\/\\]+/.exec(trimmedIdentifier[1])
let finalName = processedIdentifier && processedIdentifier[0]
finalName = finalName && finalName.replace(/[\\\/]/g, '~')
const backupName = identifier.replace(/[\\\/]/g, '~')
return finalName || backupName
}, },
priority: 30, priority: 30,
minChunks: 1, minChunks: 1,
......
import rd from 'react-dom'
export default () => {
console.log(rd)
return <div>Component One</div>
}
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin
module.exports = { module.exports = {
experimental: { experimental: {
granularChunks: true granularChunks: true
},
webpack (config) {
config.plugins = config.plugins || []
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'disabled',
generateStatsFile: true
})
)
return config
} }
} }
import * as _ from 'lodash' import * as _ from 'lodash'
import dynamic from 'next/dynamic'
const One = dynamic(() => import('../components/one'))
const Page = () => { const Page = () => {
console.log(_) console.log(_)
return <div>page2</div> return (
<div>
page2
<One />
</div>
)
} }
export default Page export default Page
...@@ -10,6 +10,7 @@ const appDir = join(__dirname, '../') ...@@ -10,6 +10,7 @@ const appDir = join(__dirname, '../')
let buildId let buildId
let chunks let chunks
let stats
const existsChunkNamed = name => { const existsChunkNamed = name => {
return chunks.some(chunk => new RegExp(name).test(chunk)) return chunks.some(chunk => new RegExp(name).test(chunk))
...@@ -29,6 +30,8 @@ describe('Chunking', () => { ...@@ -29,6 +30,8 @@ describe('Chunking', () => {
// Error here means old chunks don't exist, so we don't need to do anything // Error here means old chunks don't exist, so we don't need to do anything
} }
await nextBuild(appDir) await nextBuild(appDir)
stats = await readFile(join(appDir, '.next', 'stats.json'), 'utf8')
stats = JSON.parse(stats)
buildId = await readFile(join(appDir, '.next/BUILD_ID'), 'utf8') buildId = await readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
chunks = await readdir(join(appDir, '.next', 'static', 'chunks')) chunks = await readdir(join(appDir, '.next', 'static', 'chunks'))
}) })
...@@ -37,12 +40,6 @@ describe('Chunking', () => { ...@@ -37,12 +40,6 @@ describe('Chunking', () => {
expect(existsChunkNamed('framework')).toBe(true) expect(existsChunkNamed('framework')).toBe(true)
}) })
it('should create a library chunk for lodash', () => {
// This test app has an import on all of lodash in page2.js. Because it is
// a large library, it should be chunked out into its own library chunk
expect(existsChunkNamed('lodash')).toBe(true)
})
it('should not create a commons chunk', () => { it('should not create a commons chunk', () => {
// This app has no dependency that is required by ALL pages, and should // This app has no dependency that is required by ALL pages, and should
// therefore not have a commons chunk // therefore not have a commons chunk
...@@ -62,4 +59,17 @@ describe('Chunking', () => { ...@@ -62,4 +59,17 @@ describe('Chunking', () => {
) )
).toBe(undefined) /* fs.access callback returns undefined if file exists */ ).toBe(undefined) /* fs.access callback returns undefined if file exists */
}) })
it('should not include more than one instance of react-dom', async () => {
const misplacedReactDom = stats.chunks.some(chunk => {
if (chunk.names.includes('framework')) {
// disregard react-dom in framework--it's supposed to be there
return false
}
return chunk.modules.some(module => {
return /react-dom/.test(module.name)
})
})
expect(misplacedReactDom).toBe(false)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment