Skip to content

Commit

Permalink
fix: change emit compressed file and delete original assets order (#30)
Browse files Browse the repository at this point in the history
* fix: change emit compressed file and delete original assets order

* fix: `closeBundle` file processing

---------

Co-authored-by: kanno <812137533@qq.com>
  • Loading branch information
jglee96 and nonzzz authored Jun 5, 2023
1 parent d8aaf94 commit df2e22d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions __tests__/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,15 @@ test('exclude-assets', async (t) => {
const compressed = len(r.filter((s) => s.endsWith('.gz')))
t.is(compressed, 2)
})

test('aws s3', async (t) => {
const id = await mockBuild({ filename: '[path][base]', deleteOriginalAssets: true }, 'dynamic')
await sleep(3000)
const r = await readAll(path.join(dist, id))
const compressed = len(r.filter((s) => s.endsWith('.gz')))
t.is(compressed, 0)
// eslint-disable-next-line padded-blocks
const css = r.filter(v => v.endsWith('.css'))[0]
const bf = zlib.unzipSync(fs.readFileSync(css))
t.is(bf.toString(), '.pr{padding-right:30px}.pl{padding-left:30px}.mt{margin-top:30px}\n')
})
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ function compression<T, A extends Algorithm>(opts: ViteCompressionPluginConfig<T
const bundle = bundles[file]
const source = bundle.type === 'asset' ? bundle.source : bundle.code
const compressed = await transfer(Buffer.from(source), zlib.algorithm, zlib.options)
// #issue 30
if (deleteOriginalAssets) Reflect.deleteProperty(bundles, file)
const fileName = replaceFileName(file, zlib.filename)
this.emitFile({ type: 'asset', source: compressed, fileName })
if (deleteOriginalAssets) Reflect.deleteProperty(bundles, file)
stores.delete(file)
}
try {
Expand All @@ -180,8 +181,10 @@ function compression<T, A extends Algorithm>(opts: ViteCompressionPluginConfig<T
const buf = await fsp.readFile(f)
const compressed = await transfer(buf, zlib.algorithm, zlib.options)
const fileName = replaceFileName(file, zlib.filename)
await fsp.writeFile(path.join(dest, fileName), compressed)
if (deleteOriginalAssets) await fsp.rm(f, { recursive: true, force: true })
// issue #30
const outputPath = path.join(dest, fileName)
if (deleteOriginalAssets && outputPath !== f) await fsp.rm(f, { recursive: true, force: true })
await fsp.writeFile(outputPath, compressed)
}
}

Expand Down

0 comments on commit df2e22d

Please # to comment.