diff --git a/.vscode/settings.json b/.vscode/settings.json index f6b0547..9e62bcd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "eslint.useFlatConfig": true, "editor.defaultFormatter": "dprint.dprint", + "dprint.path": "./node_modules/dprint/dprint", "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, diff --git a/package.json b/package.json index 0f6b10b..18a1614 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ }, "dependencies": { "@rollup/pluginutils": "^5.1.0", - "tar-mini": "^0.1.0" + "tar-mini": "^0.1.1" }, "resolutions": { "is-core-module": "npm:@nolyfill/is-core-module@^1", diff --git a/src/compress.ts b/src/compress.ts index 8ccdf6c..eecaa3e 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -65,13 +65,13 @@ export function createTarBall() { root: '' } - const setOptions = (tarballOPtions: TarballOptions) => Object.assign(options, tarballOPtions) - const add = (meta: TarballFileMeta) => { pack.add(stringToBytes(meta.content), { filename: meta.filename }) } - const write = () => { + const setup = async (tarballOPtions: TarballOptions) => { + Object.assign(options, tarballOPtions) + const promises = options.dests.map(dest => { const expected = slash(path.resolve(options.root, dest + '.tar')) const parent = slash(path.dirname(expected)) @@ -80,19 +80,19 @@ export function createTarBall() { } return new Promise((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 = { add, - write, - setOptions + setup, + done: () => pack.done() } return context diff --git a/src/index.ts b/src/index.ts index 17a5e73..8fcb4c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,7 +103,6 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { outputs.push(...handleOutputOption(config)) root = config.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. ctx = compression.getPluginAPI(config.plugins) @@ -114,6 +113,9 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { } const plugin = config.plugins.find(p => p.name === VITE_INTERNAL_ANALYSIS_PLUGIN) if (!plugin) throw new Error("[vite-plugin-tarball] can't be work in versions lower than vite at 2.0.0") + + // create dest dir + tarball.setup({ dests, root }) }, async writeBundle(_, bundles) { for (const fileName in bundles) { @@ -135,18 +137,22 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin { } } await queue.wait() - await tarball.write() + tarball.done() } } } function compression(): Plugin -function compression | AlgorithmFunction>( - opts: A extends Algorithm - ? Pretty> +function compression< + T extends UserCompressionOptions | undefined, + A extends Algorithm | AlgorithmFunction | AlgorithmFunction +>( + opts: A extends Algorithm ? Pretty> : ViteCompressionPluginConfigFunction> ): Plugin -function compression(opts: ViteCompressionPluginConfigFunction>): Plugin +function compression( + opts: ViteCompressionPluginConfigFunction> +): Plugin function compression(opts: ViteWithoutCompressionPluginConfigFunction): Plugin function compression( opts: ViteCompressionPluginConfig = {} @@ -207,7 +213,7 @@ function compression( let viteAnalyzerPlugin = null - const plugin = { + const plugin = { name: VITE_COMPRESSION_PLUGIN, apply: 'build', enforce: 'post', @@ -242,7 +248,9 @@ function compression( statics.push(file) }) viteAnalyzerPlugin = config.plugins.find(p => p.name === VITE_INTERNAL_ANALYSIS_PLUGIN) - if (!viteAnalyzerPlugin) throw new Error("[vite-plugin-compression] Can't be work in versions lower than vite at 2.0.0") + if (!viteAnalyzerPlugin) { + throw new Error("[vite-plugin-compression] Can't be work in versions lower than vite at 2.0.0") + } }, async closeBundle() { // parallel run @@ -292,4 +300,10 @@ export { compression, defineCompressionOption, tarball } export default compression -export type { Algorithm, CompressionOptions, ViteCompressionPluginConfig, ViteTarballPluginOptions, ViteCompressionPluginOption } from './interface' +export type { + Algorithm, + CompressionOptions, + ViteCompressionPluginConfig, + ViteCompressionPluginOption, + ViteTarballPluginOptions +} from './interface' diff --git a/yarn.lock b/yarn.lock index 385c36d..067c920 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5397,10 +5397,10 @@ __metadata: languageName: node linkType: hard -"tar-mini@npm:^0.1.0": - version: 0.1.0 - resolution: "tar-mini@npm:0.1.0" - checksum: 10/03957e8f573d303e93a5110bb3cd6e0abaccde7880f2690c2bb6f3e3341ef6887a8e1716e433a189ed8c455ee6d41fa133957a5cbba0c15dbeea14312789b654 +"tar-mini@npm:^0.1.1": + version: 0.1.1 + resolution: "tar-mini@npm:0.1.1" + checksum: 10/62f69990e6f6c0015653ec62ea271f5ed64e6ae6183ad17d5cfefc53ec5372a492c2c2096a9eac7af14c327ebe74cefd20e6a8a1bcdbbd30dd612273e33239d4 languageName: node linkType: hard @@ -5721,7 +5721,7 @@ __metadata: rollup-plugin-dts: "npm:^6.1.1" rollup-plugin-swc3: "npm:^0.11.2" sirv: "npm:^2.0.3" - tar-mini: "npm:^0.1.0" + tar-mini: "npm:^0.1.1" typescript: "npm:^5.3.3" vite: "npm:^5.3.4" vitest: "npm:^2.0.3"