Skip to content

Commit

Permalink
chore: house keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Oct 8, 2024
1 parent 5a25033 commit 02babc3
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 93 deletions.
8 changes: 1 addition & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.formatOnSave": true,
"[json]": {
"editor.defaultFormatter": "dprint.dprint"
},
"[markdown]": {
"editor.defaultFormatter": "dprint.dprint"
}
"editor.formatOnSave": true
}
21 changes: 21 additions & 0 deletions __tests__/tarball.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,24 @@ test('tarball gz with compression', async () => {
}
}
})

test('tarball gz with compression and skipIfLargerOrEqual', async () => {
const id = await mockBuild('public-assets-nest', { deleteOriginalAssets: true, skipIfLargerOrEqual: true }, true)
await sleep(3000)
const { bundle } = await mockBuildwithoutCompression('public-assets-nest', getId())
await sleep(3000)
const outputs = extract(path.join(dest, id + '.tar.gz'), true)

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') {
expect(act).toStrictEqual(Buffer.from(chunk.source))
} else {
expect(act).toStrictEqual(Buffer.from(chunk.code))
}
}
}
}
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}
},
"devDependencies": {
"@swc/core": "^1.6.6",
"@swc/core": "^1.7.26",
"@types/node": "^20.14.9",
"@vitest/coverage-v8": "^2.0.3",
"dprint": "^0.46.3",
Expand All @@ -55,7 +55,7 @@
"playwright": "^1.32.3",
"rollup": "^4.18.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-swc3": "^0.11.2",
"rollup-plugin-swc3": "^0.12.1",
"sirv": "^2.0.3",
"typescript": "^5.3.3",
"vite": "^5.3.4",
Expand Down
56 changes: 35 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,30 +253,44 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(
}
},
async closeBundle() {
const compressAndHandleFile = async (filePath: string, file: string, dest: string) => {
const buf = await fsp.readFile(filePath)
const compressed = await compress(buf, zlib.algorithm, zlib.options)
if (skipIfLargerOrEqual && len(compressed) >= len(buf)) {
if (!pluginContext.staticOutputs.has(filePath)) pluginContext.staticOutputs.add(filePath)
return
}

const fileName = replaceFileName(file, zlib.filename)
if (!pluginContext.staticOutputs.has(fileName)) pluginContext.staticOutputs.add(fileName)

const outputPath = path.join(dest, fileName)
if (deleteOriginalAssets && outputPath !== filePath) {
await fsp.rm(filePath, { recursive: true, force: true })
}
await fsp.writeFile(outputPath, compressed)
}

const processFile = async (dest: string, file: string) => {
const filePath = path.join(dest, file)
if (!filter(filePath) && !pluginContext.staticOutputs.has(file)) {
pluginContext.staticOutputs.add(file)
return
}
const { size } = await fsp.stat(filePath)
if (size < threshold) {
if (!pluginContext.staticOutputs.has(file)) {
pluginContext.staticOutputs.add(file)
}
return
}
await compressAndHandleFile(filePath, file, dest)
}

// parallel run
for (const dest of outputs) {
for (const file of statics) {
queue.enqueue(async () => {
const p = path.join(dest, file)
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)
}
}
})
queue.enqueue(() => processFile(dest, file))
}
}
// issue #18
Expand Down
Loading

0 comments on commit 02babc3

Please sign in to comment.