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

Toolchain: Warn about undefined CSS Module classes in TypeScript #4387

Merged
merged 3 commits into from Jun 28, 2023

Conversation

p2edwards
Copy link
Contributor

Description

Show a warning in your code editor if you use an undefined CSS module class in a TypeScript file.

Details

To help catch issues like the wrong class name being used.

One caveat: It's a TypeScript Language Server plugin, so it'll work in editors but not withtsc, webpack, or anything we run in CI.

Notes for reviewer

There used to be an ESLint plugin that did this, but it's currently unmaintained, and it didn't work when I tried it.

There's some talk in the typescript-plugin-css-modules repo of adding an ESLint plugin, so if that happens we could perhaps use it.

To test this PR, npm install, then open the kpi.code-workspace in VS Code. See what happens when you modify something like className={styles.root} in a TypeScript file.

Screen Shot 2023-03-24 at 1 19 11 PM

Further config might be needed to get this functionality in other editors (Sublime, coc.nvim).


Changes in this PR:

  • node_modules - Install typescript-plugin-css-modules - the TypeScript server plugin
  • tsconfig.json - Set typescript-plugin-css-modules as a plugin
  • kpi.code-workspace: Configure VS Code to use the TypeScript version in node_modules, which will use the plugin in our tsconfig.json.
  • While we're here, updated our node_modules TypeScript to 5.0.2. This wasn't strictly necessary, but since TypeScript doesn't follow semver, 5.0.0 wasn't a major breaking change, and I figured it's a good time to update.

Install `typescript-plugin-css-modules` as a TypeScript Language Server
plugin.

This won't run in our build or `npm run check-types`, but it will show
alongside other TypeScript feedback when you're editing files in
supported editors.

More info here: npmjs.com/package/typescript-plugin-css-modules

Other changes:

- Upgrade TypeScript to 5.0.2
  - TypeScript doesn't use semver, so 5.0.0 wasn't a big breaking change

- (vscode - kpi.workspace) Configure TypeScript SDK to use our
  TypeScript from npm (instead of the built-in VS Code version)
  - This allows us to configure plugins in tsconfig.json

Notes:

- Q: Why a TypeScript plugin, not an ESLint plugin?
- A: eslint-plugin-css-modules didn't work when I tried it, and is
     no longer maintained.
- Q: Why won't it run in our build, or `npm run check-types`? 
- A: At this time, TypeScript doesn't support plugins during compilation
     https://github.com/mrmckeb/typescript-plugin-css-modules
@p2edwards
Copy link
Contributor Author

According to Leszek, this works in Sublime if you have these plugins:

  • LSP
  • LSP-typescript
  • LSP-css

@magicznyleszek magicznyleszek merged commit 4ca5d3b into beta Jun 28, 2023
3 checks passed
@magicznyleszek magicznyleszek deleted the ts-plugin-css-modules branch June 28, 2023 15:13
@p2edwards
Copy link
Contributor Author

@magicznyleszek Thanks for merging!

@magicznyleszek magicznyleszek restored the ts-plugin-css-modules branch July 4, 2023 15:20
magicznyleszek added a commit that referenced this pull request Jul 4, 2023
The changes were causing issues with swc-loader
@p2edwards p2edwards mentioned this pull request Jul 5, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants