File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed
vite-plugin-mock-dev-server/src Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change 11import type { CompilerOptions , CompilerResult , MockRawData , TransformResult } from './types'
22import process from 'node:process'
3- import { isPackageExists } from 'local-pkg '
3+ import { isPackageExists } from '../utils '
44import { transformWithEsbuild } from './esbuild'
55import { loadFromCode } from './loadFromCode'
66import { transformWithRolldown } from './rolldown'
77
8- const hasRolldown = isPackageExists ( 'rolldown' )
9- const hasEsbuild = isPackageExists ( 'esbuild' )
10-
118export async function transform ( entryPoint : string , options : CompilerOptions ) : Promise < TransformResult > {
12- if ( hasRolldown )
9+ if ( await isPackageExists ( 'rolldown' ) )
1310 return transformWithRolldown ( entryPoint , options )
14- if ( hasEsbuild )
11+ if ( await isPackageExists ( 'esbuild' ) )
1512 return transformWithEsbuild ( entryPoint , options )
1613 throw new Error ( 'rolldown or esbuild not found' )
1714}
Original file line number Diff line number Diff line change 11import type { Readable , Stream } from 'node:stream'
2+ import { hasOwn } from '@pengzhanbo/utils'
23
34export function isStream ( stream : unknown ) : stream is Stream {
45 return stream !== null
@@ -12,3 +13,25 @@ export function isReadableStream(stream: unknown): stream is Readable {
1213 && typeof ( stream as any ) . _read === 'function'
1314 && typeof ( stream as any ) . _readableState === 'object'
1415}
16+
17+ const PACKAGE_CACHE : Record < string , boolean > = { }
18+
19+ export async function isPackageExists ( mod : string ) : Promise < boolean > {
20+ if ( hasOwn ( PACKAGE_CACHE , mod ) ) {
21+ return PACKAGE_CACHE [ mod ]
22+ }
23+ try {
24+ // @ts -expect-error fallback for node
25+ if ( import . meta. resolve ) {
26+ PACKAGE_CACHE [ mod ] = ! ! import . meta. resolve ( mod )
27+ }
28+ else {
29+ await import ( mod )
30+ PACKAGE_CACHE [ mod ] = true
31+ }
32+ return PACKAGE_CACHE [ mod ]
33+ }
34+ catch { }
35+ PACKAGE_CACHE [ mod ] = false
36+ return false
37+ }
You can’t perform that action at this time.
0 commit comments