From 4bc722be6b350ccb344fe2accbd94c01aa8ce580 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 23 Jan 2026 14:58:24 +0800 Subject: [PATCH 1/2] fix: cannot be used in the moduleDependencies --- src/module.ts | 138 +++++++++++++++++++++++++++----------------------- src/utils.ts | 97 +++++++++++++++++------------------ 2 files changed, 125 insertions(+), 110 deletions(-) diff --git a/src/module.ts b/src/module.ts index c6beb34..6f0cfb3 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,5 @@ -import { addPluginTemplate, addTemplate, defineNuxtModule } from '@nuxt/kit' -import { defaults, libraryName } from './config' +import { addPluginTemplate, addTemplate, defineNuxtModule } from "@nuxt/kit"; +import { defaults, libraryName } from "./config"; import { resolveCache, resolveComponents, @@ -15,88 +15,102 @@ import { resolveThemes, resolveMethods, transformPlugin, - localePlugin -} from './core/index' -import { getLayersDir } from './utils' -import type { ModuleOptions } from './types' -export type { ModuleOptions } from './types' + localePlugin, +} from "./core/index"; +import { getLayersDir } from "./utils"; +import type { ModuleOptions } from "./types"; +export type { ModuleOptions } from "./types"; export default defineNuxtModule({ meta: { name: libraryName, - configKey: 'elementPlus', + configKey: "elementPlus", compatibility: { - nuxt: '>=3' - } + nuxt: ">=3", + }, }, defaults, - setup (options, nuxt) { - const layers = getLayersDir(nuxt.options._layers) + onInstall() { + resolveBaseImports({} as ModuleOptions); + }, + setup(options, nuxt) { + const layers = getLayersDir(nuxt.options._layers); // disable the `cache` option when building applications if (!nuxt.options.dev) { - options.cache = false + options.cache = false; } - resolveOptions(options) - resolveThemes(options) - resolveBaseImports(options) - nuxt.options.imports.autoImport !== false && resolveImports(options) - nuxt.options.components !== false && resolveComponents(options) - options.cache && addTemplate(resolveCache(options)) - options.globalConfig && addPluginTemplate(resolveGlobalConfig(options)) - options.importStyle === 'scss' && options.themeChalk && resolveThemeChalk(options).map(addTemplate) + resolveOptions(options); + resolveThemes(options); + resolveBaseImports(options); + nuxt.options.imports.autoImport !== false && resolveImports(options); + nuxt.options.components !== false && resolveComponents(options); + options.cache && addTemplate(resolveCache(options)); + options.globalConfig && addPluginTemplate(resolveGlobalConfig(options)); + options.importStyle === "scss" && + options.themeChalk && + resolveThemeChalk(options).map(addTemplate); if (nuxt.options.ssr !== false) { - addPluginTemplate(resolveInjection(options)) - addPluginTemplate(resolveTeleports(options)) - options.installMethods.length && addPluginTemplate(resolveMethods(options)) + addPluginTemplate(resolveInjection(options)); + addPluginTemplate(resolveTeleports(options)); + options.installMethods.length && + addPluginTemplate(resolveMethods(options)); } - nuxt.hook('vite:extendConfig', (config, { isClient }) => { - const mode = isClient ? 'client' : 'server' - - config.plugins = config.plugins || [] - config.plugins.push(transformPlugin.vite({ - layers, - cache: options.cache, - include: options.include, - exclude: options.exclude, - sourcemap: nuxt.options.sourcemap[mode], - transformStyles: name => resolveStyles(options, name), - transformDirectives: name => resolveDirectives(options, name) - })) - - if (options.defaultLocale !== 'en') { - config.plugins.push(localePlugin.vite({ - sourcemap: nuxt.options.sourcemap[mode], - locale: options.defaultLocale - })) - } - }) - - nuxt.hook('webpack:config', (configs) => { - configs.forEach((config) => { - const mode = config.name === 'client' ? 'client' : 'server' + nuxt.hook("vite:extendConfig", (config, { isClient }) => { + const mode = isClient ? "client" : "server"; - config.plugins = config.plugins || [] - config.plugins.push(transformPlugin.webpack({ + config.plugins = config.plugins || []; + config.plugins.push( + transformPlugin.vite({ layers, cache: options.cache, include: options.include, exclude: options.exclude, sourcemap: nuxt.options.sourcemap[mode], - transformStyles: name => resolveStyles(options, name), - transformDirectives: name => resolveDirectives(options, name) - })) + transformStyles: (name) => resolveStyles(options, name), + transformDirectives: (name) => resolveDirectives(options, name), + }), + ); - if (options.defaultLocale !== 'en') { - config.plugins.push(localePlugin.webpack({ + if (options.defaultLocale !== "en") { + config.plugins.push( + localePlugin.vite({ sourcemap: nuxt.options.sourcemap[mode], - locale: options.defaultLocale - })) + locale: options.defaultLocale, + }), + ); + } + }); + + nuxt.hook("webpack:config", (configs) => { + configs.forEach((config) => { + const mode = config.name === "client" ? "client" : "server"; + + config.plugins = config.plugins || []; + config.plugins.push( + transformPlugin.webpack({ + layers, + cache: options.cache, + include: options.include, + exclude: options.exclude, + sourcemap: nuxt.options.sourcemap[mode], + transformStyles: (name) => resolveStyles(options, name), + transformDirectives: (name) => resolveDirectives(options, name), + }), + ); + + if (options.defaultLocale !== "en") { + config.plugins.push( + localePlugin.webpack({ + sourcemap: nuxt.options.sourcemap[mode], + locale: options.defaultLocale, + }), + ); } - }) - }) - } -}) + }); + }); + }, +}); diff --git a/src/utils.ts b/src/utils.ts index 8abc522..814fe95 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,97 +1,98 @@ -import type { Component } from 'vue' -import { createResolver } from '@nuxt/kit' -import type { NuxtConfigLayer } from '@nuxt/schema' -import { allIcons, libraryName } from './config' -import type { PresetComponent } from './types' - -export function resolvePath (path: string): Promise { - const { resolvePath } = createResolver(import.meta.url) - return resolvePath(path) +import type { Component } from "vue"; +import { createResolver } from "@nuxt/kit"; +import type { NuxtConfigLayer } from "@nuxt/schema"; +import { allIcons, libraryName } from "./config"; +import type { PresetComponent } from "./types"; + +export function resolvePath(path: string): Promise { + const { resolvePath } = createResolver(import.meta.url); + return resolvePath(path); } -export async function resolveComponentPath ( +export async function resolveComponentPath( path: string, - cache: boolean | undefined + cache: boolean | undefined, ): Promise { if (cache) { - return `#build/${libraryName}-cache.mjs` + return `#build/${libraryName}-cache.mjs`; } - return await resolvePath(`${libraryName}/${path}`) + return await resolvePath(`${libraryName}/${path}`); } -export function getLayersDir (layers: readonly NuxtConfigLayer[]) { - const list = [] +export function getLayersDir(layers: readonly NuxtConfigLayer[]) { + const list = []; for (const layer of layers) { - const srcDir = layer.config.srcDir || layer.cwd + const srcDir = layer.config.srcDir || layer.cwd; if ( - srcDir.includes('node_modules') && + srcDir.includes("node_modules") && + isObject(layer.config.elementPlus) && layer.config.elementPlus?.importStyle !== false ) { - list.push(srcDir) + list.push(srcDir); } } - return list + return list; } -export function isObject (value: any): value is Record { - return typeof value === 'object' && value !== null && !isArray(value) +export function isObject(value: any): value is Record { + return typeof value === "object" && value !== null && !isArray(value); } -export function isFunction (value: any): value is Function { - return typeof value === 'function' +export function isFunction(value: any): value is Function { + return typeof value === "function"; } -export function isArray (value: any): value is any[] { - return Array.isArray(value) +export function isArray(value: any): value is any[] { + return Array.isArray(value); } -export function isVueComponent (value: any): value is Component { +export function isVueComponent(value: any): value is Component { return ( - typeof value === 'object' && + typeof value === "object" && value.name && (value.props || value.emits || value.setup || value.render) - ) + ); } -export function toArray ( - value: T +export function toArray( + value: T, ): T extends any[] ? T : T[] { - return (isArray(value) ? value : [value]) as any + return (isArray(value) ? value : [value]) as any; } -export function toRegExp (arr: string[], flags?: string): RegExp { - return new RegExp(`\\b(${arr.join('|')})\\b`, flags) +export function toRegExp(arr: string[], flags?: string): RegExp { + return new RegExp(`\\b(${arr.join("|")})\\b`, flags); } -export async function genLibraryImport ( +export async function genLibraryImport( [name, as, from]: Required>, - cache: boolean | undefined + cache: boolean | undefined, ): Promise { - const fromPath = await resolveComponentPath(from, cache) - return `import { ${name} as ${as} } from '${fromPath}';\n` + const fromPath = await resolveComponentPath(from, cache); + return `import { ${name} as ${as} } from '${fromPath}';\n`; } -export async function genSideEffectsImport (from: string): Promise { - const fromPath = await resolvePath(from) - return `import '${fromPath}';\n` +export async function genSideEffectsImport(from: string): Promise { + const fromPath = await resolvePath(from); + return `import '${fromPath}';\n`; } -export function genIconPresets ( +export function genIconPresets( prefix: string, - from?: string + from?: string, ): Exclude[] { return allIcons.map((name) => { - return [name, `${prefix}${name}`, from] as Exclude - }) + return [name, `${prefix}${name}`, from] as Exclude; + }); } -export function camelize (value: string): string { - return value.replace(/(^|-)(\w)/g, (a, b, c) => c.toUpperCase()) +export function camelize(value: string): string { + return value.replace(/(^|-)(\w)/g, (a, b, c) => c.toUpperCase()); } -export function hyphenate (value: string): string { - return value.replace(/\B([A-Z])/g, '-$1').toLowerCase() +export function hyphenate(value: string): string { + return value.replace(/\B([A-Z])/g, "-$1").toLowerCase(); } From 7dc144da1c8d1ae6a1317993470fa54dc3e8d33b Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 23 Jan 2026 14:59:41 +0800 Subject: [PATCH 2/2] style: lint --- src/module.ts | 108 +++++++++++++++++++++++++------------------------- src/utils.ts | 96 ++++++++++++++++++++++---------------------- 2 files changed, 102 insertions(+), 102 deletions(-) diff --git a/src/module.ts b/src/module.ts index 6f0cfb3..a2def66 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,5 @@ -import { addPluginTemplate, addTemplate, defineNuxtModule } from "@nuxt/kit"; -import { defaults, libraryName } from "./config"; +import { addPluginTemplate, addTemplate, defineNuxtModule } from '@nuxt/kit' +import { defaults, libraryName } from './config' import { resolveCache, resolveComponents, @@ -15,54 +15,54 @@ import { resolveThemes, resolveMethods, transformPlugin, - localePlugin, -} from "./core/index"; -import { getLayersDir } from "./utils"; -import type { ModuleOptions } from "./types"; -export type { ModuleOptions } from "./types"; + localePlugin +} from './core/index' +import { getLayersDir } from './utils' +import type { ModuleOptions } from './types' +export type { ModuleOptions } from './types' export default defineNuxtModule({ meta: { name: libraryName, - configKey: "elementPlus", + configKey: 'elementPlus', compatibility: { - nuxt: ">=3", - }, + nuxt: '>=3' + } }, defaults, - onInstall() { - resolveBaseImports({} as ModuleOptions); + onInstall () { + resolveBaseImports({} as ModuleOptions) }, - setup(options, nuxt) { - const layers = getLayersDir(nuxt.options._layers); + setup (options, nuxt) { + const layers = getLayersDir(nuxt.options._layers) // disable the `cache` option when building applications if (!nuxt.options.dev) { - options.cache = false; + options.cache = false } - resolveOptions(options); - resolveThemes(options); - resolveBaseImports(options); - nuxt.options.imports.autoImport !== false && resolveImports(options); - nuxt.options.components !== false && resolveComponents(options); - options.cache && addTemplate(resolveCache(options)); - options.globalConfig && addPluginTemplate(resolveGlobalConfig(options)); - options.importStyle === "scss" && + resolveOptions(options) + resolveThemes(options) + resolveBaseImports(options) + nuxt.options.imports.autoImport !== false && resolveImports(options) + nuxt.options.components !== false && resolveComponents(options) + options.cache && addTemplate(resolveCache(options)) + options.globalConfig && addPluginTemplate(resolveGlobalConfig(options)) + options.importStyle === 'scss' && options.themeChalk && - resolveThemeChalk(options).map(addTemplate); + resolveThemeChalk(options).map(addTemplate) if (nuxt.options.ssr !== false) { - addPluginTemplate(resolveInjection(options)); - addPluginTemplate(resolveTeleports(options)); + addPluginTemplate(resolveInjection(options)) + addPluginTemplate(resolveTeleports(options)) options.installMethods.length && - addPluginTemplate(resolveMethods(options)); + addPluginTemplate(resolveMethods(options)) } - nuxt.hook("vite:extendConfig", (config, { isClient }) => { - const mode = isClient ? "client" : "server"; + nuxt.hook('vite:extendConfig', (config, { isClient }) => { + const mode = isClient ? 'client' : 'server' - config.plugins = config.plugins || []; + config.plugins = config.plugins || [] config.plugins.push( transformPlugin.vite({ layers, @@ -70,26 +70,26 @@ export default defineNuxtModule({ include: options.include, exclude: options.exclude, sourcemap: nuxt.options.sourcemap[mode], - transformStyles: (name) => resolveStyles(options, name), - transformDirectives: (name) => resolveDirectives(options, name), - }), - ); + transformStyles: name => resolveStyles(options, name), + transformDirectives: name => resolveDirectives(options, name) + }) + ) - if (options.defaultLocale !== "en") { + if (options.defaultLocale !== 'en') { config.plugins.push( localePlugin.vite({ sourcemap: nuxt.options.sourcemap[mode], - locale: options.defaultLocale, - }), - ); + locale: options.defaultLocale + }) + ) } - }); + }) - nuxt.hook("webpack:config", (configs) => { + nuxt.hook('webpack:config', (configs) => { configs.forEach((config) => { - const mode = config.name === "client" ? "client" : "server"; + const mode = config.name === 'client' ? 'client' : 'server' - config.plugins = config.plugins || []; + config.plugins = config.plugins || [] config.plugins.push( transformPlugin.webpack({ layers, @@ -97,20 +97,20 @@ export default defineNuxtModule({ include: options.include, exclude: options.exclude, sourcemap: nuxt.options.sourcemap[mode], - transformStyles: (name) => resolveStyles(options, name), - transformDirectives: (name) => resolveDirectives(options, name), - }), - ); + transformStyles: name => resolveStyles(options, name), + transformDirectives: name => resolveDirectives(options, name) + }) + ) - if (options.defaultLocale !== "en") { + if (options.defaultLocale !== 'en') { config.plugins.push( localePlugin.webpack({ sourcemap: nuxt.options.sourcemap[mode], - locale: options.defaultLocale, - }), - ); + locale: options.defaultLocale + }) + ) } - }); - }); - }, -}); + }) + }) + } +}) diff --git a/src/utils.ts b/src/utils.ts index 814fe95..f3bf9b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,98 +1,98 @@ -import type { Component } from "vue"; -import { createResolver } from "@nuxt/kit"; -import type { NuxtConfigLayer } from "@nuxt/schema"; -import { allIcons, libraryName } from "./config"; -import type { PresetComponent } from "./types"; - -export function resolvePath(path: string): Promise { - const { resolvePath } = createResolver(import.meta.url); - return resolvePath(path); +import type { Component } from 'vue' +import { createResolver } from '@nuxt/kit' +import type { NuxtConfigLayer } from '@nuxt/schema' +import { allIcons, libraryName } from './config' +import type { PresetComponent } from './types' + +export function resolvePath (path: string): Promise { + const { resolvePath } = createResolver(import.meta.url) + return resolvePath(path) } -export async function resolveComponentPath( +export async function resolveComponentPath ( path: string, - cache: boolean | undefined, + cache: boolean | undefined ): Promise { if (cache) { - return `#build/${libraryName}-cache.mjs`; + return `#build/${libraryName}-cache.mjs` } - return await resolvePath(`${libraryName}/${path}`); + return await resolvePath(`${libraryName}/${path}`) } -export function getLayersDir(layers: readonly NuxtConfigLayer[]) { - const list = []; +export function getLayersDir (layers: readonly NuxtConfigLayer[]) { + const list = [] for (const layer of layers) { - const srcDir = layer.config.srcDir || layer.cwd; + const srcDir = layer.config.srcDir || layer.cwd if ( - srcDir.includes("node_modules") && + srcDir.includes('node_modules') && isObject(layer.config.elementPlus) && layer.config.elementPlus?.importStyle !== false ) { - list.push(srcDir); + list.push(srcDir) } } - return list; + return list } -export function isObject(value: any): value is Record { - return typeof value === "object" && value !== null && !isArray(value); +export function isObject (value: any): value is Record { + return typeof value === 'object' && value !== null && !isArray(value) } -export function isFunction(value: any): value is Function { - return typeof value === "function"; +export function isFunction (value: any): value is Function { + return typeof value === 'function' } -export function isArray(value: any): value is any[] { - return Array.isArray(value); +export function isArray (value: any): value is any[] { + return Array.isArray(value) } -export function isVueComponent(value: any): value is Component { +export function isVueComponent (value: any): value is Component { return ( - typeof value === "object" && + typeof value === 'object' && value.name && (value.props || value.emits || value.setup || value.render) - ); + ) } -export function toArray( - value: T, +export function toArray ( + value: T ): T extends any[] ? T : T[] { - return (isArray(value) ? value : [value]) as any; + return (isArray(value) ? value : [value]) as any } -export function toRegExp(arr: string[], flags?: string): RegExp { - return new RegExp(`\\b(${arr.join("|")})\\b`, flags); +export function toRegExp (arr: string[], flags?: string): RegExp { + return new RegExp(`\\b(${arr.join('|')})\\b`, flags) } -export async function genLibraryImport( +export async function genLibraryImport ( [name, as, from]: Required>, - cache: boolean | undefined, + cache: boolean | undefined ): Promise { - const fromPath = await resolveComponentPath(from, cache); - return `import { ${name} as ${as} } from '${fromPath}';\n`; + const fromPath = await resolveComponentPath(from, cache) + return `import { ${name} as ${as} } from '${fromPath}';\n` } -export async function genSideEffectsImport(from: string): Promise { - const fromPath = await resolvePath(from); - return `import '${fromPath}';\n`; +export async function genSideEffectsImport (from: string): Promise { + const fromPath = await resolvePath(from) + return `import '${fromPath}';\n` } -export function genIconPresets( +export function genIconPresets ( prefix: string, - from?: string, + from?: string ): Exclude[] { return allIcons.map((name) => { - return [name, `${prefix}${name}`, from] as Exclude; - }); + return [name, `${prefix}${name}`, from] as Exclude + }) } -export function camelize(value: string): string { - return value.replace(/(^|-)(\w)/g, (a, b, c) => c.toUpperCase()); +export function camelize (value: string): string { + return value.replace(/(^|-)(\w)/g, (a, b, c) => c.toUpperCase()) } -export function hyphenate(value: string): string { - return value.replace(/\B([A-Z])/g, "-$1").toLowerCase(); +export function hyphenate (value: string): string { + return value.replace(/\B([A-Z])/g, '-$1').toLowerCase() }