diff --git a/CHANGELOG.md b/CHANGELOG.md index dc49f1a..a4d7d2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.1.1 + +# Patches + +- Tarball should handle right static sources. + ## 1.1.0 # Improve diff --git a/README.md b/README.md index 3723cbb..6173787 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,10 @@ export default defineComponent({ ### Others -If you want to analysis your bundle assets. Maybe you can try [vite-bundle-analyzer](https://github.com/nonzzz/vite-bundle-analyzer) +- If you want to analysis your bundle assets. Maybe you can try [vite-bundle-analyzer](https://github.com/nonzzz/vite-bundle-analyzer) + +- `tarball` option `dest` means to generate a tarball somewhere + ### LICENSE diff --git a/package.json b/package.json index aa12349..79f4e1a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vite-plugin-compression2", "packageManager": "yarn@4.1.0", - "version": "1.1.0", + "version": "1.1.1", "description": "a fast vite compression plugin", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 966d307..6a12852 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,7 +80,7 @@ async function handleStaticFiles(config: ResolvedConfig, callback: (file: string 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)) - Promise.all(staticAssets.map(async (assets) => { + await Promise.all(staticAssets.map(async (assets) => { const file = slash(path.relative(publicPath, assets)) await callback(file, assets) })) @@ -95,6 +95,7 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { let root = process.cwd() const tarball = createTarBall() const queue = createConcurrentQueue(MAX_CONCURRENT) + let ctx: ReturnType return { name: 'vite-plugin-tarball', enforce: 'post', @@ -105,11 +106,9 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { tarball.setOptions({ dests, root }) // No need to add source to pack in configResolved stage // If we do at the start stage. The build task will be slow. - const ctx = compression.getPluginAPI(config.plugins) - if (ctx && ctx.staticOutputs) { - statics.push(...ctx.staticOutputs) - } else { - handleStaticFiles(config, async (file) => { statics.push(file) }) + ctx = compression.getPluginAPI(config.plugins) + if (!ctx) { + await handleStaticFiles(config, async (file) => { statics.push(file) }) } const plugin = config.plugins.find(p => p.name === VITE_INTERNAL_ANALYSIS_PLUGIN) if (!plugin) throw new Error('vite-plugin-cp can\'t be work in versions lower than vite2.0.0') @@ -121,6 +120,9 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { } }, async closeBundle() { + if (!statics.length && ctx && ctx.staticOutputs) { + statics.push(...ctx.staticOutputs) + } for (const dest of outputs) { for (const file of statics) { queue.enqueue(async () => { @@ -210,11 +212,8 @@ function compression(opts outputs.push(...handleOutputOption(config)) // Vite's pubic build: https://github.com/vitejs/vite/blob/HEAD/packages/vite/src/node/build.ts#L704-L709 // copyPublicDir minimum version 3.2+ - await handleStaticFiles(config, async (file, assets) => { - if (!filter(assets)) return - const { size } = await fsp.stat(assets) - if (size > threshold) statics.push(file) - }) + // No need check size here. + await handleStaticFiles(config, async (file) => { statics.push(file) }) const plugin = config.plugins.find(p => p.name === VITE_INTERNAL_ANALYSIS_PLUGIN) if (!plugin) throw new Error('vite-plugin-compression can\'t be work in versions lower than vite2.0.0') hijackGenerateBundle(plugin, generateBundle) @@ -225,15 +224,24 @@ function compression(opts for (const file of statics) { queue.enqueue(async () => { const p = path.join(dest, file) - const buf = await fsp.readFile(p) - const compressed = await compress(buf, zlib.algorithm, zlib.options) - if (skipIfLargerOrEqual && len(compressed) >= len(buf)) return - const fileName = replaceFileName(file, zlib.filename) - if (!pluginContext.staticOutputs.has(fileName)) pluginContext.staticOutputs.add(fileName) - // issue #30 - const outputPath = path.join(dest, fileName) - if (deleteOriginalAssets && outputPath !== p) await fsp.rm(p, { recursive: true, force: true }) - await fsp.writeFile(outputPath, compressed) + if (!filter(p) && !pluginContext.staticOutputs.has(file)) { + pluginContext.staticOutputs.add(file) + } else { + const { size } = await fsp.stat(p) + if (size < threshold) { + if (!pluginContext.staticOutputs.has(file)) pluginContext.staticOutputs.add(file) + } else { + const buf = await fsp.readFile(p) + const compressed = await compress(buf, zlib.algorithm, zlib.options) + if (skipIfLargerOrEqual && len(compressed) >= len(buf)) return + const fileName = replaceFileName(file, zlib.filename) + if (!pluginContext.staticOutputs.has(fileName)) pluginContext.staticOutputs.add(fileName) + // issue #30 + const outputPath = path.join(dest, fileName) + if (deleteOriginalAssets && outputPath !== p) await fsp.rm(p, { recursive: true, force: true }) + await fsp.writeFile(outputPath, compressed) + } + } }) } }