2022-04-15 15:19:05 +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-05-06 11:11:34 +00:00
|
|
|
import { 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'
|
2021-07-28 12:11:32 +00:00
|
|
|
|
|
|
|
interface LoaderOptions {
|
|
|
|
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']
|
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 || []
|
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-04-22 15:35:42 +00:00
|
|
|
|
2022-05-06 11:11:34 +00:00
|
|
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
|
|
|
const query = parseQuery(search)
|
|
|
|
// we only transform render functions
|
|
|
|
// from `type=template` (in Webpack) and bare `.vue` file (in Vite)
|
|
|
|
return pathname.endsWith('.vue') && (query.type === 'template' || !!query.macro || !search)
|
|
|
|
},
|
|
|
|
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
|
2022-06-12 21:22:15 +00:00
|
|
|
s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy)?([^'"]*?)["'][\s,]*\)/g, (full, lazy, name) => {
|
2022-05-06 11:11:34 +00:00
|
|
|
const component = findComponent(components, name, options.mode)
|
|
|
|
if (component) {
|
|
|
|
const identifier = map.get(component) || `__nuxt_component_${num++}`
|
|
|
|
map.set(component, identifier)
|
|
|
|
const isClientOnly = component.mode === 'client'
|
|
|
|
if (isClientOnly) {
|
|
|
|
imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
|
|
|
|
}
|
|
|
|
if (lazy) {
|
|
|
|
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
|
|
|
imports.add(`const ${identifier}_lazy = __defineAsyncComponent(${genDynamicImport(component.filePath)})`)
|
|
|
|
return isClientOnly ? `createClientOnly(${identifier}_lazy)` : `${identifier}_lazy`
|
|
|
|
} else {
|
|
|
|
imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }]))
|
|
|
|
return isClientOnly ? `createClientOnly(${identifier})` : 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(),
|
|
|
|
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
|
|
}
|
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, '')
|
2022-04-19 19:13:55 +00:00
|
|
|
const component = components.find(component => id === component.pascalName && ['all', mode, undefined].includes(component.mode))
|
|
|
|
if (!component && components.some(component => id === component.pascalName)) {
|
|
|
|
return components.find(component => component.pascalName === 'ServerPlaceholder')
|
|
|
|
}
|
|
|
|
return component
|
2021-07-28 12:11:32 +00:00
|
|
|
}
|