webpack-contrib/compression-webpack-plugin

[types] Failed to import new types

vitoyucepi opened this issue · 3 comments

Bug report

Since 0acff90 compression-webpack-plugin provides it's own types and @types/compression-webpack-plugin has marked as stub.
But it's unable to simply migrate from one to another because types can't be imported in the same way.

Actual Behavior

With compression-webpack-plugin@9.1.1 types can't be imported

import {Rule} from 'compression-webpack-plugin'

because

index.ts:1:9 - error TS2305: Module '"compression-webpack-plugin"' has no exported member 'Rule'.

Expected Behavior

Using @types/compression-webpack-plugin@9.0.0 and compression-webpack-plugin@9.1.0 it's possible to write

import {Rule} from 'compression-webpack-plugin'

How Do We Reproduce?

  1. yarn init, use defaults
  2. yarn add typescript
  3. yarn tsc --init, use defaults
  4. yarn add webpack
  5. yarn add compression-webpack-plugin
  6. Create index.ts
    import {Rule} from 'compression-webpack-plugin';
    
    const a: Rule = 'a';
    console.log(a)
  7. yarn tsc

Please paste the results of npx webpack-cli info here, and mention other relevant information

Not related

Yes, I see, we don't write types manually, they are generated by typescript, and plugin in common js format, so you need to use if you want to import plugin and types:

import CompressionPlugin = require("compression-webpack-plugin");
import type { Rule } from "compression-webpack-plugin/types";
// import { type Rule } from "compression-webpack-plugin/types";

const a: Rule = /test/;

console.log(CompressionPlugin)
console.log(a)

Just done some more testing.

-  "types": "types/cjs.d.ts"
+  "types": "types/index.d.ts"

With default typescript project config this works fine.
Here's my script

import CompressionPlugin, {Rule} from 'compression-webpack-plugin';

const a: Rule = 'a';
const b = new CompressionPlugin();
console.log(a)
console.log(b)

Everything else remains the same.

tsconfig.json option esModuleInterop can be set to true for compatibility with the older node versions.

@alexander-akait could you please share a test case that will not work without reexport to cjs.
Is it related to linking and type generation with other projects of yours?

P.S.
I changed the last step in original issue

- yarn tsc index.ts
+ yarn tsc

Yep, we have a problem, we are in cjs, but exports esm types, due babel and traspiling, we should fix it, so I keep it open