feat(nuxt3): support components with global: false and always enable transform (#1578)

This commit is contained in:
pooya parsa 2021-10-29 13:36:55 +02:00 committed by GitHub
parent 2625a0394f
commit 4e424d0d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 23 deletions

View File

@ -8,14 +8,13 @@ export interface Component {
level: number level: number
prefetch: boolean prefetch: boolean
preload: boolean preload: boolean
global?: boolean
/** @deprecated */ /** @deprecated */
import?: string import?: string
/** @deprecated */ /** @deprecated */
asyncImport?: string asyncImport?: string
/** @deprecated */ /** @deprecated */
global?: boolean
/** @deprecated */
async?: boolean async?: boolean
} }
@ -62,8 +61,11 @@ export interface ScanDir {
extendComponent?: (component: Component) => Promise<Component | void> | (Component | void) extendComponent?: (component: Component) => Promise<Component | void> | (Component | void)
/** @deprecated */ /**
global?: boolean | 'dev' * If enabled, registers components to be globally available
*
*/
global?: boolean
} }
export interface ComponentsDir extends ScanDir { export interface ComponentsDir extends ScanDir {

View File

@ -93,10 +93,8 @@ export default defineNuxtModule({
} }
}) })
if (!nuxt.options.dev) { const loaderOptions = { getComponents: () => components }
const options = { getComponents: () => components } addWebpackPlugin(loaderPlugin.webpack(loaderOptions))
addWebpackPlugin(loaderPlugin.webpack(options)) addVitePlugin(loaderPlugin.vite(loaderOptions))
addVitePlugin(loaderPlugin.vite(options))
}
} }
}) })

View File

@ -18,11 +18,11 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
const filePaths = new Set<string>() const filePaths = new Set<string>()
const scannedPaths: string[] = [] const scannedPaths: string[] = []
for (const { path, pattern, ignore = [], prefix, extendComponent, pathPrefix, level, prefetch = false, preload = false } of dirs.sort(sortDirsByPathLength)) { for (const dir of dirs.sort(sortDirsByPathLength)) {
const resolvedNames = new Map<string, string>() const resolvedNames = new Map<string, string>()
for (const _file of await globby(pattern!, { cwd: path, ignore })) { for (const _file of await globby(dir.pattern!, { cwd: dir.path, ignore: dir.ignore })) {
const filePath = join(path, _file) const filePath = join(dir.path, _file)
if (scannedPaths.find(d => filePath.startsWith(d))) { if (scannedPaths.find(d => filePath.startsWith(d))) {
continue continue
@ -33,12 +33,12 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
// Resolve componentName // Resolve componentName
const prefixParts = ([] as string[]).concat( const prefixParts = ([] as string[]).concat(
prefix ? splitByCase(prefix) : [], dir.prefix ? splitByCase(dir.prefix) : [],
(pathPrefix !== false) ? splitByCase(relative(path, dirname(filePath))) : [] (dir.pathPrefix !== false) ? splitByCase(relative(dir.path, dirname(filePath))) : []
) )
let fileName = basename(filePath, extname(filePath)) let fileName = basename(filePath, extname(filePath))
if (fileName.toLowerCase() === 'index') { if (fileName.toLowerCase() === 'index') {
fileName = pathPrefix === false ? basename(dirname(filePath)) : '' /* inherits from path */ fileName = dir.pathPrefix === false ? basename(dirname(filePath)) : '' /* inherits from path */
} }
const fileNameParts = splitByCase(fileName) const fileNameParts = splitByCase(fileName)
@ -74,14 +74,14 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
chunkName, chunkName,
shortPath, shortPath,
export: 'default', export: 'default',
global: Boolean(global), global: dir.global,
level: Number(level), level: Number(dir.level),
prefetch: Boolean(prefetch), prefetch: Boolean(dir.prefetch),
preload: Boolean(preload) preload: Boolean(dir.preload)
} }
if (typeof extendComponent === 'function') { if (typeof dir.extendComponent === 'function') {
component = (await extendComponent(component)) || component component = (await dir.extendComponent(component)) || component
} }
// Check if component is already defined, used to overwite if level is inferiour // Check if component is already defined, used to overwite if level is inferiour
@ -93,7 +93,7 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
} }
} }
scannedPaths.push(path) scannedPaths.push(dir.path)
} }
return components return components

View File

@ -28,7 +28,7 @@ export const componentsTemplate = {
return `import { defineAsyncComponent } from 'vue' return `import { defineAsyncComponent } from 'vue'
const components = { const components = {
${options.components.map((c) => { ${options.components.filter(c => c.global !== false).map((c) => {
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']` const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
const magicComments = createImportMagicComments(c) const magicComments = createImportMagicComments(c)