You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import path from 'path';
|
|
import { builtinModules } from 'module';
|
|
import { resolveDependencyData, isCommonDepWithoutSvelteField } from './dependencies';
|
|
import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
|
|
import { normalizePath } from 'vite';
|
|
|
|
export async function resolveViaPackageJsonSvelte(
|
|
importee: string,
|
|
importer: string | undefined,
|
|
cache: VitePluginSvelteCache
|
|
): Promise<string | void> {
|
|
if (
|
|
importer &&
|
|
isBareImport(importee) &&
|
|
!isNodeInternal(importee) &&
|
|
!isCommonDepWithoutSvelteField(importee)
|
|
) {
|
|
const cached = cache.getResolvedSvelteField(importee, importer);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const pkgData = await resolveDependencyData(importee, importer);
|
|
if (pkgData) {
|
|
const { pkg, dir } = pkgData;
|
|
if (pkg.svelte) {
|
|
const result = normalizePath(path.resolve(dir, pkg.svelte));
|
|
cache.setResolvedSvelteField(importee, importer, result);
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function isNodeInternal(importee: string) {
|
|
return importee.startsWith('node:') || builtinModules.includes(importee);
|
|
}
|
|
|
|
function isBareImport(importee: string): boolean {
|
|
if (
|
|
!importee ||
|
|
importee[0] === '.' ||
|
|
importee[0] === '\0' ||
|
|
importee.includes(':') ||
|
|
path.isAbsolute(importee)
|
|
) {
|
|
return false;
|
|
}
|
|
const parts = importee.split('/');
|
|
switch (parts.length) {
|
|
case 1:
|
|
return true;
|
|
case 2:
|
|
return parts[0].startsWith('@');
|
|
default:
|
|
return false;
|
|
}
|
|
}
|