diff --git a/.changeset/old-waves-start.md b/.changeset/old-waves-start.md new file mode 100644 index 00000000..a16a8c6f --- /dev/null +++ b/.changeset/old-waves-start.md @@ -0,0 +1,5 @@ +--- +'@css-modules-kit/core': patch +--- + +refactor: refactor resolver diff --git a/packages/core/src/resolver.ts b/packages/core/src/resolver.ts index 1bb88e04..d4aa9f4f 100644 --- a/packages/core/src/resolver.ts +++ b/packages/core/src/resolver.ts @@ -1,16 +1,13 @@ -import { fileURLToPath, pathToFileURL } from 'node:url'; import type { CompilerOptions } from 'typescript'; import ts from 'typescript'; -import { isAbsolute, resolve } from './path.js'; +import { resolve } from './path.js'; import type { Resolver, ResolverOptions } from './type.js'; export function createResolver( compilerOptions: CompilerOptions, moduleResolutionCache: ts.ModuleResolutionCache | undefined, ): Resolver { - return (_specifier: string, options: ResolverOptions) => { - let specifier = _specifier; - + return (specifier: string, options: ResolverOptions) => { const host: ts.ModuleResolutionHost = { ...ts.sys, fileExists: (fileName) => { @@ -29,27 +26,8 @@ export function createResolver( ); if (resolvedModule) { // TODO: Logging that the paths is used. - specifier = resolvedModule.resolvedFileName.replace(/\.module\.d\.css\.ts$/u, '.module.css'); - } - if (isAbsolute(specifier)) { - return resolve(specifier); - } else if (isRelativeSpecifier(specifier)) { - // Convert the specifier to an absolute path - // NOTE: Node.js resolves relative specifier with standard relative URL resolution semantics. So we will follow that here as well. - // ref: https://nodejs.org/docs/latest-v23.x/api/esm.html#terminology - return resolve(fileURLToPath(new URL(specifier, pathToFileURL(options.request)).href)); - } else { - // Do not support URL or bare specifiers - // TODO: Logging that the specifier could not resolve. - return undefined; + return resolve(resolvedModule.resolvedFileName.replace(/\.module\.d\.css\.ts$/u, '.module.css')); } + return undefined; }; } - -/** - * Check if the specifier is a relative specifier. - * @see https://nodejs.org/docs/latest-v23.x/api/esm.html#terminology - */ -function isRelativeSpecifier(specifier: string): boolean { - return specifier.startsWith('./') || specifier.startsWith('../'); -}