-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,489 additions
and
137 deletions.
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
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 @@ | ||
import './index.scss'; |
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,96 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = require("fs"); | ||
const parser_1 = require("@babel/parser"); | ||
const core_1 = require("@babel/core"); | ||
const path_1 = require("path"); | ||
// const curDir = path.dirname(fileURLToPath(import.meta.url)); | ||
const curDir = __dirname; | ||
function getPivotDesignProps() { | ||
return getProps((0, path_1.resolve)(curDir, '../../../design-props/components/index.ts')); | ||
} | ||
function parseComment(commentStr) { | ||
let props = {}; | ||
if (!commentStr) { | ||
return props; | ||
} | ||
const COMMENT_REG = /@(\w+)\s+(.+)/g; | ||
for (const prop of commentStr.matchAll(COMMENT_REG)) { | ||
const [, key, val] = prop; | ||
props[key] = val; | ||
} | ||
return props; | ||
} | ||
function getProps(path) { | ||
if (!(0, fs_1.existsSync)(path)) { | ||
throw `<API />设置的props所在路径${path}不存在`; | ||
} | ||
const sourceCode = (0, fs_1.readFileSync)(path, { encoding: 'utf-8' }); | ||
const ast = (0, parser_1.parse)(sourceCode, { | ||
// 不确定是module还是es6写法 | ||
sourceType: 'unambiguous', | ||
plugins: ['typescript'], | ||
}); | ||
let returnProps = new Map(); | ||
// 该函数返回值是代码,所以只好在plugin的post部分修改了props值 | ||
(0, core_1.traverse)(ast, { | ||
TSInterfaceDeclaration: (path) => { | ||
const { name } = path.node.id; | ||
const extendsVal = path.node.extends; | ||
if (!returnProps.has(name)) { | ||
returnProps.set(name, []); | ||
} | ||
extendsVal?.forEach((item) => { | ||
if (item.expression.name === 'PivotDesignProps') { | ||
const props = getPivotDesignProps().get('PivotDesignProps'); | ||
returnProps.set(name, props); | ||
} | ||
}); | ||
path.traverse({ | ||
TSPropertySignature: (p) => { | ||
if (p.parentKey === 'members') | ||
return; | ||
// "'--button-background-color'"的特殊处理 | ||
const key = p.get('key').toString().replaceAll(/['"]/g, ''); | ||
const value = p.get('typeAnnotation').toString().slice(1); | ||
const optional = p.get('optional').node; | ||
const commentStr = p.get('leadingComments').length > 0 && p.get('leadingComments')[0].node.value; | ||
const comments = parseComment(commentStr); | ||
let props = returnProps.get(name); | ||
const newProps = { key, value, optional, ...comments }; | ||
props.push(newProps); | ||
returnProps.set(name, props); | ||
}, | ||
}); | ||
}, | ||
}); | ||
return returnProps; | ||
} | ||
function getPropsNode(props) { | ||
let node = ''; | ||
for (const key of props.keys()) { | ||
const val = props.get(key); | ||
node += `export const ${key}Props = ${JSON.stringify(val)}\n | ||
<ApiTable name='${key}' params={${key}Props}></ApiTable>\n`; | ||
} | ||
return node; | ||
} | ||
function loader(source) { | ||
let sourceCode = source.trim(); | ||
const apiBlockReg = /<Api\s+path=(["'])([^"']+)\1(><\/Api>|\s+\/>)/g; | ||
const matchValArr = sourceCode.matchAll(apiBlockReg); | ||
const arr = Array.from(matchValArr); | ||
if (arr.length) { | ||
for (let matchVal of arr) { | ||
let [initialVal, , pathVal] = matchVal; | ||
pathVal = (0, path_1.join)(curDir, '../../../design-props', pathVal); | ||
const props = getProps(pathVal); | ||
const propsNode = getPropsNode(props); | ||
sourceCode = sourceCode.replace(initialVal, propsNode); | ||
} | ||
// 添加ApiTable的import\ | ||
sourceCode = 'import ApiTable from "../../ApiTable.tsx";\n\n' + sourceCode; | ||
} | ||
return sourceCode; | ||
} | ||
exports.default = loader; |
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,105 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { parse } from '@babel/parser'; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { transformFromAstSync, traverse } from '@babel/core'; | ||
import { ParamsMap } from './type'; | ||
import { join, resolve } from 'path'; | ||
|
||
// const curDir = path.dirname(fileURLToPath(import.meta.url)); | ||
const curDir = __dirname; | ||
|
||
function getPivotDesignProps() { | ||
return getProps(resolve(curDir, '../../../design-props/components/index.ts')); | ||
} | ||
|
||
function parseComment(commentStr: string) { | ||
let props = {}; | ||
if (!commentStr) { | ||
return props; | ||
} | ||
const COMMENT_REG = /@(\w+)\s+(.+)/g; | ||
for (const prop of commentStr.matchAll(COMMENT_REG)) { | ||
const [, key, val] = prop; | ||
props[key] = val; | ||
} | ||
return props; | ||
} | ||
|
||
function getProps(path: string) { | ||
if (!existsSync(path)) { | ||
throw `<API />设置的props所在路径${path}不存在`; | ||
} | ||
const sourceCode = readFileSync(path, { encoding: 'utf-8' }); | ||
const ast = parse(sourceCode, { | ||
// 不确定是module还是es6写法 | ||
sourceType: 'unambiguous', | ||
plugins: ['typescript'], | ||
}); | ||
let returnProps = new Map(); | ||
// 该函数返回值是代码,所以只好在plugin的post部分修改了props值 | ||
traverse(ast, { | ||
TSInterfaceDeclaration: (path) => { | ||
const { name } = path.node.id; | ||
const extendsVal = path.node.extends; | ||
if (!returnProps.has(name)) { | ||
returnProps.set(name, []); | ||
} | ||
extendsVal?.forEach((item) => { | ||
if (item.expression.name === 'PivotDesignProps') { | ||
const props = getPivotDesignProps().get('PivotDesignProps'); | ||
returnProps.set(name, props); | ||
} | ||
}); | ||
path.traverse({ | ||
TSPropertySignature: (p) => { | ||
if (p.parentKey === 'members') return; | ||
// "'--button-background-color'"的特殊处理 | ||
const key = p.get('key').toString().replaceAll(/['"]/g, ''); | ||
const value = p.get('typeAnnotation').toString().slice(1); | ||
const optional = p.get('optional').node; | ||
const commentStr = p.get('leadingComments').length > 0 && p.get('leadingComments')[0].node.value; | ||
const comments = parseComment(commentStr); | ||
let props = returnProps.get(name); | ||
const newProps = { key, value, optional, ...comments }; | ||
props.push(newProps); | ||
returnProps.set(name, props); | ||
}, | ||
}); | ||
}, | ||
}); | ||
return returnProps; | ||
} | ||
|
||
function getPropsNode(props: ParamsMap) { | ||
let node = ''; | ||
for (const key of props.keys()) { | ||
const val = props.get(key); | ||
node += `export const ${key}Props = ${JSON.stringify(val)}\n | ||
<ApiTable name='${key}' params={${key}Props}></ApiTable>\n`; | ||
} | ||
return node; | ||
} | ||
|
||
function loader(source: string) { | ||
let sourceCode = source.trim(); | ||
|
||
const apiBlockReg = /<Api\s+path=(["'])([^"']+)\1(><\/Api>|\s+\/>)/g; | ||
|
||
const matchValArr = sourceCode.matchAll(apiBlockReg); | ||
const arr = Array.from(matchValArr); | ||
if (arr.length) { | ||
for (let matchVal of arr) { | ||
let [initialVal, , pathVal] = matchVal; | ||
pathVal = join(curDir, '../../../design-props', pathVal); | ||
const props = getProps(pathVal); | ||
const propsNode = getPropsNode(props); | ||
sourceCode = sourceCode.replace(initialVal, propsNode); | ||
} | ||
// 添加ApiTable的import\ | ||
sourceCode = 'import ApiTable from "../../ApiTable.tsx";\n\n' + sourceCode; | ||
} | ||
|
||
return sourceCode; | ||
} | ||
|
||
export default loader; |
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,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
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,12 @@ | ||
export interface Param { | ||
key: string; | ||
value: string; | ||
optional: boolean; | ||
version: string; | ||
description: string; | ||
default: string; | ||
} | ||
|
||
export type Params = Param[]; | ||
|
||
export type ParamsMap = Map<string, Params>; |
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,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": ["src/*"], | ||
}, | ||
"target": "ESNext", | ||
"module": "CommonJS", | ||
"noEmitOnError": false, | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
} |
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,45 @@ | ||
import { Params } from '../loaders/api/type'; | ||
import React from 'react'; | ||
import './index.scss'; | ||
|
||
interface ITableProps { | ||
name: string; | ||
params: Params; | ||
} | ||
|
||
const ApiTable: React.FC<ITableProps> = ({ name, params }) => { | ||
// console.log(params, name); | ||
return ( | ||
<> | ||
<h3 className="pivot-props-apiTitle">{name}</h3> | ||
<table className="pivot-props-table"> | ||
<thead> | ||
<tr> | ||
<th>参数名称</th> | ||
<th>说明</th> | ||
<th>参数类型</th> | ||
<th>默认值</th> | ||
<th>版本支持</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{params.map((param) => { | ||
return ( | ||
<tr> | ||
<td style={{ width: '15%' }}>{param.key}</td> | ||
<td style={{ width: '40%' }}>{param.description}</td> | ||
<td style={{ width: '20%' }}>{param.value}</td> | ||
<td style={{ width: '15%' }}> | ||
{param.default && param.default.length && param.default !== 'undefined' ? param.default : '—'} | ||
</td> | ||
<td style={{ width: '10%' }}>{param.version}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
</> | ||
); | ||
}; | ||
|
||
export default ApiTable; |
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
Oops, something went wrong.