25 lines
842 B
JavaScript
25 lines
842 B
JavaScript
|
|
import path from 'node:path';
|
||
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||
|
|
import { existsSync } from 'node:fs';
|
||
|
|
|
||
|
|
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||
|
|
const libRoot = path.join(projectRoot, 'src', 'lib');
|
||
|
|
|
||
|
|
export function resolve(specifier, context, nextResolve) {
|
||
|
|
if (specifier.startsWith('$lib/')) {
|
||
|
|
const relative = specifier.slice('$lib/'.length);
|
||
|
|
const candidates = [
|
||
|
|
path.join(libRoot, relative + '.ts'),
|
||
|
|
path.join(libRoot, relative + '.js'),
|
||
|
|
path.join(libRoot, relative, 'index.ts'),
|
||
|
|
path.join(libRoot, relative, 'index.js'),
|
||
|
|
];
|
||
|
|
for (const candidate of candidates) {
|
||
|
|
if (existsSync(candidate)) {
|
||
|
|
return nextResolve(pathToFileURL(candidate).href, context);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nextResolve(specifier, context);
|
||
|
|
}
|