Skip to content

Comments

Fix bugs for resolver and checker#296

Merged
mizdra merged 3 commits intomainfrom
refactor-resolver
Jan 10, 2026
Merged

Fix bugs for resolver and checker#296
mizdra merged 3 commits intomainfrom
refactor-resolver

Conversation

@mizdra
Copy link
Owner

@mizdra mizdra commented Jan 10, 2026

replace: #292

Background

Currently, css-modules-kit reports an error Cannot import module 'xxx' when code tries to import a non-existent CSS Module file. This helps users catch mistakes early.

However, in certain cases, this error is not reported. Specifically, when importing a non-existent CSS Module file using an import alias.

src/a.module.css:

@import '@/src/non-existent.module.css';
@import './non-existent.module.css';

tsconfig.json:

{
  "compilerOptions": {
    "paths": { "@/*": ["./*"] }
  },
  "cmkOptions": {
    "enabled": true
  }
}
image

This happens because import specifiers are resolved differently for @/src/non-existent.module.css and ./non-existent.module.css. The former uses ts.resolveModuleName, while the latter uses resolve(fileURLToPath(new URL(specifier, pathToFileURL(options.request)).href)). The latter can resolve the specifier even if the file does not exist, while the former cannot resolve non-existent files.

Moreover, css-modules-kit does not report the error for specifiers that cannot be resolved. Ideally, it should report an error in such cases. However, users often write code like @import 'https://example.com/remote.css'; to import CSS files that are resolved at runtime. To avoid reporting errors for these cases, css-modules-kit is designed not to report errors for specifiers that cannot be resolved.

This behavior of not reporting errors for unresolved specifiers also applies when using import aliases. As a result, import specifiers like @/src/non-existent.module.css do not trigger errors even when they cannot be resolved.

Solution

Modify the resolver to always resolve specifiers using ts.resolveModuleName. Additionally, enable reporting of Cannot import module 'xxx' diagnostics even when resolving with ts.resolveModuleName.

Note

This pull request introduced a regression, but it was fixed in #298.

@changeset-bot
Copy link

changeset-bot bot commented Jan 10, 2026

🦋 Changeset detected

Latest commit: 3d5667a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@css-modules-kit/core Minor
@css-modules-kit/codegen Patch
@css-modules-kit/ts-plugin Patch
@css-modules-kit/stylelint-plugin Patch
@css-modules-kit/eslint-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mizdra mizdra force-pushed the refactor-resolver branch from cd02e10 to 3d5667a Compare January 10, 2026 10:19
@mizdra mizdra added Type: Bug Bug or Bug fixes Type: Breaking Change Includes breaking changes and removed Type: Other labels Jan 10, 2026
@mizdra mizdra marked this pull request as ready for review January 10, 2026 10:26
@mizdra mizdra merged commit b5cdd4a into main Jan 10, 2026
18 checks passed
@mizdra mizdra deleted the refactor-resolver branch January 10, 2026 10:27
@github-actions github-actions bot mentioned this pull request Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Breaking Change Includes breaking changes Type: Bug Bug or Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant