Merged
Conversation
🦋 Changeset detectedLatest commit: 83b2c03 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
Cannot import module 'xxx' error
88d45b7 to
fc3aa6a
Compare
fc3aa6a to
83b2c03
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:tsconfig.json:{ "compilerOptions": { "paths": { "@/*": ["./*"] } }, "cmkOptions": { "enabled": true } }This happens because import specifiers are resolved differently for
@/src/non-existent.module.cssand./non-existent.module.css. The former usests.resolveModuleName, while the latter usesresolve(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.cssdo not trigger errors even when they cannot be resolved.Solution
The
Cannot import module 'xxx'error is useful for users, but it conflicts with the behavior of not reporting errors for unresolved specifiers. Additionally, this error is typically detected by the bundler. Even if css-modules-kit does not report it, users will likely notice the mistakes. Therefore, we will remove theCannot import module 'xxx'error entirely.With this change, it will also be possible to unify the import specifier resolution method to
ts.resolveModuleNamein the future.