Closed silverwind closed 1 month 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.
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.
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.
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.
@mengdaoshizhongxinyang How do you think it.
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.
defineCompressionConfig
ViteCompressionPluginConfigFunction
and others etc.
// can using generic for This function. (If user define the custom algorithm)
const options = defineCompressionConfig({
// ...
})
If expose a new define function can be resolve this scene, I'll create a PR for it.
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",
~~~~~~~~~
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.
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.
We exported a new generic ViteCompressionPluginOption
Case
import type { ViteCompressionPluginOption, Algorithm } from 'vite-plugin-compression2'
const opt: ViteCompressionPluginOption<Algorithm> = {
// ...
}
I had some issues typing a object that holds the options for this plugin:
I think the correct type for
obj
would beViteWithoutCompressionPluginConfigFunction
but this module currently does not export it.Can it be exported? Possibly under a more memorable name?