|
| 1 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L5 |
| 2 | + |
| 3 | +import { AsyncFunction } from './utils/helper' |
| 4 | +import { getImportBase } from './utils/import-base' |
| 5 | +import { exportDefaultRegex, exportRegex, importAsRegex, importDefaultRegex, importObjectRegex, importRegex } from './utils/regexp' |
| 6 | + |
| 7 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L7-L9 |
| 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(getImportBase() + name) |
| 18 | + const json = await response.json() |
| 19 | + |
| 20 | + return { default: json } |
| 21 | + } |
| 22 | + |
| 23 | + return nativeImport(getImportBase() + name) |
| 24 | +} |
| 25 | + |
| 26 | +// https://github.com/unocss/unocss/blob/6d94efc56b0c966f25f46d8988b3fd30ebc189aa/packages/shared-docs/src/config.ts#L27-L37 |
| 27 | +// bypass vite interop |
| 28 | +async function dynamicImportAnyModule(name: string): Promise<any> { |
| 29 | + if (modulesCache.has(name)) |
| 30 | + return modulesCache.get(name) |
| 31 | + |
| 32 | + try { |
| 33 | + const module = await fetchAndImportAnyModuleWithCDNCapabilities(name) |
| 34 | + modulesCache.set(name, module) |
| 35 | + |
| 36 | + return module |
| 37 | + } |
| 38 | + catch (error) { |
| 39 | + console.error(`Failed to import module ${name} from CDN`, error) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// https://github.com/unocss/unocss/blob/main/packages/shared-docs/src/config.ts |
| 44 | +export async function evaluateAnyModule<T>(configCode: string): Promise<T | undefined> { |
| 45 | + const transformedCode = configCode |
| 46 | + .replace(importObjectRegex, (_match, p1, _p2, p3) => { |
| 47 | + // Replace `as` with `:` within the destructuring assignment |
| 48 | + const transformedP1 = p1.replace(importAsRegex, '$1: $2') |
| 49 | + return `const {${transformedP1}} = await __import("${p3}");` |
| 50 | + }) |
| 51 | + .replace(importDefaultRegex, 'const $1 = (await __import("$3")).default;') |
| 52 | + .replace(exportDefaultRegex, 'return ') |
| 53 | + .replace(importRegex, '__import(') |
| 54 | + .replace(exportRegex, 'return function ') |
| 55 | + |
| 56 | + const wrappedDynamicImport = new AsyncFunction('__import', transformedCode) |
| 57 | + return await wrappedDynamicImport(dynamicImportAnyModule) |
| 58 | +} |
0 commit comments