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-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')
|
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) {
|
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) {
|
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) {
|
|
|
|
console.warn(`Standalone server components (\`${name}\`) are not yet supported without enabling \`experimental.componentIslands\`.`)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
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
|
2023-04-14 17:21:08 +00:00
|
|
|
? s.generateMap({ hires: true })
|
2022-08-22 10:12:02 +00:00
|
|
|
: 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
|
|
|
}
|