Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
215 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
import Pool from 'tinypool' | ||
import type { GetExportsOptions } from './types' | ||
|
||
let _worker: Pool | undefined | ||
|
||
export async function getExports(name: string, options?: GetExportsOptions) { | ||
if (!_worker) { | ||
_worker = new Pool({ | ||
filename: new URL('./worker.js', import.meta.url).href, | ||
}) | ||
} | ||
return await _worker.run({ name, options }) | ||
} | ||
export * from './runtime' | ||
export * from './static' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Pool from 'tinypool' | ||
import type { GetExportsOptions } from './types' | ||
|
||
let _worker: Pool | undefined | ||
|
||
export async function getExportsRuntime(name: string, options?: GetExportsOptions) { | ||
if (!_worker) { | ||
_worker = new Pool({ | ||
filename: new URL('./worker.js', import.meta.url).href, | ||
}) | ||
} | ||
return await _worker.run({ name, options }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { promises as fs } from 'fs' | ||
import { fileURLToPath, pathToFileURL } from 'url' | ||
import { resolve } from 'path' | ||
import { findExports, resolve as resolvePackagePath } from 'mlly' | ||
import { getPackageInfo } from 'local-pkg' | ||
import type { GetExportsOptions } from './types' | ||
|
||
export async function getExportsStatic(name: string, options?: GetExportsOptions) { | ||
const map = new Map<string, Promise<string[]>>() | ||
|
||
async function resolvePackageEntry(name: string, importer?: string) { | ||
try { | ||
const { rootPath, packageJson } = (await getPackageInfo(name, { paths: importer ? [fileURLToPath(importer)] : undefined }))! | ||
if (!packageJson.exports && packageJson.module) | ||
return pathToFileURL(resolve(rootPath, packageJson.module)).href | ||
} | ||
catch {} | ||
|
||
return await resolvePackagePath(name, { | ||
url: importer, | ||
extensions: ['.mjs', '.js'], | ||
conditions: ['module', 'import', 'require', 'default'], | ||
}) | ||
} | ||
|
||
async function getExportsRelative(relative: string, importer?: string): Promise<string[]> { | ||
const url = relative.match(/^[@a-z]/) | ||
? await resolvePackageEntry(relative, importer) | ||
: new URL(relative, importer).href | ||
|
||
return getExportsUrl(url) | ||
} | ||
|
||
async function getExportsUrl(url: string) { | ||
if (!map.has(url)) | ||
map.set(url, _getExportsUrl(url)) | ||
return await map.get(url)! | ||
} | ||
|
||
async function _getExportsUrl(url: string) { | ||
const code = await fs.readFile(fileURLToPath(url), 'utf8') | ||
const exports = findExports(code) | ||
|
||
return (await Promise.all( | ||
exports.map(async (i) => { | ||
if (i.type === 'star' && i.specifier) | ||
return await getExportsRelative(i.specifier, url) | ||
return i.names | ||
}))) | ||
.flat() | ||
} | ||
|
||
return getExportsRelative(name, options?.url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { getExportsStatic } from '../src' | ||
|
||
describe('ESM', () => { | ||
it('@antfu/utils', async () => { | ||
expect((await getExportsStatic('@antfu/utils')).slice(0, 5)) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
"assert", | ||
"at", | ||
"batchInvoke", | ||
"clamp", | ||
"clampArrayRange", | ||
] | ||
`) | ||
}) | ||
|
||
it.only('vue', async () => { | ||
expect((await getExportsStatic('vue'))) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
"compile", | ||
"Transition", | ||
"TransitionGroup", | ||
"VueElement", | ||
"createApp", | ||
"createSSRApp", | ||
"defineCustomElement", | ||
"defineSSRCustomElement", | ||
"hydrate", | ||
"initDirectivesForSSR", | ||
"render", | ||
"useCssModule", | ||
"useCssVars", | ||
"vModelCheckbox", | ||
"vModelDynamic", | ||
"vModelRadio", | ||
"vModelSelect", | ||
"vModelText", | ||
"vShow", | ||
"withKeys", | ||
"withModifiers", | ||
"BaseTransition", | ||
"Comment", | ||
"Fragment", | ||
"KeepAlive", | ||
"Static", | ||
"Suspense", | ||
"Teleport", | ||
"Text", | ||
"callWithAsyncErrorHandling", | ||
"callWithErrorHandling", | ||
"cloneVNode", | ||
"compatUtils", | ||
"computed", | ||
"createBlock", | ||
"createCommentVNode", | ||
"createElementBlock", | ||
"createElementVNode", | ||
"createHydrationRenderer", | ||
"createPropsRestProxy", | ||
"createRenderer", | ||
"createSlots", | ||
"createStaticVNode", | ||
"createTextVNode", | ||
"createVNode", | ||
"defineAsyncComponent", | ||
"defineComponent", | ||
"defineEmits", | ||
"defineExpose", | ||
"defineProps", | ||
"devtools", | ||
"getCurrentInstance", | ||
"getTransitionRawChildren", | ||
"guardReactiveProps", | ||
"h", | ||
"handleError", | ||
"initCustomFormatter", | ||
"inject", | ||
"isMemoSame", | ||
"isRuntimeOnly", | ||
"isVNode", | ||
"mergeDefaults", | ||
"mergeProps", | ||
"nextTick", | ||
"onActivated", | ||
"onBeforeMount", | ||
"onBeforeUnmount", | ||
"onBeforeUpdate", | ||
"onDeactivated", | ||
"onErrorCaptured", | ||
"onMounted", | ||
"onRenderTracked", | ||
"onRenderTriggered", | ||
"onServerPrefetch", | ||
"onUnmounted", | ||
"onUpdated", | ||
"openBlock", | ||
"popScopeId", | ||
"provide", | ||
"pushScopeId", | ||
"queuePostFlushCb", | ||
"registerRuntimeCompiler", | ||
"renderList", | ||
"renderSlot", | ||
"resolveComponent", | ||
"resolveDirective", | ||
"resolveDynamicComponent", | ||
"resolveFilter", | ||
"resolveTransitionHooks", | ||
"setBlockTracking", | ||
"setDevtoolsHook", | ||
"setTransitionHooks", | ||
"ssrContextKey", | ||
"ssrUtils", | ||
"toHandlers", | ||
"transformVNodeArgs", | ||
"useAttrs", | ||
"useSSRContext", | ||
"useSlots", | ||
"useTransitionState", | ||
"version", | ||
"warn", | ||
"watch", | ||
"watchEffect", | ||
"watchPostEffect", | ||
"watchSyncEffect", | ||
"withAsyncContext", | ||
"withCtx", | ||
"withDefaults", | ||
"withDirectives", | ||
"withMemo", | ||
"withScopeId", | ||
] | ||
`) | ||
}) | ||
}) |