2023-01-11 20:51:49 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2021-07-28 12:11:32 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import { parseQuery, parseURL } from 'ufo'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { Component, ComponentsOptions } from '@nuxt/schema'
|
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'
|
|
|
|
import { distDir } from '../dirs'
|
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-07-22 08:28:16 +00:00
|
|
|
function isVueTemplate (id: string) {
|
|
|
|
// Bare `.vue` file (in Vite)
|
|
|
|
if (id.endsWith('.vue')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const { search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
|
|
|
if (!search) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = parseQuery(search)
|
|
|
|
|
|
|
|
// Macro
|
|
|
|
if (query.macro) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-Vue or Styles
|
|
|
|
if (!('vue' in query) || query.type === 'style') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-13 15:00:57 +00:00
|
|
|
// Query `?vue&type=template` (in webpack or external template)
|
2022-07-22 08:28:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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')
|
2022-04-22 15:35:42 +00:00
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
return {
|
|
|
|
name: 'nuxt:components-loader',
|
|
|
|
enforce: 'post',
|
|
|
|
transformInclude (id) {
|
|
|
|
if (exclude.some(pattern => id.match(pattern))) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (include.some(pattern => id.match(pattern))) {
|
|
|
|
return true
|
|
|
|
}
|
2022-07-22 08:28:16 +00:00
|
|
|
return isVueTemplate(id)
|
2022-05-06 11:11:34 +00:00
|
|
|
},
|
|
|
|
transform (code, id) {
|
|
|
|
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) {
|
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-01-09 11:20:33 +00:00
|
|
|
const isServerOnly = component.mode === 'server' &&
|
|
|
|
!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) {
|
|
|
|
console.warn(`Standalone server components (\`${name}\`) are not yet supported without enabling \`experimental.componentIslands\`.`)
|
|
|
|
}
|
|
|
|
return identifier
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:14:07 +00:00
|
|
|
const isClientOnly = component.mode === 'client'
|
|
|
|
if (isClientOnly) {
|
|
|
|
imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
|
|
|
|
identifier += '_client'
|
|
|
|
}
|
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
if (lazy) {
|
|
|
|
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
2022-09-06 07:40:25 +00:00
|
|
|
identifier += '_lazy'
|
2022-10-03 14:14:07 +00:00
|
|
|
imports.add(`const ${identifier} = /*#__PURE__*/ __defineAsyncComponent(${genDynamicImport(component.filePath, { interopDefault: true })}${isClientOnly ? '.then(c => createClientOnly(c))' : ''})`)
|
2022-05-06 11:11:34 +00:00
|
|
|
} else {
|
|
|
|
imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }]))
|
2022-09-06 07:40:25 +00:00
|
|
|
|
2022-10-03 14:14:07 +00:00
|
|
|
if (isClientOnly) {
|
|
|
|
imports.add(`const ${identifier}_wrapped = /*#__PURE__*/ createClientOnly(${identifier})`)
|
|
|
|
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
|
|
|
|
? s.generateMap({ source: id, includeContent: true })
|
|
|
|
: undefined
|
2022-05-06 11:11:34 +00:00
|
|
|
}
|
2022-04-22 15:35:42 +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
|
|
|
}
|