Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-waves-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@css-modules-kit/core': patch
---

refactor: refactor resolver
30 changes: 4 additions & 26 deletions packages/core/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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('../');
}
Loading