Skip to content

Commit

Permalink
fix: tarball should work normal without compression
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Mar 3, 2024
1 parent 0b13c56 commit a182ebc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ export default defineComponent({
there are write (delete) operations on the bundle inside the plugin. So you should ignore the compressed chunk :) If you want delete
the original assets you also follow the way.

> Can i create a tarball for all of chunks after compressed?
- Yes, you can import `cp` plugin from this package(>=12.0.0)
> Can i create a tarball for all of assets after compressed?
- Yes, you can import `tarball` plugin from this package(>=12.0.0)
```js
import { defineComponent } from 'vite'
import { compression, cp } from 'vite-plugin-compression2'
import { compression, tarball } from 'vite-plugin-compression2'

export default defineComponent({
plugins: [
// ...your plugin
compression(),
cp({ dest: './xzy' })
tarball()
]
})

Expand Down
51 changes: 45 additions & 6 deletions __tests__/cp.spec.ts → __tests__/tarball.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path'
import zlib from 'zlib'
import fs from 'fs'
import fsp from 'fs/promises'
import zlib from 'zlib'
import { build } from 'vite'
import test from 'ava'
import tar from 'tar-stream'
import { readAll } from '../src/utils'
import type { ViteCompressionPluginConfig } from '../src'
import { compression, cp } from '../src'
import type { ViteCompressionPluginConfig, ViteTarballPluginOptions } from '../src'
import { compression, tarball } from '../src'

const getId = () => Math.random().toString(32).slice(2, 10)
const sleep = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay))
Expand Down Expand Up @@ -38,7 +38,7 @@ async function mockBuild<T extends Algorithm = never>(dir = 'public-assets-nest'
const id = getId()
await build({
root: path.join(__dirname, 'fixtures', dir),
plugins: [compression(options), cp({ dest: path.join(dest, id) })],
plugins: [compression(options), tarball({ dest: path.join(dest, id) })],
configFile: false,
logLevel: 'silent',
build: {
Expand All @@ -48,12 +48,27 @@ async function mockBuild<T extends Algorithm = never>(dir = 'public-assets-nest'
return id
}

async function mockBuildwithoutCompression(dir: string, id: string, options: ViteTarballPluginOptions = {}) {
const bundle = await build({
root: path.join(__dirname, 'fixtures', dir),
plugins: [tarball(options)],
configFile: false,
logLevel: 'silent',
build: {
outDir: path.join(dist, id)
}
})
return { id, bundle }
}

test.after(async () => {
await fsp.rm(dest, { recursive: true })
})

test('cp', async (t) => {
const ids = await Promise.all([mockBuild('dynamic', { deleteOriginalAssets: true, skipIfLargerOrEqual: false }), mockBuild('dynamic')])
test('tarball', async (t) => {
const ids = await Promise.all([
mockBuild('public-assets-nest', { deleteOriginalAssets: true, skipIfLargerOrEqual: false }),
mockBuild('public-assets-nest', { skipIfLargerOrEqual: false })])
await sleep(3000)
const [diff1, diff2] = await Promise.all(ids.map((id) => readAll(path.join(dist, id))))
const diff1Js = diff1.filter((v) => v.endsWith('.js.gz')).map((v) => zlib.unzipSync(fs.readFileSync(v)))
Expand All @@ -66,3 +81,27 @@ test('cp', async (t) => {
}
}
})

test('tarball without compression', async (t) => {
const { id, bundle } = await mockBuildwithoutCompression('normal', getId())
const outputs = await extract(path.join(dist, id + '.tar.gz'))
if (typeof bundle === 'object' && 'output' in bundle) {
for (const chunk of bundle.output) {
if (chunk.fileName in outputs) {
const act = Buffer.from(outputs[chunk.fileName])
if (chunk.type === 'asset') {
t.deepEqual(act, Buffer.from(chunk.source))
} else {
t.deepEqual(act, Buffer.from(chunk.code))
}
}
}
}
})

test('tarball specify output', async (t) => {
const id = getId()
await mockBuildwithoutCompression('public-assets-nest', id, { dest: path.join(dest, id) })
const outputs = await extract(path.join(dest, id + '.tar.gz'))
t.truthy(Object.keys(outputs).length > 0)
})
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import type {
ViteCompressionPluginConfig,
ViteCompressionPluginConfigAlgorithm,
ViteCompressionPluginConfigFunction,
ViteCpPluginOptions,
ViteTarballPluginOptions,
ViteWithoutCompressionPluginConfigFunction
} from './interface'

const VITE_INTERNAL_ANALYSIS_PLUGIN = 'vite:build-import-analysis'
const VITE_COMPRESSION_PLUGIN = 'vite-plugin-compression'
const VITE_COPY_PUBLIC_DIR = 'copyPublicDir'
const MAX_CONCURRENT = (() => {
const cpus = os.cpus() || { length: 1 }
Expand Down Expand Up @@ -86,21 +87,22 @@ async function handleStaticFiles(config: ResolvedConfig, callback: (file: string
}
}

function cp(opts: ViteCpPluginOptions = {}): Plugin {
function tarball(opts: ViteTarballPluginOptions = {}): Plugin {
const { dest: userDest } = opts
const statics: string[] = []
let outputs: string[] = []
const outputs: string[] = []
let dests: string[] = []
let root = process.cwd()
const tarball = createTarBall()
const queue = createConcurrentQueue(MAX_CONCURRENT)
return {
name: 'vite-plugin-cp',
name: 'vite-plugin-tarball',
enforce: 'post',
async configResolved(config) {
outputs.push(...handleOutputOption(config))
root = config.root
outputs = userDest ? [userDest] : outputs
tarball.setOptions({ dests: outputs, root })
dests = userDest ? [userDest] : outputs
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)
Expand Down Expand Up @@ -128,7 +130,7 @@ function cp(opts: ViteCpPluginOptions = {}): Plugin {
})
}
}
await queue.wait().catch(e => e)
await queue.wait()
await tarball.write()
}
}
Expand Down Expand Up @@ -195,7 +197,7 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(opts
}

return {
name: 'vite-plugin-compression',
name: VITE_COMPRESSION_PLUGIN,
apply: 'build',
enforce: 'post',
api: pluginContext,
Expand Down Expand Up @@ -244,10 +246,10 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(opts
}

compression.getPluginAPI = (plugins: readonly Plugin[]): CompressionPluginAPI | undefined =>
plugins.find(p => p.name === 'vite-plugin-compression')?.api
plugins.find(p => p.name === VITE_COMPRESSION_PLUGIN)?.api

export { compression, cp }
export { compression, tarball }

export default compression

export type { CompressionOptions, Algorithm, ViteCompressionPluginConfig, ViteCpPluginOptions } from './interface'
export type { CompressionOptions, Algorithm, ViteCompressionPluginConfig, ViteTarballPluginOptions } from './interface'
2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export type ViteCompressionPluginConfig<T, A extends Algorithm> =

export type GenerateBundle = HookHandler<Plugin['generateBundle']>

export interface ViteCpPluginOptions {
export interface ViteTarballPluginOptions {
dest?: string,
}

0 comments on commit a182ebc

Please sign in to comment.