From 38807d1f76fc7580bd35d79a0614cb3657f3c1f8 Mon Sep 17 00:00:00 2001 From: Kedejin <8087790+nongshell@users.noreply.github.com> Date: Sun, 15 Feb 2026 22:36:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(module):=20=E4=BC=98=E5=8C=96=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=92=8C=E5=9B=BE=E6=A0=87=E6=94=AF=E6=8C=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=8A=E6=B3=A8=E5=86=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为模块增加 component 选项支持,默认启用组件 - 调整 icon 选项默认值为 false,区分组件和图标的启用逻辑 - 优化组件和图标过滤注册流程,支持 include 和 exclude 规则 - 仅当组件启用时添加插件和服务端插件 - 配置文件中新增 antd.component 开关支持 - playground 示例中补充并调整图标使用和布局 - 修正测试路径变量为基于 URL 的动态路径,提升兼容性 --- .../components/basic-demos/icon/demo.vue | 11 +-- playground/nuxt.config.ts | 1 + src/module.ts | 90 ++++++++++++------- test/hydration.scan.test.ts | 2 +- 4 files changed, 66 insertions(+), 38 deletions(-) diff --git a/playground/components/basic-demos/icon/demo.vue b/playground/components/basic-demos/icon/demo.vue index 2016246..1089fb7 100644 --- a/playground/components/basic-demos/icon/demo.vue +++ b/playground/components/basic-demos/icon/demo.vue @@ -1,20 +1,21 @@ diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 48020cb..665fdc7 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -7,5 +7,6 @@ export default defineNuxtConfig({ compatibilityDate: 'latest', antd: { icon: true, + component: true, }, }) diff --git a/src/module.ts b/src/module.ts index 0610c53..93ed572 100644 --- a/src/module.ts +++ b/src/module.ts @@ -7,6 +7,15 @@ import icons from './runtime/icons' // Module options TypeScript interface definition export interface ModuleOptions { + /** + * Enable components + * @default true + */ + component?: boolean + /** + * Enable icons + * @default false + */ icon?: boolean /** * Components to be included or excluded @@ -43,9 +52,15 @@ export default defineNuxtModule({ // Default configuration options of the Nuxt module defaults: { icon: false, + component: true, prefix: 'A', }, setup(_options, _nuxt) { + // Skip if both components and icons are disabled + if (_options.component === false && _options.icon !== true) { + return + } + const transpileList = _nuxt.options.build.transpile const appendTranspile = (dep: string) => { if (!transpileList.includes(dep)) { @@ -55,38 +70,45 @@ export default defineNuxtModule({ // Keep icon definition modules in Nuxt transform pipeline to avoid // cold-start interop inconsistency in dev SSR/hydration. - appendTranspile(libName) + if (_options.component !== false) { + appendTranspile(libName) + } + + // Always transpile icon libs because users may import icons directly appendTranspile(iconLibName) appendTranspile(iconsSvgLibName) - const componentMap = { - QRCode: 'Qrcode', - } - // Filter components based on include/exclude options - const filteredComponents = components.filter((comp) => { - if (_options.include?.length) { - return _options.include.includes(comp) + // Register components + if (_options.component !== false) { + const componentMap = { + QRCode: 'Qrcode', } - if (_options.exclude?.length) { - return !_options.exclude.includes(comp) - } - return true - }) + // Filter components based on include/exclude options + const filteredComponents = components.filter((comp) => { + if (_options.include?.length) { + return _options.include.includes(comp) + } + if (_options.exclude?.length) { + return !_options.exclude.includes(comp) + } + return true + }) - filteredComponents.forEach((comp) => { - let _comp: string = comp - if (comp in componentMap) { - _comp = componentMap[comp as keyof typeof componentMap] - } - addComponent({ - filePath: 'antdv-next', - export: comp, - name: _options.prefix + _comp, + filteredComponents.forEach((comp) => { + let _comp: string = comp + if (comp in componentMap) { + _comp = componentMap[comp as keyof typeof componentMap] + } + addComponent({ + filePath: 'antdv-next', + export: comp, + name: _options.prefix + _comp, + }) }) - }) + } - if (_options.icon !== false) { - appendTranspile(iconLibName) + // Register icons + if (_options.icon === true) { // Filter icons based on include/exclude options const filteredIcons = icons.filter((icon) => { if (_options.includeIcons?.length) { @@ -105,15 +127,19 @@ export default defineNuxtModule({ }) }) } - const resolver = createResolver(import.meta.url) - // Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack` - addPlugin(resolver.resolve('./runtime/plugin')) - addServerPlugin(resolver.resolve('./runtime/server')) + // Only add plugins when components are enabled + if (_options.component !== false) { + const resolver = createResolver(import.meta.url) - // Check if the builder is Vite - if (_nuxt.options.builder === '@nuxt/vite-builder') { - addVitePlugin(dayjs()) + // Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack` + addPlugin(resolver.resolve('./runtime/plugin')) + addServerPlugin(resolver.resolve('./runtime/server')) + + // Check if the builder is Vite + if (_nuxt.options.builder === '@nuxt/vite-builder') { + addVitePlugin(dayjs()) + } } }, }) diff --git a/test/hydration.scan.test.ts b/test/hydration.scan.test.ts index ff75bec..14eb532 100644 --- a/test/hydration.scan.test.ts +++ b/test/hydration.scan.test.ts @@ -197,7 +197,7 @@ interface ScanResult { runtimeErrors: string[] } -const SOURCE_COMPONENTS_DIR = '/Users/yanyu/workspace/gitea/antdv-next/antdv-next/playground/src/pages/components' +const SOURCE_COMPONENTS_DIR = new URL('../playground/pages/components', import.meta.url).pathname const sourceComponentSlugs = readdirSync(SOURCE_COMPONENTS_DIR, { withFileTypes: true }) .filter(item => item.isDirectory())