chore: export components declaration (#2189)

* chore: export components declaration

* fix: update

* fix: update typing

* fix: update components

* Update demo/pages/docs/usage-sfc/enUS/index.md

* fix: remove themeEditor

Co-authored-by: yugang.cao <yugang.cao@tusimple.ai>
Co-authored-by: 07akioni <07akioni2@gmail.com>
This commit is contained in:
Yugang Cao 2022-01-23 16:00:40 +08:00 committed by GitHub
parent 9b9ab497cc
commit a4fa34f4ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 214 additions and 201 deletions

View File

@ -62,3 +62,17 @@ After the installation. You can use all the components in you SFC like this.
<n-button>naive-ui</n-button>
</template>
```
### Volar support
If you are using Volar, you can specify global component types by configure `compilerOptions.types` in `tsconfig.json`.
```json
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["naive-ui/components"]
}
}
```

View File

@ -62,3 +62,17 @@ app.use(naive)
<n-button>naive-ui</n-button>
</template>
```
### Volar 支持
如果你已经使用了 Volar, 那么需要在 `tsconfig.json` 中配置 `compilerOptions.types` 来指定全局组件类型
```json
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["naive-ui/components"]
}
}
```

View File

@ -6,8 +6,8 @@
"module": "es/index.js",
"scripts": {
"start": "pnpm run dev",
"dev": "pnpm run clean && pnpm run gen-version && cross-env NODE_ENV=development vite",
"build:package": "pnpm run gen-version && pnpm run clean && tsc -b --force tsconfig.esm.json && node scripts/pre-build/pre-cjs-build.js && tsc -b --force tsconfig.cjs.json && node scripts/post-build && rimraf {es,lib}/*.tsbuildinfo",
"dev": "pnpm run clean && pnpm run gen-version && pnpm run gen-component-declaration && cross-env NODE_ENV=development vite",
"build:package": "pnpm run gen-version && pnpm run clean && pnpm run gen-component-declaration && tsc -b --force tsconfig.esm.json && node scripts/pre-build/pre-cjs-build.js && tsc -b --force tsconfig.cjs.json && node scripts/post-build && rimraf {es,lib}/*.tsbuildinfo",
"build:site": "./scripts/pre-build-site/pre-build-site.sh && cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=4096 vite build && ./scripts/post-build-site/post-build-site.sh",
"clean": "rimraf site lib es node_modules/naive-ui themes/**/es themes/**/lib",
"release:package": "pnpm run test && pnpm run build:package && pnpm publish --no-git-checks",
@ -38,7 +38,8 @@
"es",
"lib",
"web-types.json",
"README.md"
"README.md",
"typings/components.d.ts"
],
"web-types": "./web-types.json",
"lint-staged": {

View File

@ -1,22 +1,13 @@
const { resolve, join } = require('path')
const fs = require('fs-extra')
const fg = require('fast-glob')
import * as globalComponents from '../src/components'
import { resolve } from 'path'
import fs from 'fs-extra'
const COMPONENT_ROOT = resolve(__dirname, '../src')
const excludeDir = ['global-style', 'themes', 'composables', 'locales']
const TYPE_ROOT = resolve(__dirname, '../typings')
function exist (path) {
return fs.existsSync(path)
}
async function loadComponent (dir) {
return {
path: dir,
name: `n-${dir}`,
components: exist(join(COMPONENT_ROOT, dir, 'index.ts')) ? await require(join(COMPONENT_ROOT, dir, 'index.ts')) : {}
}
}
function parseComponentsDeclaration (code) {
if (!code) {
return {}
@ -29,30 +20,18 @@ function parseComponentsDeclaration (code) {
}
async function generateComponentsType () {
const folders = (await fg('[^_]*', {
onlyDirectories: true,
cwd: COMPONENT_ROOT
})).filter(fold => !excludeDir.includes(fold))
const files = await Promise.all(
folders.map(async dir => await loadComponent(dir))
)
const components = {}
files.forEach((file) => {
const fileComponents = file.components
Object.keys(fileComponents).forEach((key) => {
const entry = `typeof import('./${file.path}')['${key}']`
if (key.startsWith('N')) {
components[key] = entry
}
})
Object.keys(globalComponents).forEach((key) => {
const entry = `typeof import('naive-ui')['${key}']`
if (key.startsWith('N')) {
components[key] = entry
}
})
const originalContent = exist(
resolve(COMPONENT_ROOT, 'components-declaration.d.ts')
resolve(TYPE_ROOT, 'components.d.ts')
)
? await fs.readFile(
resolve(COMPONENT_ROOT, 'components-declaration.d.ts'),
resolve(TYPE_ROOT, 'components.d.ts'),
'utf-8'
)
: ''
@ -82,7 +61,7 @@ export { }
`
if (code !== originalContent) {
await fs.writeFile(
resolve(COMPONENT_ROOT, 'components-declaration.d.ts'),
resolve(TYPE_ROOT, 'components.d.ts'),
code,
'utf-8'
)

View File

@ -1,143 +0,0 @@
// this is global component declaration
declare module 'vue' {
export interface GlobalComponents {
NAffix: typeof import('./affix')['NAffix']
NAlert: typeof import('./alert')['NAlert']
NAnchor: typeof import('./anchor')['NAnchor']
NAnchorLink: typeof import('./anchor')['NAnchorLink']
NAutoComplete: typeof import('./auto-complete')['NAutoComplete']
NAvatar: typeof import('./avatar')['NAvatar']
NAvatarGroup: typeof import('./avatar')['NAvatarGroup']
NBackTop: typeof import('./back-top')['NBackTop']
NBadge: typeof import('./badge')['NBadge']
NBreadcrumb: typeof import('./breadcrumb')['NBreadcrumb']
NBreadcrumbItem: typeof import('./breadcrumb')['NBreadcrumbItem']
NButton: typeof import('./button')['NButton']
NButtonGroup: typeof import('./button')['NButtonGroup']
NxButton: typeof import('./button')['NxButton']
NCalendar: typeof import('./calendar')['NCalendar']
NCard: typeof import('./card')['NCard']
NCarousel: typeof import('./carousel')['NCarousel']
NCascader: typeof import('./cascader')['NCascader']
NCheckbox: typeof import('./checkbox')['NCheckbox']
NCheckboxGroup: typeof import('./checkbox')['NCheckboxGroup']
NCode: typeof import('./code')['NCode']
NCollapse: typeof import('./collapse')['NCollapse']
NCollapseItem: typeof import('./collapse')['NCollapseItem']
NCollapseTransition: typeof import('./collapse-transition')['NCollapseTransition']
NColorPicker: typeof import('./color-picker')['NColorPicker']
NConfigProvider: typeof import('./config-provider')['NConfigProvider']
NCountdown: typeof import('./countdown')['NCountdown']
NDataTable: typeof import('./data-table')['NDataTable']
NDatePicker: typeof import('./date-picker')['NDatePicker']
NDescriptions: typeof import('./descriptions')['NDescriptions']
NDescriptionsItem: typeof import('./descriptions')['NDescriptionsItem']
NDialog: typeof import('./dialog')['NDialog']
NDialogProvider: typeof import('./dialog')['NDialogProvider']
NDivider: typeof import('./divider')['NDivider']
NDrawer: typeof import('./drawer')['NDrawer']
NDrawerContent: typeof import('./drawer')['NDrawerContent']
NDropdown: typeof import('./dropdown')['NDropdown']
NDynamicInput: typeof import('./dynamic-input')['NDynamicInput']
NDynamicTags: typeof import('./dynamic-tags')['NDynamicTags']
NEl: typeof import('./element')['NEl']
NElement: typeof import('./element')['NElement']
NEllipsis: typeof import('./ellipsis')['NEllipsis']
NEmpty: typeof import('./empty')['NEmpty']
NForm: typeof import('./form')['NForm']
NFormItem: typeof import('./form')['NFormItem']
NFormItemCol: typeof import('./form')['NFormItemCol']
NFormItemGi: typeof import('./form')['NFormItemGi']
NFormItemGridItem: typeof import('./form')['NFormItemGridItem']
NFormItemRow: typeof import('./form')['NFormItemRow']
NGradientText: typeof import('./gradient-text')['NGradientText']
NGi: typeof import('./grid')['NGi']
NGrid: typeof import('./grid')['NGrid']
NGridItem: typeof import('./grid')['NGridItem']
NIcon: typeof import('./icon')['NIcon']
NImage: typeof import('./image')['NImage']
NImageGroup: typeof import('./image')['NImageGroup']
NInput: typeof import('./input')['NInput']
NInputGroup: typeof import('./input')['NInputGroup']
NInputGroupLabel: typeof import('./input')['NInputGroupLabel']
NInputNumber: typeof import('./input-number')['NInputNumber']
NLayout: typeof import('./layout')['NLayout']
NLayoutContent: typeof import('./layout')['NLayoutContent']
NLayoutFooter: typeof import('./layout')['NLayoutFooter']
NLayoutHeader: typeof import('./layout')['NLayoutHeader']
NLayoutSider: typeof import('./layout')['NLayoutSider']
NCol: typeof import('./legacy-grid')['NCol']
NRow: typeof import('./legacy-grid')['NRow']
NList: typeof import('./list')['NList']
NListItem: typeof import('./list')['NListItem']
NLoadingBarProvider: typeof import('./loading-bar')['NLoadingBarProvider']
NLog: typeof import('./log')['NLog']
NMention: typeof import('./mention')['NMention']
NMenu: typeof import('./menu')['NMenu']
NMessageProvider: typeof import('./message')['NMessageProvider']
NModal: typeof import('./modal')['NModal']
NNotificationProvider: typeof import('./notification')['NNotificationProvider']
NNumberAnimation: typeof import('./number-animation')['NNumberAnimation']
NPageHeader: typeof import('./page-header')['NPageHeader']
NPagination: typeof import('./pagination')['NPagination']
NPopconfirm: typeof import('./popconfirm')['NPopconfirm']
NPopover: typeof import('./popover')['NPopover']
NPopselect: typeof import('./popselect')['NPopselect']
NProgress: typeof import('./progress')['NProgress']
NRadio: typeof import('./radio')['NRadio']
NRadioButton: typeof import('./radio')['NRadioButton']
NRadioGroup: typeof import('./radio')['NRadioGroup']
NRate: typeof import('./rate')['NRate']
NResult: typeof import('./result')['NResult']
NScrollbar: typeof import('./scrollbar')['NScrollbar']
NSelect: typeof import('./select')['NSelect']
NSkeleton: typeof import('./skeleton')['NSkeleton']
NSlider: typeof import('./slider')['NSlider']
NSpace: typeof import('./space')['NSpace']
NSpin: typeof import('./spin')['NSpin']
NStatistic: typeof import('./statistic')['NStatistic']
NStep: typeof import('./steps')['NStep']
NSteps: typeof import('./steps')['NSteps']
NSwitch: typeof import('./switch')['NSwitch']
NTable: typeof import('./table')['NTable']
NTbody: typeof import('./table')['NTbody']
NTd: typeof import('./table')['NTd']
NTh: typeof import('./table')['NTh']
NThead: typeof import('./table')['NThead']
NTr: typeof import('./table')['NTr']
NTab: typeof import('./tabs')['NTab']
NTabPane: typeof import('./tabs')['NTabPane']
NTabs: typeof import('./tabs')['NTabs']
NTag: typeof import('./tag')['NTag']
NThemeEditor: typeof import('./theme-editor')['NThemeEditor']
NThing: typeof import('./thing')['NThing']
NTime: typeof import('./time')['NTime']
NTimePicker: typeof import('./time-picker')['NTimePicker']
NTimeline: typeof import('./timeline')['NTimeline']
NTimelineItem: typeof import('./timeline')['NTimelineItem']
NTooltip: typeof import('./tooltip')['NTooltip']
NTransfer: typeof import('./transfer')['NTransfer']
NTree: typeof import('./tree')['NTree']
NTreeSelect: typeof import('./tree-select')['NTreeSelect']
NA: typeof import('./typography')['NA']
NBlockquote: typeof import('./typography')['NBlockquote']
NH1: typeof import('./typography')['NH1']
NH2: typeof import('./typography')['NH2']
NH3: typeof import('./typography')['NH3']
NH4: typeof import('./typography')['NH4']
NH5: typeof import('./typography')['NH5']
NH6: typeof import('./typography')['NH6']
NHr: typeof import('./typography')['NHr']
NLi: typeof import('./typography')['NLi']
NOl: typeof import('./typography')['NOl']
NP: typeof import('./typography')['NP']
NText: typeof import('./typography')['NText']
NUl: typeof import('./typography')['NUl']
NUpload: typeof import('./upload')['NUpload']
NUploadDragger: typeof import('./upload')['NUploadDragger']
NUploadFileList: typeof import('./upload')['NUploadFileList']
NUploadTrigger: typeof import('./upload')['NUploadTrigger']
[key: string]: any
}
}
export { }

View File

@ -1,16 +1,18 @@
<markdown>
# Icon
The content of the default slot will be used as the icon.
</markdown>
```html
<n-rate>
<n-icon size="20">
<cash-icon />
</n-icon>
</n-rate>
```
<template>
<n-rate>
<n-icon size="20">
<cash-icon />
</n-icon>
</n-rate>
</template>
```js
<script lang="ts">
import { CashOutline as CashIcon } from '@vicons/ionicons5'
import { defineComponent } from 'vue'
@ -19,4 +21,4 @@ export default defineComponent({
CashIcon
}
})
```
</script>

View File

@ -8,7 +8,7 @@ Hint: If you are not very confident, think twice before changing the star's colo
basic
size
color
icon
icon.vue
allow-half
readonly
```
@ -30,6 +30,6 @@ readonly
### Rate Slots
| Name | Parameters | Description |
| ------- | ---------- | --------------------- |
| Name | Parameters | Description |
| ------- | ---------- | ----------------------- |
| default | `()` | The icon of the rating. |

View File

@ -1,16 +1,18 @@
<markdown>
# 图标
默认插槽的内容会被用作图标
</markdown>
```html
<n-rate>
<n-icon size="20">
<cash-icon />
</n-icon>
</n-rate>
```
<template>
<n-rate>
<n-icon size="20">
<cash-icon />
</n-icon>
</n-rate>
</template>
```js
<script lang="ts">
import { CashOutline as CashIcon } from '@vicons/ionicons5'
import { defineComponent } from 'vue'
@ -19,4 +21,4 @@ export default defineComponent({
CashIcon
}
})
```
</script>

View File

@ -8,7 +8,7 @@
basic
size
color
icon
icon.vue
allow-half
readonly
```

144
typings/components.d.ts vendored Normal file
View File

@ -0,0 +1,144 @@
// this is global component declaration
declare module 'vue' {
export interface GlobalComponents {
NAffix: typeof import('naive-ui')['NAffix']
NAlert: typeof import('naive-ui')['NAlert']
NAnchor: typeof import('naive-ui')['NAnchor']
NAnchorLink: typeof import('naive-ui')['NAnchorLink']
NAutoComplete: typeof import('naive-ui')['NAutoComplete']
NAvatar: typeof import('naive-ui')['NAvatar']
NAvatarGroup: typeof import('naive-ui')['NAvatarGroup']
NBackTop: typeof import('naive-ui')['NBackTop']
NBadge: typeof import('naive-ui')['NBadge']
NBreadcrumb: typeof import('naive-ui')['NBreadcrumb']
NBreadcrumbItem: typeof import('naive-ui')['NBreadcrumbItem']
NButton: typeof import('naive-ui')['NButton']
NButtonGroup: typeof import('naive-ui')['NButtonGroup']
NxButton: typeof import('naive-ui')['NxButton']
NCalendar: typeof import('naive-ui')['NCalendar']
NColorPicker: typeof import('naive-ui')['NColorPicker']
NCard: typeof import('naive-ui')['NCard']
NCarousel: typeof import('naive-ui')['NCarousel']
NCarouselItem: typeof import('naive-ui')['NCarouselItem']
NCascader: typeof import('naive-ui')['NCascader']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup']
NCode: typeof import('naive-ui')['NCode']
NCollapse: typeof import('naive-ui')['NCollapse']
NCollapseItem: typeof import('naive-ui')['NCollapseItem']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NCountdown: typeof import('naive-ui')['NCountdown']
NNumberAnimation: typeof import('naive-ui')['NNumberAnimation']
NDataTable: typeof import('naive-ui')['NDataTable']
NDatePicker: typeof import('naive-ui')['NDatePicker']
NDescriptions: typeof import('naive-ui')['NDescriptions']
NDescriptionsItem: typeof import('naive-ui')['NDescriptionsItem']
NDialog: typeof import('naive-ui')['NDialog']
NDialogProvider: typeof import('naive-ui')['NDialogProvider']
NDivider: typeof import('naive-ui')['NDivider']
NDrawer: typeof import('naive-ui')['NDrawer']
NDrawerContent: typeof import('naive-ui')['NDrawerContent']
NDropdown: typeof import('naive-ui')['NDropdown']
NDynamicInput: typeof import('naive-ui')['NDynamicInput']
NDynamicTags: typeof import('naive-ui')['NDynamicTags']
NEl: typeof import('naive-ui')['NEl']
NElement: typeof import('naive-ui')['NElement']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NEmpty: typeof import('naive-ui')['NEmpty']
NForm: typeof import('naive-ui')['NForm']
NFormItem: typeof import('naive-ui')['NFormItem']
NFormItemCol: typeof import('naive-ui')['NFormItemCol']
NFormItemGi: typeof import('naive-ui')['NFormItemGi']
NFormItemGridItem: typeof import('naive-ui')['NFormItemGridItem']
NFormItemRow: typeof import('naive-ui')['NFormItemRow']
NGlobalStyle: typeof import('naive-ui')['NGlobalStyle']
NGradientText: typeof import('naive-ui')['NGradientText']
NGi: typeof import('naive-ui')['NGi']
NGrid: typeof import('naive-ui')['NGrid']
NGridItem: typeof import('naive-ui')['NGridItem']
NIcon: typeof import('naive-ui')['NIcon']
NImage: typeof import('naive-ui')['NImage']
NImageGroup: typeof import('naive-ui')['NImageGroup']
NInput: typeof import('naive-ui')['NInput']
NInputGroup: typeof import('naive-ui')['NInputGroup']
NInputGroupLabel: typeof import('naive-ui')['NInputGroupLabel']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutContent: typeof import('naive-ui')['NLayoutContent']
NLayoutFooter: typeof import('naive-ui')['NLayoutFooter']
NLayoutHeader: typeof import('naive-ui')['NLayoutHeader']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NList: typeof import('naive-ui')['NList']
NListItem: typeof import('naive-ui')['NListItem']
NLoadingBarProvider: typeof import('naive-ui')['NLoadingBarProvider']
NLog: typeof import('naive-ui')['NLog']
NMenu: typeof import('naive-ui')['NMenu']
NMention: typeof import('naive-ui')['NMention']
NMessageProvider: typeof import('naive-ui')['NMessageProvider']
NModal: typeof import('naive-ui')['NModal']
NNotificationProvider: typeof import('naive-ui')['NNotificationProvider']
NPageHeader: typeof import('naive-ui')['NPageHeader']
NPagination: typeof import('naive-ui')['NPagination']
NPopconfirm: typeof import('naive-ui')['NPopconfirm']
NPopover: typeof import('naive-ui')['NPopover']
NPopselect: typeof import('naive-ui')['NPopselect']
NProgress: typeof import('naive-ui')['NProgress']
NRadio: typeof import('naive-ui')['NRadio']
NRadioButton: typeof import('naive-ui')['NRadioButton']
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
NRate: typeof import('naive-ui')['NRate']
NResult: typeof import('naive-ui')['NResult']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSelect: typeof import('naive-ui')['NSelect']
NSkeleton: typeof import('naive-ui')['NSkeleton']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin']
NStatistic: typeof import('naive-ui')['NStatistic']
NStep: typeof import('naive-ui')['NStep']
NSteps: typeof import('naive-ui')['NSteps']
NSwitch: typeof import('naive-ui')['NSwitch']
NTable: typeof import('naive-ui')['NTable']
NTbody: typeof import('naive-ui')['NTbody']
NTd: typeof import('naive-ui')['NTd']
NTh: typeof import('naive-ui')['NTh']
NThead: typeof import('naive-ui')['NThead']
NTr: typeof import('naive-ui')['NTr']
NTab: typeof import('naive-ui')['NTab']
NTabPane: typeof import('naive-ui')['NTabPane']
NTabs: typeof import('naive-ui')['NTabs']
NTag: typeof import('naive-ui')['NTag']
NThing: typeof import('naive-ui')['NThing']
NTime: typeof import('naive-ui')['NTime']
NTimePicker: typeof import('naive-ui')['NTimePicker']
NTimeline: typeof import('naive-ui')['NTimeline']
NTimelineItem: typeof import('naive-ui')['NTimelineItem']
NTooltip: typeof import('naive-ui')['NTooltip']
NTransfer: typeof import('naive-ui')['NTransfer']
NTree: typeof import('naive-ui')['NTree']
NTreeSelect: typeof import('naive-ui')['NTreeSelect']
NA: typeof import('naive-ui')['NA']
NBlockquote: typeof import('naive-ui')['NBlockquote']
NH1: typeof import('naive-ui')['NH1']
NH2: typeof import('naive-ui')['NH2']
NH3: typeof import('naive-ui')['NH3']
NH4: typeof import('naive-ui')['NH4']
NH5: typeof import('naive-ui')['NH5']
NH6: typeof import('naive-ui')['NH6']
NHr: typeof import('naive-ui')['NHr']
NLi: typeof import('naive-ui')['NLi']
NOl: typeof import('naive-ui')['NOl']
NP: typeof import('naive-ui')['NP']
NText: typeof import('naive-ui')['NText']
NUl: typeof import('naive-ui')['NUl']
NUpload: typeof import('naive-ui')['NUpload']
NUploadDragger: typeof import('naive-ui')['NUploadDragger']
NUploadFileList: typeof import('naive-ui')['NUploadFileList']
NUploadTrigger: typeof import('naive-ui')['NUploadTrigger']
NCol: typeof import('naive-ui')['NCol']
NRow: typeof import('naive-ui')['NRow']
[key: string]: any
}
}
export {}