Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use typescript with css modules #616

Closed
ghost opened this issue Jan 22, 2018 · 12 comments
Closed

Can't use typescript with css modules #616

ghost opened this issue Jan 22, 2018 · 12 comments

Comments

@ghost
Copy link

ghost commented Jan 22, 2018

Hi, I'm bootstrapping a simple project with react and typescript and I can't seem to import css modules correctly.

This works:
import './styles.css'

This doesn't
import * as classes from './styles.css'

Am I missing something?

screen shot 2018-01-22 at 13 27 55

@wereHamster
Copy link

You probably have to provide a declaration file which specifies what you get when you import a css file. This would be a start:

declare module '*.css' {
  const css: any
  export default css
}

@fathyb
Copy link
Contributor

fathyb commented Jan 23, 2018

Use declare module '*.css' on an ambient .d.ts file on your project to let TypeScript know you are using CSS modules.

Adding { const css: any; export default css } will restrict you from importing named classes (eg. import {myClass} from './index.css').

@mightyiam
Copy link

Are css modules in TypeScript working for anyone in v1.6.2? Perhaps from node_modules it doesn't work? I'm getting an empty object and the CSS classes are not transformed into hashes.

@fathyb
Copy link
Contributor

fathyb commented Mar 3, 2018

@mightyiam CSS modules are working for me with TypeScript and Parcel 1.6.2.
Check if you have "modules": true in your PostCSS configuration. If it still doesn't work could you create an issue with a minimal reproduction?

I'm closing this issue as OP's problem is resolved with the correct module definition.

@fathyb fathyb closed this as completed Mar 3, 2018
@pietmichal
Copy link

@mightyiam I have this issue at the moment.

@mightyiam
Copy link

Thanks. I didn't know I needed a PostCSS configuration... I've added a postcss.config.js with

module.exports = {
  modules: true
}

And now the CSS file does get imported but not transpiled, still. And the object is still empty. So, seemingly same result.

@mightyiam
Copy link

#70 suggests disabling hot module reload. So I used --no-hmr and same result seemingly.

@wangcch
Copy link

wangcch commented Apr 5, 2019

I am experiencing the same problem. But this seems to be typescript type error.

I use postcss, is working

// .postcssrc
"modules": true,

But the type error did not find a good solution.

  1. use require
  2. declare module
  3. generate *.scss.d.ts for *.scss

Maybe can give up😔. To use emotion

@Place1
Copy link

Place1 commented Apr 7, 2019

I wrote a plugin to generate .d.ts files called parcel-plugin-typed-css-modules. Maybe this can solve your problem. I've kinda neglected it so there are some known bugs but it might get the job done for you

@ngduc
Copy link

ngduc commented Aug 23, 2019

Thanks @Place1 Great work!
I'm using your plugin in my parcelui boilerplate (Parcel + Typescript + React + CSS Modules + SASS) here:
https://github.com/ngduc/parcelui

@dmitrijs-balcers
Copy link

This will work for parcel v2: https://www.npmjs.com/package/parcel-transformer-ts-css-modules

@8bitjoey
Copy link

I personally had problems with the above mentioned parcel-transformer-ts-css-modules. Maybe I didn't use postcss correctly, but in my case all classes with the same name were merged into one in the dist/main.css.

Also, I personally dislike that every *.module.scss file gets a *.d.ts twin. Instead I did the following:

  1. created src/module.d.ts file with the following content
declare module "*.module.scss"; // or *.module.css if you use that
  1. updated src/index.ts which is used as an entry point in my project to start with
/// <reference types="./module.d.ts" />
... rest of the file ...

Now it compiles without any warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants