Skip to content

Commit

Permalink
chore: release v1.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 14, 2024
1 parent b7d4f6d commit 780a9da
Show file tree
Hide file tree
Showing 7 changed files with 1,171 additions and 1,734 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.4

- Migrate to `tar-mini`

## 1.1.3

# Improves
Expand Down
16 changes: 5 additions & 11 deletions __tests__/tarball.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs'
import fsp from 'fs/promises'
import { build } from 'vite'
import { afterAll, expect, test } from 'vitest'
import tar from 'tar-stream'
import { createExtract } from 'tar-mini'
import { readAll } from '../src/shared'
import type { ViteCompressionPluginConfig, ViteTarballPluginOptions } from '../src'
import { compression, tarball } from '../src'
Expand All @@ -16,21 +16,15 @@ const dist = path.join(__dirname, 'dist')
const dest = path.join(__dirname, '.dist')

function extract(p: string): Promise<Record<string, Buffer>> {
const extract = createExtract()
fs.createReadStream(p).pipe(extract.receiver)
return new Promise((resolve, reject) => {
const extract = tar.extract()
const files: Record<string, Buffer> = {}
extract.on('entry', (header, stream, next) => {
let file = Buffer.alloc(0)
stream.on('data', c => file = Buffer.concat([file, c]))
stream.on('end', () => {
files[header.name] = file
next()
})
stream.resume()
extract.on('entry', (head, file) => {
files[head.name] = Buffer.from(file)
})
extract.on('finish', () => resolve(files))
extract.on('error', reject)
fs.createReadStream(p).pipe(extract)
})
}

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-compression2",
"packageManager": "[email protected]",
"version": "1.1.3",
"version": "1.1.4",
"description": "a fast vite compression plugin",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -48,7 +48,6 @@
"devDependencies": {
"@swc/core": "^1.6.6",
"@types/node": "^20.14.9",
"@types/tar-stream": "^3.1.3",
"@vitest/coverage-v8": "^2.0.3",
"dprint": "^0.46.3",
"eslint": "^8.57.0",
Expand All @@ -58,13 +57,13 @@
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-swc3": "^0.11.2",
"sirv": "^2.0.3",
"tar-stream": "^3.1.7",
"typescript": "^5.3.3",
"vite": "^5.3.4",
"vitest": "^2.0.3"
},
"dependencies": {
"@rollup/pluginutils": "^5.1.0"
"@rollup/pluginutils": "^5.1.0",
"tar-mini": "^0.1.0"
},
"resolutions": {
"is-core-module": "npm:@nolyfill/is-core-module@^1",
Expand Down
27 changes: 17 additions & 10 deletions src/compress.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import zlib from 'zlib'
import util from 'util'
import fsp from 'fs/promises'
import path from 'path'
import fs from 'fs'
import type { BrotliOptions, InputType, ZlibOptions } from 'zlib'
import { Pack } from './tar'
import { createPack } from 'tar-mini'

import type { Algorithm, AlgorithmFunction, UserCompressionOptions } from './interface'
import { slash, stringToBytes } from './shared'

Expand Down Expand Up @@ -57,7 +58,7 @@ interface TarballFileMeta {
}

export function createTarBall() {
const pack = new Pack()
const pack = createPack()

const options: TarballOptions = {
dests: [],
Expand All @@ -67,19 +68,25 @@ export function createTarBall() {
const setOptions = (tarballOPtions: TarballOptions) => Object.assign(options, tarballOPtions)

const add = (meta: TarballFileMeta) => {
pack.add({ filename: meta.filename, content: stringToBytes(meta.content) })
pack.add(stringToBytes(meta.content), { filename: meta.filename })
}

const write = async () => {
const archive = pack.write()
await Promise.all(options.dests.map(async (dest) => {
const write = () => {
const promises = options.dests.map(dest => {
const expected = slash(path.resolve(options.root, dest + '.tar'))
const parent = slash(path.dirname(expected))
if (options.root !== parent) {
await fsp.mkdir(parent, { recursive: true })
fs.mkdirSync(parent, { recursive: true })
}
await fsp.writeFile(expected, archive)
}))
return new Promise<void>((resolve, reject) => {
const w = fs.createWriteStream(expected)
w.on('error', reject)
w.on('finish', resolve)
pack.receiver.pipe(w)
})
})

return Promise.all(promises)
}

const context = {
Expand Down
5 changes: 3 additions & 2 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fsp from 'fs/promises'
import path from 'path'
import { u8 } from './tar'

export function len<T extends ArrayLike<unknown>>(source: T) {
return source.length
Expand Down Expand Up @@ -41,6 +40,8 @@ export async function readAll(entry: string) {
return result
}

const encoder = new TextEncoder()

export function stringToBytes(b: string | Uint8Array) {
return typeof b === 'string' ? u8.encode(b) : b
return typeof b === 'string' ? encoder.encode(b) : b
}
Loading

0 comments on commit 780a9da

Please sign in to comment.