2021-07-28 12:11:32 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
2022-03-22 17:04:31 +00:00
|
|
|
import { genDynamicImport, genImport } from 'knitwork'
|
2022-03-03 10:01:14 +00:00
|
|
|
import MagicString from 'magic-string'
|
2022-03-18 18:42:40 +00:00
|
|
|
import { pascalCase } from 'scule'
|
2023-01-11 20:51:49 +00:00
|
|
|
import { resolve } from 'pathe'
|
2023-03-11 21:16:01 +00:00
|
|
|
import type { Component, ComponentsOptions } from 'nuxt/schema'
|
|
|
|
|
2023-12-05 17:33:27 +00:00
|
|
|
import { logger, tryUseNuxt } from '@nuxt/kit'
|
2023-01-11 20:51:49 +00:00
|
|
|
import { distDir } from '../dirs'
|
2023-04-19 18:17:36 +00:00
|
|
|
import { isVue } from '../core/utils'
|
2021-07-28 12:11:32 +00:00
|
|
|
|
|
|
|
interface LoaderOptions {
|
2022-08-22 08:50:11 +00:00
|
|
|
getComponents (): Component[]
|
2022-04-19 19:13:55 +00:00
|
|
|
mode: 'server' | 'client'
|
2022-04-22 15:35:42 +00:00
|
|
|
sourcemap?: boolean
|
2022-05-06 11:11:34 +00:00
|
|
|
transform?: ComponentsOptions['transform']
|
2023-01-09 11:20:33 +00:00
|
|
|
experimentalComponentIslands?: boolean
|
2021-07-28 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
|
|
|
|
const exclude = options.transform?.exclude || []
|
|
|
|
const include = options.transform?.include || []
|
2023-01-11 20:51:49 +00:00
|
|
|
const serverComponentRuntime = resolve(distDir, 'components/runtime/server-component')
|
2024-04-08 14:19:59 +00:00
|
|
|
const clientDelayedComponentRuntime = resolve(distDir, 'components/runtime/client-delayed-component')
|
2022-05-06 11:11:34 +00:00
|
|
|
return {
|
|
|
|
name: 'nuxt:components-loader',
|
|
|
|
enforce: 'post',
|
|
|
|
transformInclude (id) {
|
2023-05-22 20:25:42 +00:00
|
|
|
if (exclude.some(pattern => pattern.test(id))) {
|
2022-05-06 11:11:34 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-22 20:25:42 +00:00
|
|
|
if (include.some(pattern => pattern.test(id))) {
|
2022-05-06 11:11:34 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-07-26 04:30:44 +00:00
|
|
|
return isVue(id, { type: ['template', 'script'] }) || !!id.match(/\.[tj]sx$/)
|
2022-05-06 11:11:34 +00:00
|
|
|
},
|
2023-04-14 17:21:08 +00:00
|
|
|
transform (code) {
|
2022-05-06 11:11:34 +00:00
|
|
|
const components = options.getComponents()
|
|
|
|
|
|
|
|
let num = 0
|
|
|
|
const imports = new Set<string>()
|
|
|
|
const map = new Map<Component, string>()
|
|
|
|
const s = new MagicString(code)
|
|
|
|
|
|
|
|
// replace `_resolveComponent("...")` to direct import
|
2023-01-09 11:20:33 +00:00
|
|
|
s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy)?([^'"]*?)["'][\s,]*[^)]*\)/g, (full: string, lazy: string, name: string) => {
|
2022-05-06 11:11:34 +00:00
|
|
|
const component = findComponent(components, name, options.mode)
|
|
|
|
if (component) {
|
2023-10-17 11:07:31 +00:00
|
|
|
// @ts-expect-error TODO: refactor to nuxi
|
2023-12-05 17:33:27 +00:00
|
|
|
if (component._internal_install && tryUseNuxt()?.options.test === false) {
|
2023-10-17 11:07:31 +00:00
|
|
|
// @ts-expect-error TODO: refactor to nuxi
|
|
|
|
import('../core/features').then(({ installNuxtModule }) => installNuxtModule(component._internal_install))
|
|
|
|
}
|
2022-09-06 07:40:25 +00:00
|
|
|
let identifier = map.get(component) || `__nuxt_component_${num++}`
|
2022-05-06 11:11:34 +00:00
|
|
|
map.set(component, identifier)
|
2022-09-06 07:40:25 +00:00
|
|
|
|
2023-09-13 21:56:15 +00:00
|
|
|
const isServerOnly = !component._raw && component.mode === 'server' &&
|
2023-01-09 11:20:33 +00:00
|
|
|
!components.some(c => c.pascalName === component.pascalName && c.mode === 'client')
|
|
|
|
if (isServerOnly) {
|
|
|
|
imports.add(genImport(serverComponentRuntime, [{ name: 'createServerComponent' }]))
|
|
|
|
imports.add(`const ${identifier} = createServerComponent(${JSON.stringify(name)})`)
|
|
|
|
if (!options.experimentalComponentIslands) {
|
2023-09-19 21:26:15 +00:00
|
|
|
logger.warn(`Standalone server components (\`${name}\`) are not yet supported without enabling \`experimental.componentIslands\`.`)
|
2023-01-09 11:20:33 +00:00
|
|
|
}
|
|
|
|
return identifier
|
|
|
|
}
|
|
|
|
|
2023-09-13 21:56:15 +00:00
|
|
|
const isClientOnly = !component._raw && component.mode === 'client'
|
2022-10-03 14:14:07 +00:00
|
|
|
if (isClientOnly) {
|
|
|
|
imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
|
|
|
|
identifier += '_client'
|
|
|
|
}
|
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
if (lazy) {
|
2024-04-08 14:19:59 +00:00
|
|
|
// Temporary hardcoded check to verify runtime functionality
|
2024-04-08 14:22:13 +00:00
|
|
|
if (name === 'DelayedWrapperTestComponent') {
|
2024-04-09 19:55:27 +00:00
|
|
|
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
2024-04-08 14:22:13 +00:00
|
|
|
imports.add(genImport(clientDelayedComponentRuntime, [{ name: 'createLazyIOClientPage' }]))
|
|
|
|
identifier += '_delayedIO'
|
2024-04-09 19:55:27 +00:00
|
|
|
imports.add(`const ${identifier} = createLazyIOClientPage(__defineAsyncComponent(${genDynamicImport(component.filePath)}))`)
|
2024-04-08 14:22:13 +00:00
|
|
|
} else {
|
2024-04-08 14:19:59 +00:00
|
|
|
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
|
|
|
identifier += '_lazy'
|
|
|
|
imports.add(`const ${identifier} = __defineAsyncComponent(${genDynamicImport(component.filePath, { interopDefault: false })}.then(c => c.${component.export ?? 'default'} || c)${isClientOnly ? '.then(c => createClientOnly(c))' : ''})`)
|
|
|
|
}
|
2022-05-06 11:11:34 +00:00
|
|
|
} else {
|
2024-01-20 20:43:11 +00:00
|
|
|
imports.add(genImport(component.filePath, [{ name: component._raw ? 'default' : component.export, as: identifier }]))
|
2022-09-06 07:40:25 +00:00
|
|
|
|
2022-10-03 14:14:07 +00:00
|
|
|
if (isClientOnly) {
|
2023-10-30 21:04:45 +00:00
|
|
|
imports.add(`const ${identifier}_wrapped = createClientOnly(${identifier})`)
|
2022-10-03 14:14:07 +00:00
|
|
|
identifier += '_wrapped'
|
|
|
|
}
|
2022-09-06 07:40:25 +00:00
|
|
|
}
|
2022-10-03 14:14:07 +00:00
|
|
|
|
2022-09-06 07:40:25 +00:00
|
|
|
return identifier
|
2022-04-22 15:35:42 +00:00
|
|
|
}
|
2022-05-06 11:11:34 +00:00
|
|
|
// no matched
|
|
|
|
return full
|
|
|
|
})
|
2022-04-22 15:35:42 +00:00
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
if (imports.size) {
|
|
|
|
s.prepend([...imports, ''].join('\n'))
|
|
|
|
}
|
2022-04-22 15:35:42 +00:00
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
2022-08-22 10:12:02 +00:00
|
|
|
map: options.sourcemap
|
2023-04-14 17:21:08 +00:00
|
|
|
? s.generateMap({ hires: true })
|
2024-04-05 18:08:32 +00:00
|
|
|
: undefined,
|
2022-05-06 11:11:34 +00:00
|
|
|
}
|
2022-04-22 15:35:42 +00:00
|
|
|
}
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2021-07-28 12:11:32 +00:00
|
|
|
}
|
2022-05-06 11:11:34 +00:00
|
|
|
})
|
2021-07-28 12:11:32 +00:00
|
|
|
|
2022-04-19 19:13:55 +00:00
|
|
|
function findComponent (components: Component[], name: string, mode: LoaderOptions['mode']) {
|
2022-03-18 18:42:40 +00:00
|
|
|
const id = pascalCase(name).replace(/["']/g, '')
|
2023-01-09 11:20:33 +00:00
|
|
|
// Prefer exact match
|
2022-04-19 19:13:55 +00:00
|
|
|
const component = components.find(component => id === component.pascalName && ['all', mode, undefined].includes(component.mode))
|
2023-01-09 11:20:33 +00:00
|
|
|
if (component) { return component }
|
|
|
|
|
2023-01-25 10:30:59 +00:00
|
|
|
const otherModeComponent = components.find(component => id === component.pascalName)
|
|
|
|
|
2023-01-09 11:20:33 +00:00
|
|
|
// Render client-only components on the server with <ServerPlaceholder> (a simple div)
|
2023-01-25 10:30:59 +00:00
|
|
|
if (mode === 'server' && otherModeComponent) {
|
2023-01-09 11:20:33 +00:00
|
|
|
return components.find(c => c.pascalName === 'ServerPlaceholder')
|
2022-04-19 19:13:55 +00:00
|
|
|
}
|
2023-01-09 11:20:33 +00:00
|
|
|
|
|
|
|
// Return the other-mode component in all other cases - we'll handle createClientOnly
|
|
|
|
// and createServerComponent above
|
2023-01-25 10:30:59 +00:00
|
|
|
return otherModeComponent
|
2021-07-28 12:11:32 +00:00
|
|
|
}
|