elchininet / postcss-rtlcss

PostCSS plugin to automatically build Cascading Style Sheets (CSS) with Left-To-Right (LTR) and Right-To-Left (RTL) rules using RTLCSS
https://elchininet.github.io/postcss-rtlcss/
Apache License 2.0
102 stars 16 forks source link

Types don't work with allowJs: true and require() #128

Closed catrope closed 2 years ago

catrope commented 2 years ago

If I enable "allowJs": true and "checkJs": true in my tsconfig.json, TypeScript will do type checking in JavaScript files like postcss.confg.js. However, postcss-rtlcss's type definitions break in this case:

const postcssRtlcss = require( 'postcss-rtlcss' );
const autoprefixer = require( 'autoprefixer' );

module.exports = {
    plugins: [
        postcssRtlcss(),
        autoprefixer()
    ]
};

This results in the following TypeScript error:

postcss.config.js:12:3 - error TS2349: This expression is not callable.
  Type 'typeof import("/home/catrope/[...]/node_modules/postcss-rtlcss/index")' has no call signatures.

12   postcssRtlcss()
     ~~~~~~~~~~~~~

If I use require( 'postcss-rtlcss').default, then TypeScript is happy, but of course I get a runtime error because that property doesn't exist in reality.

For autoprefixer everything does work correctly. It looks like the critical difference is that postcss-rtlcss uses export default blah; but autoprefixer uses export = blah;.

elchininet commented 2 years ago

Hi @catrope, This is due to how the types are exported in the d.ts files at the moment. I‘m refactoring the package to provide a commonjs and a esm version of it. When this is ready, you will not have this issue anymore, for now you can juts ignore the line with @ts-ignore. I‘ll let you know when it is ready, I‘ll let this issue open. Regards

elchininet commented 2 years ago

@catrope, about this part:

For autoprefixer everything does work correctly. It looks like the critical difference is that postcss-rtlcss uses export default blah; but autoprefixer uses export = blah;.

I think that you are looking at the type definition file of autoprefixer instead of the real module.

elchininet commented 2 years ago

Hi @catrope, I‘ve released a new version of the package. Now, two bundles are generated using Rollup, one for esm and one for CommonJs, the type definitions should be fixed for CommonJs in this version. Please, check the version 3.5.4 and let me know if your issue gets solved. Regards.

elchininet commented 2 years ago

Hi @catrope, It seems that the issue is already resolved. Closing this issue. Regards

catrope commented 2 years ago

Yes, this new version works great, thanks! Sorry for forgetting to leave a comment here on Friday

elchininet commented 2 years ago

@catrope, no worries. Glad to hear that the issue is solved. Regards