-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add volar.d.ts generation (#698)
* build: add volar.d.ts generation * docs: update volar support
- Loading branch information
Showing
4 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// The file is not designed to run directly. `cwd` should be project root. | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import process from 'process' | ||
|
||
const componentDirectory = './src/components'; | ||
|
||
// Components to be ignored for creating volar type | ||
const IGNORE = [ | ||
"FieldBody", | ||
"SliderThumb", | ||
"TableMobileSort", | ||
"TablePagination", | ||
"PaginationButton", | ||
"DatepickerTable", | ||
"DatepickerTableRow", | ||
"DatepickerMonth", | ||
"PickerWrapper", | ||
"NotificationNotice", | ||
]; | ||
|
||
const TYPE_ROOT = process.cwd() | ||
|
||
function getComponents(dir) { | ||
const files = fs.readdirSync(dir, { recursive: true }); | ||
return files | ||
// filter only vue files and remove test files | ||
.filter(f => f.includes(".vue") && !f.includes("tests")) | ||
// remove path | ||
.map(f => f.includes("\\") ? f.substring(f.lastIndexOf("\\") + 1) : f) | ||
// remove .vue suffix | ||
.map(f => f.substring(0, f.indexOf(".vue"))) | ||
// filter blacklist | ||
.filter((key) => !IGNORE.includes(key)) | ||
} | ||
|
||
function exist (path) { | ||
return fs.existsSync(path) | ||
} | ||
|
||
function generateComponentsType () { | ||
if(!exist(path.resolve(TYPE_ROOT, componentDirectory))) | ||
throw new Error("Path not exist: " + componentDirectory); | ||
|
||
const globalComponents = getComponents(componentDirectory); | ||
|
||
const components = {} | ||
globalComponents | ||
// add global O prefix | ||
.map((dir) => "O" + dir) | ||
// add type declaration | ||
.forEach((key) => { | ||
components[key] = `(typeof import("@oruga-ui/oruga-next"))["${key}"];`; | ||
}); | ||
|
||
const lines = Object.entries(components) | ||
.filter(([name]) => components[name]) | ||
.map(([name, v]) => { | ||
if (!/^\w+$/.test(name)) { | ||
name = `'${name}'` | ||
} | ||
return `${name}: ${v}` | ||
}); | ||
|
||
const code = `// Auto generated component declarations | ||
declare module "vue" { | ||
export interface GlobalComponents { | ||
${lines.join('\n ')} | ||
} | ||
} | ||
export {}; | ||
`; | ||
|
||
fs.writeFileSync(path.resolve(TYPE_ROOT, 'volar.d.ts'), code, 'utf-8') | ||
} | ||
|
||
generateComponentsType() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Auto generated component declarations | ||
declare module "vue" { | ||
export interface GlobalComponents { | ||
OAutocomplete: (typeof import("@oruga-ui/oruga-next"))["OAutocomplete"]; | ||
OButton: (typeof import("@oruga-ui/oruga-next"))["OButton"]; | ||
OCarousel: (typeof import("@oruga-ui/oruga-next"))["OCarousel"]; | ||
OCarouselItem: (typeof import("@oruga-ui/oruga-next"))["OCarouselItem"]; | ||
OCheckbox: (typeof import("@oruga-ui/oruga-next"))["OCheckbox"]; | ||
OCollapse: (typeof import("@oruga-ui/oruga-next"))["OCollapse"]; | ||
ODatepicker: (typeof import("@oruga-ui/oruga-next"))["ODatepicker"]; | ||
ODatetimepicker: (typeof import("@oruga-ui/oruga-next"))["ODatetimepicker"]; | ||
ODropdown: (typeof import("@oruga-ui/oruga-next"))["ODropdown"]; | ||
ODropdownItem: (typeof import("@oruga-ui/oruga-next"))["ODropdownItem"]; | ||
OField: (typeof import("@oruga-ui/oruga-next"))["OField"]; | ||
OIcon: (typeof import("@oruga-ui/oruga-next"))["OIcon"]; | ||
OInput: (typeof import("@oruga-ui/oruga-next"))["OInput"]; | ||
OLoading: (typeof import("@oruga-ui/oruga-next"))["OLoading"]; | ||
OMenu: (typeof import("@oruga-ui/oruga-next"))["OMenu"]; | ||
OMenuItem: (typeof import("@oruga-ui/oruga-next"))["OMenuItem"]; | ||
OModal: (typeof import("@oruga-ui/oruga-next"))["OModal"]; | ||
ONotification: (typeof import("@oruga-ui/oruga-next"))["ONotification"]; | ||
OPagination: (typeof import("@oruga-ui/oruga-next"))["OPagination"]; | ||
ORadio: (typeof import("@oruga-ui/oruga-next"))["ORadio"]; | ||
OSelect: (typeof import("@oruga-ui/oruga-next"))["OSelect"]; | ||
OSidebar: (typeof import("@oruga-ui/oruga-next"))["OSidebar"]; | ||
OSkeleton: (typeof import("@oruga-ui/oruga-next"))["OSkeleton"]; | ||
OSlider: (typeof import("@oruga-ui/oruga-next"))["OSlider"]; | ||
OSliderTick: (typeof import("@oruga-ui/oruga-next"))["OSliderTick"]; | ||
OStepItem: (typeof import("@oruga-ui/oruga-next"))["OStepItem"]; | ||
OSteps: (typeof import("@oruga-ui/oruga-next"))["OSteps"]; | ||
OSwitch: (typeof import("@oruga-ui/oruga-next"))["OSwitch"]; | ||
OTable: (typeof import("@oruga-ui/oruga-next"))["OTable"]; | ||
OTableColumn: (typeof import("@oruga-ui/oruga-next"))["OTableColumn"]; | ||
OTabItem: (typeof import("@oruga-ui/oruga-next"))["OTabItem"]; | ||
OTabs: (typeof import("@oruga-ui/oruga-next"))["OTabs"]; | ||
OTaginput: (typeof import("@oruga-ui/oruga-next"))["OTaginput"]; | ||
OTimepicker: (typeof import("@oruga-ui/oruga-next"))["OTimepicker"]; | ||
OTooltip: (typeof import("@oruga-ui/oruga-next"))["OTooltip"]; | ||
OUpload: (typeof import("@oruga-ui/oruga-next"))["OUpload"]; | ||
} | ||
} | ||
export {}; |