Skip to content

Commit

Permalink
ionic-team#19522 Generate web-types for @ionic/vue during build
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrtomiak committed Nov 4, 2020
1 parent a9b2260 commit ea23740
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/vue/.gitignore
@@ -0,0 +1 @@
/web-types.json
6 changes: 4 additions & 2 deletions packages/vue/package.json
Expand Up @@ -5,11 +5,12 @@
"scripts": {
"lint": "echo add linter",
"test": "jest",
"build": "npm run clean && npm run copy && npm run copy.overlays && npm run compile && npm run bundle",
"build": "npm run clean && npm run copy && npm run copy.overlays && npm run compile && npm run build.web-types && npm run bundle",
"bundle": "rollup --config rollup.config.js",
"clean": "rimraf dist dist-transpiled",
"compile": "npm run tsc",
"tsc": "tsc -p .",
"build.web-types": "node ./scripts/build-web-types.js",
"copy": "node ./scripts/copy-css.js",
"copy.overlays": "node ./scripts/copy-overlays.js"
},
Expand Down Expand Up @@ -56,5 +57,6 @@
"dependencies": {
"@ionic/core": "5.4.1",
"ionicons": "^5.1.2"
}
},
"web-types": "./web-types.json"
}
83 changes: 83 additions & 0 deletions packages/vue/scripts/build-web-types.js
@@ -0,0 +1,83 @@
const fs = require("fs")

// Web-types build require docs to be built first
const docs = require("../../../docs/core.json")

const components = []

function toCamelCase(name) {
return name.split("-").map(n => n[0].toUpperCase() + n.substr(1)).join("")
}

for (const component of docs.components) {
if (!component.usage.vue) continue
const attributes = []
const slots = []
const events = []
const componentName = toCamelCase(component.tag)
const docUrl = "https://ionicframework.com/docs/api/" + component.tag.substr(4)

for (const prop of component.props || []) {
attributes.push({
name: prop.attr || prop.name,
description: prop.docs,
required: prop.required,
default: prop.default,
value: {
kind: "expression",
type: prop.type
}
})
}

for (const event of component.events || []) {
let eventName = event.event;
if (eventName.toLowerCase().startsWith(componentName.toLowerCase())) {
eventName = "on" + eventName.substr(componentName.length);
}
events.push({
name: eventName,
description: event.docs,
arguments: [{
name: "detail",
type: event.detail
}]
})
}

for (const slot of component.slots || []) {
slots.push({
name: slot.name === "" ? "default" : slot.name,
description: slot.docs
})
}

components.push({
name: componentName,
"doc-url": docUrl,
description: component.docs,
source: {
module: "@ionic/core/" + component.filePath.replace("./src/", "dist/types/").replace(".tsx", ".d.ts"),
symbol: componentName.substr(3)
},
attributes,
slots,
events
})
}

const webTypes = {
$schema: "http://json.schemastore.org/web-types",
framework: "vue",
name: "@ionic/vue",
version: require("../package.json").version,
contributions: {
html: {
"types-syntax": "typescript",
"description-markup": "markdown",
tags: components
}
}
}

fs.writeFileSync("web-types.json", JSON.stringify(webTypes, null, 2))

0 comments on commit ea23740

Please sign in to comment.