question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion: Support typed css-modules with Typescript

See original GitHub issue

Hi, I am working on typed Vue css-modules in Typescript support for Nuxt.js. I saw the issue: https://github.com/nuxt/typescript/issues/35, actually I found this template thanks to this (I am very grateful for this template by the way, this is what I needed but not deserved):]

Yesterday I have had some successes with a custom fork of a css-modules-typescript-loader package:

  1. I extended webpack configuration in nuxt.config.ts build.extend method:
function enableCssModulesPlugin (config): void {
  if (!(config.module && config.module.rules)) { return; }
  for (const rule of config.module.rules) {
    if (!rule.oneOf) { continue; }
    const useLoaders = rule.oneOf.map(e => e.use);
    for (const loaders of useLoaders) {
      const loadersCopy = loaders.map(e => ({ ...e }));
      loadersCopy.map(({ loader }, index) => {
        if (loader === 'css-loader') {
          loaders.splice(index, 0, '@alza54/css-modules-typescript-loader');
        }
        return {};
      });
    }
  }
}
// ...
// enableCssModulesPlugin(config);
  1. I modified Vue.$style augmentation by changing type Readonly<Record<string, string>> to any
  2. In all components I have imported definition file (which exports generated interface of class names)
  3. In all components I have created style computed getter like this:
get style (): cssModules {
  return this.$style;
}

and it kinda worked.

But…

It doesn’t make a sense, because types are available after building Webpack modules. Useless.

What will happen when I rename .reload or .actions classes? What if I have a typo there?

Considering that, I suggest trying new approach: extending Typescript configuration instead. I found that typescript-plugin-css-modules seems to work remarkably good for me:

// test.module.scss
// camelCase for simplicity
$mistyrose: #ffe4e1;

.mistyrose {
  &Background {
    background: $mistyrose;
  }
  &Color {
    color: $mistyrose;
  }
  &Border {
    border: 1px solid $mistyrose;
  }
}

$counts: 'One', 'Two', 'Three';
@each $count in $counts {
  .element#{$count} {
    background: $mistyrose;
  }
}

B9TW3gHlCg

This plugin transforms Sass/SCSS to CSS using sass.renderSync method; I assume that .vue files support could be achieved with a customRenderer function extracting CSS from Vue single file components.

I am just not assured of how to assign exported classes to the $style property yet, though.

I believe this is a proper way of implementing typed Vue CSS modules, unlike extending webpack configuration. I would love to hear your opinion about this @sobolevn 😄

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
sobolevncommented, Oct 24, 2019

Let’s keep it open. Because, this feature is so cool to have!

0reactions
StringKecommented, Aug 25, 2020

how to support module attr in style tag

<style module lang="scss">
.default-header {
  position: relative;
  height: 65px;
  border-bottom: 1px solid $gray-400;
}
</style>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use CSS Modules with TypeScript and webpack
Using CSS Modules with TypeScript is not as obvious as with JavaScript. There are several ways to do that.
Read more >
Generating TypeScript definitions for CSS Modules using SASS
The typed-scss-modules package is heavily inspired by typed-css-modules . It's a CLI that generates type definitions focused on supporting CSS ...
Read more >
What's the best way to get typescript support for css module ...
We found a few projects that can do this, one is a typescript plugin that doesn't support compile time errors, but does support...
Read more >
Create-react-app + TypeScript + CSS Modules - Stack Overflow
You can manually create ComponentName.module.css.d.ts files with type definitions like this: export const identifierName: string . This allows ...
Read more >
roc-plugin-typed-css-modules - npm package - Snyk
A roc plugin to enable the typed-css-modules, which generates typescript definition files for your css files. Visit Snyk Advisor to see a full ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found