webpack-contrib/image-minimizer-webpack-plugin

Add TypeScript definitions

glen-84 opened this issue · 5 comments

I'm using imagemin-webpack in my webpack configuration, and it would be nice to have type definitions for this loader.

This is fairly easy to add:

  1. Run npm install --save-dev typescript.
  2. Create a tsconfig.json file like:
    {
      "compilerOptions": {
        "target": "ES5",
        "outDir": "types",
        "newLine": "LF",
        "allowJs": true,
        "declaration": true,
        "emitDeclarationOnly": true
      },
      "include": [
        "index.js",
        "src/**/*"
      ],
      "compileOnSave": false
    }
  3. Add "types": "types/index.d.ts" to package.json.
  4. Run npx tsc to generate the declaration files.

(optional: Add an npm script and add some JSDoc comments)

Yep, good idea, it is on roadmap for all loaders/plugins, feel free to send a PR

Sorry to ask on this closed issue but how can we access the different types?

From types/cjs.d.ts, it seems only the default ImageMinimizerPlugin is re-exported.
I would like to use the PluginOptions type for instance (to reference it in a shared configuration)

So far the workaround I found is:

import ImageMinimizerPlugin from 'image-minimizer-webpack-plugin';
import type { PluginOptions as ImageMinimizerPluginOptions } from 'image-minimizer-webpack-plugin/types/index';

But is there a way with a single import?

PluginOptions is any, but yes we should improve this, we prepare new major release, I will try to improve this

Hi @alexander-akait, thanks for the quick answer :)

I was thinking about this interface: https://github.com/webpack-contrib/image-minimizer-webpack-plugin/blob/master/types/index.d.ts#L6

It is pretty good I think, but I'd rather be able to import it from the package:

import ImageMinimizerPlugin, { PluginOptions as ImageMinimizerPluginOptions } from 'image-minimizer-webpack-plugin';

You can get them from ImageMinimizerPlugin, i.e. ImageMinimizerPlugin['options'], types some wrong right now