Skip to content

Commit

Permalink
fix(dynamicImports): compressed on the closeBundle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Nov 28, 2022
1 parent f9d61d9 commit 61044b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fsp from 'fs/promises'
import path from 'path'
import { createFilter } from '@rollup/pluginutils'
import { len, replaceFileName } from './utils'
import { len, replaceFileName, slash } from './utils'
import { defaultCompressionOptions, ensureAlgorithm, transfer } from './compress'
import type { Plugin } from 'vite'
import type { AlgorithmFunction, CompressionOptions, ViteCompressionPluginConfig } from './interface'
Expand Down Expand Up @@ -27,10 +29,13 @@ function compression<T>(opts: ViteCompressionPluginConfig<T> = {}): Plugin {
}
>()

const dynamicImports = []

const zlib: {
algorithm: AlgorithmFunction<T>
filename: string | ((id: string) => string)
options: CompressionOptions<T>
dest: string
} = Object.create(null)

zlib.algorithm = typeof userAlgorithm === 'string' ? ensureAlgorithm(userAlgorithm).algorithm : userAlgorithm
Expand All @@ -44,6 +49,9 @@ function compression<T>(opts: ViteCompressionPluginConfig<T> = {}): Plugin {
name: 'vite-plugin-compression',
apply: 'build',
enforce: 'post',
configResolved(config) {
zlib.dest = config.build.outDir
},
async generateBundle(_, bundles) {
for (const fileName in bundles) {
if (!filter(fileName)) continue
Expand All @@ -52,6 +60,10 @@ function compression<T>(opts: ViteCompressionPluginConfig<T> = {}): Plugin {
const beforeCompressBytes =
typeof source === 'string' ? Buffer.from(source).byteLength : source.buffer.byteLength
if (beforeCompressBytes < threshold) continue
if (bundle.type === 'chunk' && len(bundle.dynamicImports)) {
dynamicImports.push(fileName)
continue
}
bucket.set(fileName, {
source: Buffer.from(source),
type: bundle.type,
Expand All @@ -64,12 +76,10 @@ function compression<T>(opts: ViteCompressionPluginConfig<T> = {}): Plugin {
if (!len(tasks)) return
await Promise.all(
tasks.map(async (task) => {
const { beforeCompressBytes, type, source } = bucket.get(task)
const { source } = bucket.get(task)
const compressed = await transfer(source, zlib.algorithm, zlib.options)
const fileName = replaceFileName(task, zlib.filename)
this.emitFile({ type: 'asset', source: compressed, fileName })
const afterCompressBytes = compressed.byteLength
bucket.set(task, { beforeCompressBytes, type, afterCompressBytes })
if (deleteOriginalAssets) {
if (Reflect.has(bundles, task)) {
Reflect.deleteProperty(bundles, task)
Expand All @@ -80,6 +90,22 @@ function compression<T>(opts: ViteCompressionPluginConfig<T> = {}): Plugin {
} catch (error) {
this.error(error)
}
},
async closeBundle() {
if (len(dynamicImports)) {
const files = dynamicImports.map((file) => [file, slash(path.join(zlib.dest, file))])
await Promise.all(
files.map(async ([filename, file]) => {
const buf = await fsp.readFile(file)
const compressed = await transfer(buf, zlib.algorithm, zlib.options)
const fileName = replaceFileName(filename, zlib.filename)
await fsp.writeFile(path.join(zlib.dest, fileName), compressed)
if (deleteOriginalAssets) {
await fsp.rm(file, { recursive: true, force: true })
}
})
)
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export const replaceFileName = (staticPath: string, rule: string | ((id: string)
if (len(path)) path = path + '/'
return fileNameTempalte.replace(/\[path\]/, path).replace(/\[base\]/, base)
}

export const slash = (path: string) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
if (isExtendedLengthPath) return path
return path.replace(/\\/g, '/')
}

0 comments on commit 61044b1

Please sign in to comment.