nonzzz / vite-plugin-compression

vite plugin. compress your bundle file.
MIT License
162 stars 9 forks source link

Export type `ViteWithoutCompressionPluginConfigFunction`? #58

Closed silverwind closed 1 month ago

silverwind commented 2 months ago

I had some issues typing a object that holds the options for this plugin:

const obj = {}; // which type?
const plugins = [compression(obj)];

I think the correct type for obj would be ViteWithoutCompressionPluginConfigFunction but this module currently does not export it.

Can it be exported? Possibly under a more memorable name?

nonzzz commented 2 months ago

Is there any scene about it? FWIW, Currently the compress method is defined with overloading. Therefore. this scene wasn't consideration when it was designed.

silverwind commented 2 months ago

My situation is that host all my plugin configs in a shared library, so there I no way I can call the plugin directly which is depended on in another repo.

nonzzz commented 2 months ago

Got it. So we should expose ViteCompressionPluginConfigFunction and ViteWithoutCompressionPluginConfigFunction and others etc? In past, I don't consider shared config. It's sounds well, PR welcome.

silverwind commented 2 months ago

I would suggest exporting just the object argument under a short memorable name like ViteCompressionPluginConfig. I think overloads will need to be changed to a exportable type alias because overloads can not be exported and are generally a pain to work with, imho.

nonzzz commented 2 months ago

@mengdaoshizhongxinyang How do you think it.

nonzzz commented 2 months ago

AFAIK, If we expose a new function like defineCompressionConfig for shared config. I find if we rewrite us overloads function is a hard work. Currently, the overload functions is design for the better DX when user define the option in compression function.

Change


// can using generic for This function. (If user define the custom algorithm)
const options = defineCompressionConfig({
   // ...
})
nonzzz commented 2 months ago

If expose a new define function can be resolve this scene, I'll create a PR for it.

silverwind commented 2 months ago

FYI, I also had been experimenting with creating this type and had limited success with:

type ViteCompressionPluginConfig = Parameters<typeof compression>[0];

This works because Parameters uses the last overload. There is at least one problem with the type though that it does not accept string for algorithm for some reason:

index.ts:136:5 - error TS2322: Type 'string' is not assignable to type '(buf: InputType, options: undefined) => Promise<Buffer>'.

136     algorithm: "gzip",
        ~~~~~~~~~
nonzzz commented 2 months ago

In fact, Currently the overload declaration have some bug for it. A fix is wait for test. The overload will be retained, And we exported a declaration function and an optimized type.

silverwind commented 2 months ago

Regarding this defineCompressionConfig function: I guess it is one way to solve it. I would have preferred for my library to not have a runtime dependency on the plugin though because that library itself is built with vite and I want to keep that bundle size minimal.

nonzzz commented 1 month ago

We exported a new generic ViteCompressionPluginOption

nonzzz commented 1 month ago

Case


import type { ViteCompressionPluginOption, Algorithm } from 'vite-plugin-compression2'

const opt: ViteCompressionPluginOption<Algorithm> = { 
 // ...
}