|
1 | 1 | import { gzip } from 'node:zlib' |
2 | 2 | import type { NuxtTemplate } from '@nuxt/schema' |
3 | 3 | import { isAbsolute, join, relative } from 'pathe' |
4 | | -import { genDynamicImport } from 'knitwork' |
5 | | -import { compile as jsonSchemaToTypescript } from 'json-schema-to-typescript-lite' |
6 | | -import type { JSONSchema } from 'json-schema-to-typescript-lite' |
| 4 | +import { compile as jsonSchemaToTypescript } from 'json-schema-to-typescript' |
| 5 | +import type { JSONSchema } from 'json-schema-to-typescript' |
7 | 6 | import { pascalCase } from 'scule' |
8 | 7 | import type { Schema } from 'untyped' |
9 | 8 | import type { CollectionInfo, ResolvedCollection } from '../types/collection' |
@@ -137,21 +136,32 @@ export const componentsManifestTemplate = (manifest: Manifest) => { |
137 | 136 | return nuxt.options.dev || options.manifest.components.includes(c.pascalName) || c.global |
138 | 137 | }) |
139 | 138 | .reduce((map, c) => { |
140 | | - map[c.pascalName] = map[c.pascalName] || [ |
141 | | - c.pascalName, |
142 | | - `${genDynamicImport(isAbsolute(c.filePath) |
143 | | - ? './' + relative(join(nuxt.options.buildDir, 'content'), c.filePath).replace(/\b\.(?!vue)\w+$/g, '') |
144 | | - : c.filePath.replace(/\b\.(?!vue)\w+$/g, ''), { wrapper: false, singleQuotes: true })}`, |
145 | | - c.global, |
146 | | - ] |
| 139 | + const importPath = isAbsolute(c.filePath) |
| 140 | + ? './' + relative(join(nuxt.options.buildDir, 'content'), c.filePath).replace(/\b\.(?!vue)\w+$/g, '') |
| 141 | + : c.filePath.replace(/\b\.(?!vue)\w+$/g, '') |
| 142 | + map[c.pascalName] = map[c.pascalName] || [c.pascalName, importPath, c.global, c.export || 'default'] |
147 | 143 | return map |
148 | 144 | }, {} as Record<string, unknown[]>) |
149 | 145 |
|
150 | 146 | const componentsList = Object.values(componentsMap) |
151 | 147 | const globalComponents = componentsList.filter(c => c[2]).map(c => c[0]) |
152 | 148 | const localComponents = componentsList.filter(c => !c[2]) |
153 | 149 | return [ |
154 | | - ...localComponents.map(([pascalName, type]) => `export const ${pascalName} = () => ${type}`), |
| 150 | + 'const pickExport = (mod, exportName, componentName, path) => {', |
| 151 | + ' const resolved = exportName === \'default\' ? mod?.default : mod?.[exportName]', |
| 152 | + ' if (!resolved) {', |
| 153 | + ' throw new Error(`[nuxt-content] Missing export "${exportName}" for component "${componentName}" in "${path}".`)', |
| 154 | + ' }', |
| 155 | + ' return resolved', |
| 156 | + '}', |
| 157 | + 'export const localComponentLoaders = {', |
| 158 | + ...localComponents.map(([pascalName, path, , exp]) => { |
| 159 | + const pathLiteral = JSON.stringify(path) |
| 160 | + const exportLiteral = JSON.stringify(exp) |
| 161 | + const nameLiteral = JSON.stringify(pascalName) |
| 162 | + return ` ${pascalName}: () => import(${pathLiteral}).then(m => pickExport(m, ${exportLiteral}, ${nameLiteral}, ${pathLiteral})),` |
| 163 | + }), |
| 164 | + '}', |
155 | 165 | `export const globalComponents: string[] = ${JSON.stringify(globalComponents)}`, |
156 | 166 | `export const localComponents: string[] = ${JSON.stringify(localComponents.map(c => c[0]))}`, |
157 | 167 | ].join('\n') |
|
0 commit comments