Skip to content

Commit

Permalink
feat: static directory support size check
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jun 26, 2023
1 parent f28a0bc commit c27275d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 8 additions & 0 deletions __tests__/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ test('public assets nest', async (t) => {
fianl.forEach((p) => t.is(fs.existsSync(p), true))
})

test('public assets threshold', async (t) => {
const id = await mockBuild({ deleteOriginalAssets: true, exclude: /\.(html)$/, threshold: 1024 * 2 }, 'public-assets-nest')
await sleep(3000)
const r = await readAll(path.join(dist, id))
const compressed = len(r.filter((s) => s.endsWith('.gz')))
t.is(compressed, 0)
})

test('exclude-assets', async (t) => {
const id = await mockBuild({ exclude: [/\.(gif)$/] }, 'exclude-assets')
await sleep(3000)
Expand Down
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function compression<T, A extends Algorithm>(opts: ViteCompressionPluginConfig<T
async configResolved(config) {
// issue #26
// https://github.com/vitejs/vite/blob/716286ef21f4d59786f21341a52a81ee5db58aba/packages/vite/src/node/build.ts#L566-L611
// Unfortunately. Vite follow rollup option as first and the configResolved Hook don't expose merged conf for user. :(
// Vite follow rollup option as first and the configResolved Hook don't expose merged conf for user. :(
// Someone who like using rollupOption. `config.build.outDir` will not as expected.
handleOutputOption(config, normalizedOutputs)
// Vite's pubic build: https://github.com/vitejs/vite/blob/HEAD/packages/vite/src/node/build.ts#L704-L709
Expand All @@ -99,19 +99,21 @@ function compression<T, A extends Algorithm>(opts: ViteCompressionPluginConfig<T
if (config.publicDir && baseCondit && fs.existsSync(config.publicDir)) {
const staticAssets = await readAll(config.publicDir)
const publicPath = path.join(config.root, path.relative(config.root, config.publicDir))
staticAssets.forEach((assets) => {
Promise.all(staticAssets.map(async (assets) => {
if (!filter(assets)) return
const { size } = await fsp.stat(assets)
if (size < threshold) return
const file = path.relative(publicPath, assets)
if (!filter(file)) return
const { files, dests } = makeOutputs(normalizedOutputs, file)
stores.set(slash(file), {
effect: true,
file: files,
dest: dests
})
})
}))
}
},
// Unfortunately. Vite support using object as hooks to change execution order need at least 3.1.0
// Vite support using object as hooks to change execution order need at least 3.1.0
// So we should record that with side Effect bundle file. (Because file with dynamic import will trigger vite's internal importAnalysisBuild logic and it will generator vite's placeholder.)
// Vite importAnalysisBuild source code: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/importAnalysisBuild.ts
// Vite's plugin order see: https://github.com/vitejs/vite/blob/HEAD/packages/vite/src/node/plugins/index.ts#L94-L98
Expand Down

0 comments on commit c27275d

Please # to comment.