|
| 1 | +import { $fetch } from 'ofetch' |
| 2 | + |
| 3 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L5 |
| 4 | +const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor |
| 5 | + |
| 6 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L7-L9 |
| 7 | +const CDN_BASE = 'https://esm.sh/' |
| 8 | +const modulesCache = new Map<string, Promise<unknown> | unknown>() |
| 9 | + |
| 10 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L26 |
| 11 | +// eslint-disable-next-line no-new-func |
| 12 | +const nativeImport = new Function('a', 'return import(a);') |
| 13 | + |
| 14 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L31-L33 |
| 15 | +async function fetchAndImportAnyModuleWithCDNCapabilities(name: string) { |
| 16 | + if (name.endsWith('.json')) { |
| 17 | + const response = await $fetch(CDN_BASE + name, { responseType: 'json' }) |
| 18 | + return { default: response } |
| 19 | + } |
| 20 | + |
| 21 | + return nativeImport(CDN_BASE + name) |
| 22 | +} |
| 23 | + |
| 24 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L27-L37 |
| 25 | +// bypass vite interop |
| 26 | +async function dynamicImportAnyModule(name: string): Promise<any> { |
| 27 | + if (modulesCache.has(name)) |
| 28 | + return modulesCache.get(name) |
| 29 | + |
| 30 | + try { |
| 31 | + const module = await fetchAndImportAnyModuleWithCDNCapabilities(name) |
| 32 | + modulesCache.set(name, module) |
| 33 | + } |
| 34 | + catch (error) { |
| 35 | + console.error(`Failed to import module ${name} from CDN`, error) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// https://github.com/unocss/unocss/blob/main/packages/shared-docs/src/config.ts |
| 40 | +const importUnocssRegex = /import\s(.*?)\sfrom\s*(['"])unocss\2/g |
| 41 | +const importObjectRegex = /import\s*\{([\s\S]*?)\}\s*from\s*(['"])([\w@/-]+)\2/g |
| 42 | +const importDefaultRegex = /import\s(.*?)\sfrom\s*(['"])([\w@/-]+)\2/g |
| 43 | +const exportDefaultRegex = /export default / |
| 44 | +const importRegex = /\bimport\s*\(/g |
| 45 | + |
| 46 | +// New regex to handle `as` to `:` transformation |
| 47 | +const importAsRegex = /(\w+)\s+as\s+(\w+)/g |
| 48 | + |
| 49 | +// https://github.com/unocss/unocss/blob/main/packages/shared-docs/src/config.ts |
| 50 | +export async function evaluateAnyModule<T = any>(configCode: string): Promise<T | undefined> { |
| 51 | + const transformedCode = configCode |
| 52 | + .replace(importUnocssRegex, 'const $1 = await __import("unocss");') |
| 53 | + .replace(importObjectRegex, (match, p1, p2, p3) => { |
| 54 | + // Replace `as` with `:` within the destructuring assignment |
| 55 | + const transformedP1 = p1.replace(importAsRegex, '$1: $2') |
| 56 | + return `const {${transformedP1}} = await __import("${p3}");` |
| 57 | + }) |
| 58 | + .replace(importDefaultRegex, 'const $1 = (await __import("$3")).default;') |
| 59 | + .replace(exportDefaultRegex, 'return ') |
| 60 | + .replace(importRegex, '__import(') |
| 61 | + |
| 62 | + const wrappedDynamicImport = new AsyncFunction('__import', transformedCode) |
| 63 | + return await wrappedDynamicImport(dynamicImportAnyModule) |
| 64 | +} |
0 commit comments