Skip to content

Commit

Permalink
test: add test in ssr-react
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuangzhang committed Nov 25, 2021
1 parent 083f9f9 commit eb5c246
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
11 changes: 11 additions & 0 deletions packages/playground/ssr-react/__tests__/ssr-react.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { editFile, untilUpdated } from '../../testUtils'
import { port } from './serve'
import fetch from 'node-fetch'
import { resolve } from 'path'
import promises from 'fs/promises'

const url = `http://localhost:${port}`

Expand Down Expand Up @@ -62,3 +64,12 @@ test(`circular dependecies modules doesn't throw`, async () => {
'circ-dep-init-a circ-dep-init-b'
)
})

test('Home chunk file should be split succeed', async () => {
const assetsArr = await promises.readdir(
resolve(process.cwd(), './packages/temp/ssr-react/dist/client/assets')
)
const re = /Home\.chunk/
const res = assetsArr.some((asset) => re.test(asset))
expect(res).toBe(true)
})
9 changes: 8 additions & 1 deletion packages/playground/ssr-react/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const react = require('@vitejs/plugin-react')
module.exports = {
plugins: [react()],
build: {
minify: false
minify: false,
manualChunks: (config) => {
return (id) => {
if (id.includes('Home.jsx')) {
return 'Home.chunk'
}
}
}
}
}
16 changes: 12 additions & 4 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export type ResolvedBuildOptions = Required<
Omit<
BuildOptions,
// make deprecated options optional
'base' | 'cleanCssOptions' | 'polyfillDynamicImport' | 'brotliSize' | 'manualChunks'
| 'base'
| 'cleanCssOptions'
| 'polyfillDynamicImport'
| 'brotliSize'
| 'manualChunks'
>
>

Expand Down Expand Up @@ -509,7 +513,7 @@ async function doBuild(
!libOptions &&
output?.format !== 'umd' &&
output?.format !== 'iife'
? (createMoveToVendorChunkFn(config) && config.build.manualChunks?.(config))
? createManualChunksFn(config)
: undefined,
...output
}
Expand Down Expand Up @@ -637,16 +641,20 @@ function getPkgName(root: string) {
return name?.startsWith('@') ? name.split('/')[1] : name
}

function createMoveToVendorChunkFn(config: ResolvedConfig): GetManualChunk {
function createManualChunksFn(config: ResolvedConfig): GetManualChunk {
const cache = new Map<string, boolean>()
return (id, { getModuleInfo }) => {
return (id, { getModuleInfo, getModuleIds }) => {
if (
id.includes('node_modules') &&
!isCSSRequest(id) &&
staticImportedByEntry(id, getModuleInfo, cache)
) {
return 'vendor'
}
return config.build.manualChunks?.(config)(id, {
getModuleInfo,
getModuleIds
})
}
}

Expand Down

0 comments on commit eb5c246

Please sign in to comment.