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 {
|
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']
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query `?vue&type=template` (in Webpack or external template)
|
|
|
|
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 || []
|
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
|
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' }]))
|
2022-08-22 08:50:11 +00:00
|
|
|
imports.add(`const ${identifier}_lazy = /*#__PURE__*/ __defineAsyncComponent(${genDynamicImport(component.filePath)})`)
|
|
|
|
return isClientOnly ? `/*#__PURE__*/ createClientOnly(${identifier}_lazy)` : `${identifier}_lazy`
|
2022-05-06 11:11:34 +00:00
|
|
|
} else {
|
|
|
|
imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }]))
|
2022-08-22 08:50:11 +00:00
|
|
|
return isClientOnly ? `/*#__PURE__*/ createClientOnly(${identifier})` : identifier
|
2022-05-06 11:11:34 +00:00
|
|
|
}
|
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, '')
|
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
|
|
|
}
|