mirror of
https://github.com/withastro/astro.git
synced 2025-02-24 22:46:02 -05:00
* chore: import sort source code, exception for the `astro` package * fix import sorting bug * Update packages/integrations/lit/server.js Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
22 lines
795 B
TypeScript
22 lines
795 B
TypeScript
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
// This file should be imported as `#import-plugin`
|
|
import { resolve as importMetaResolve } from 'import-meta-resolve';
|
|
import type * as unified from 'unified';
|
|
|
|
let cwdUrlStr: string | undefined;
|
|
|
|
// In non-browser enviroments, we can try to resolve from the filesystem too
|
|
export async function importPlugin(p: string): Promise<unified.Plugin> {
|
|
// Try import from this package first
|
|
try {
|
|
const importResult = await import(p);
|
|
return importResult.default;
|
|
} catch {}
|
|
|
|
// Try import from user project
|
|
cwdUrlStr ??= pathToFileURL(path.join(process.cwd(), 'package.json')).toString();
|
|
const resolved = importMetaResolve(p, cwdUrlStr);
|
|
const importResult = await import(resolved);
|
|
return importResult.default;
|
|
}
|