Skip to content

Commit

Permalink
feat: 完成icons-solid的编译
Browse files Browse the repository at this point in the history
  • Loading branch information
phy-lei committed Feb 8, 2025
1 parent 9cf6fed commit 15c2e31
Show file tree
Hide file tree
Showing 259 changed files with 1,816 additions and 974 deletions.
111 changes: 111 additions & 0 deletions packages/icons-solid-taro/scripts/camelCase.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-disable */
const UPPERCASE = /\p{Lu}/u
const LOWERCASE = /\p{Ll}/u
const LEADING_CAPITAL = /^\p{Lu}(?!\p{Lu})/gu
const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u
const SEPARATORS = /[_.\- ]+/

const LEADING_SEPARATORS = new RegExp(`^${SEPARATORS.source}`)
const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu')
const NUMBERS_AND_IDENTIFIER = new RegExp(`\\d+${IDENTIFIER.source}`, 'gu')

function preserveCamelCase(string, toLowerCase, toUpperCase) {
let isLastCharLower = false
let isLastCharUpper = false
let isLastLastCharUpper = false

for (let index = 0; index < string.length; index++) {
const character = string[index]

if (isLastCharLower && UPPERCASE.test(character)) {
string = `${string.slice(0, index)}-${string.slice(index)}`
isLastCharLower = false
isLastLastCharUpper = isLastCharUpper
isLastCharUpper = true
index++
}
else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
string = `${string.slice(0, index - 1)}-${string.slice(index - 1)}`
isLastLastCharUpper = isLastCharUpper
isLastCharUpper = false
isLastCharLower = true
}
else {
isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character
isLastLastCharUpper = isLastCharUpper
isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character
}
}

return string
}

function preserveConsecutiveUppercase(input, toLowerCase) {
LEADING_CAPITAL.lastIndex = 0

return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1))
}

function postProcess(input, toUpperCase) {
SEPARATORS_AND_IDENTIFIER.lastIndex = 0
NUMBERS_AND_IDENTIFIER.lastIndex = 0

return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
.replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m))
}

module.exports = function camelCase(input, options) {
if (!(typeof input === 'string' || Array.isArray(input))) {
throw new TypeError('Expected the input to be `string | string[]`')
}

options = {
pascalCase: false,
preserveConsecutiveUppercase: false,
...options,
}

if (Array.isArray(input)) {
input = input.map(x => x.trim())
.filter(x => x.length)
.join('-')
}
else {
input = input.trim()
}

if (input.length === 0) {
return ''
}

const toLowerCase = options.locale === false
? string => string.toLowerCase()
: string => string.toLocaleLowerCase(options.locale)

const toUpperCase = options.locale === false
? string => string.toUpperCase()
: string => string.toLocaleUpperCase(options.locale)

if (input.length === 1) {
if (SEPARATORS.test(input)) {
return ''
}

return options.pascalCase ? toUpperCase(input) : toLowerCase(input)
}

const hasUpperCase = input !== toLowerCase(input)

if (hasUpperCase) {
input = preserveCamelCase(input, toLowerCase, toUpperCase)
}

input = input.replace(LEADING_SEPARATORS, '')
input = options.preserveConsecutiveUppercase ? preserveConsecutiveUppercase(input, toLowerCase) : toLowerCase(input)

if (options.pascalCase) {
input = toUpperCase(input.charAt(0)) + input.slice(1)
}

return postProcess(input, toUpperCase)
}
160 changes: 160 additions & 0 deletions packages/icons-solid-taro/scripts/generate-react.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { camelCase } from './camelCase.cjs';
import glob from 'glob'
import path from 'path'
import fsExtra from 'fs-extra';
import { parse } from 'svg-parser'
import { optimize } from 'svgo'
import consola from "consola";
import svg64 from './svg64.cjs';

const getSvg = (compoentName: string, viewBox: string, d: any[]) => {
const template = `
import {FunctionComponent} from 'react'
import Icon, {defaultProps, SVG_IconProps} from '../IconTemplate'
const Add:FunctionComponent<SVG_IconProps> = (props: SVG_IconProps) => {
const realProps = { ...defaultProps, ...props }
return <Icon {...realProps} name={realProps.name || '${compoentName}'} viewBox={'${viewBox}'}>
${d.map(d => {
return `<path
d="${d}"
fill="currentColor"
fillOpacity="0.9"
></path>`
})}
</Icon>
}
export default Add
`
return template
}
const getIconFont = (compoentName: string) => {
const template = `
import IconFont, {IconFontProps} from "../IconFont";
import {FunctionComponent} from "react";
const Icon: FunctionComponent<IconFontProps> = (props: IconFontProps) => {
return <IconFont {...props} name={props.name || '${compoentName}'}/>
}
Icon.displayName = 'NutIcon${compoentName}'
export default Icon
`
return template
}

const getTaroSvg = (compoentName: string, svg: string) => {
const svg64String = svg64(svg)
const template = `
import {FunctionComponent} from 'react'
import Icon, {defaultProps, SVG_IconProps} from '../IconTemplate'
const IconSVG:FunctionComponent<SVG_IconProps> = (props: SVG_IconProps) => {
const realProps = { ...defaultProps, ...props }
return <Icon {...realProps} name={realProps.name || '${compoentName}'} svg64={'${svg64String}'}>
</Icon>
}
export default IconSVG
`
return template
}

let entryEs = `/** 此文件由 script generate 脚本生成 */
export { config as IconFontConfig } from "./icons/IconFontConfig.js";
export { default as IconFont } from "./icons/IconFont.js";
export { configure } from "./icons/configure.js";
\n`;

let entryLib = `/** 此文件由 script generate 脚本生成 */
import IconFont from '../IconFont';
import { configure } from "../configure";
import config from '../../../../iconfont/config.json';
export { IconFont, config, configure };
\n`;
let entryLibDTS = `/** 此文件由 script generate 脚本生成 */
import IconFont from '../IconFont';
import { configure } from "../configure";
export { IconFont, configure };
\n`;

const projectID = process.env.PROJECT_ID
let pattern = `${process.cwd()}/packages/icons-svg/*.svg`;
let iconsReactDir = `icons-react`;
let iconsReactTaroDir = `icons-react-taro`;

if (projectID) {

entryLib = `/** 此文件由 script generate 脚本生成 */
import IconFont from '../IconFont';
import config from '../../../../${projectID}-iconfont/config.json';
export { IconFont, config };
\n`;

pattern = `${process.cwd()}/packages/${projectID}-icons-svg/*.svg`;
iconsReactDir = `${projectID}-icons-react`;
iconsReactTaroDir = `${projectID}-icons-react-taro`;
}

new glob.Glob(pattern, {}, (err, files) => {
const entryArray: any = []
files.forEach(file => {
const basename = path.basename(file)
const iconFontName = basename.replace('.svg', '')
const componentName = camelCase(iconFontName, {
pascalCase: true
})

entryArray.push(componentName)
entryLib += `export { default as ${componentName} } from '../components/${componentName}'\n`
entryEs += `export { default as ${componentName} } from "./icons/${componentName}.js";\n`;
entryLibDTS += `export { default as ${componentName} } from "../components/${componentName}";\n`;

fsExtra.readFile(file, { encoding: 'utf8' }).then((res) => {
let svg = optimize(res).data;
const svgAST = parse(svg).children[0];
let pathds = (svgAST as any).children?.map((item: any) => {
return item.properties.d;
})
let viewBox = (svgAST as any).properties.viewBox;

fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactDir}/src/components/${componentName}.tsx`, getSvg(componentName, viewBox, pathds), 'utf8', (error) => {
consola.success(`${iconsReactDir} ${componentName} 文件写入成功`);
});

fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/src/components/${componentName}.tsx`, getTaroSvg(componentName, svg), 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} svg ${componentName} 文件写入成功`);
});

})

fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/src/components/${componentName}.tsx`, getIconFont(iconFontName), 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} ${componentName} 文件写入成功`);
});
})
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactDir}/src/components/iconsConfig.ts`, `export const iconsConfig = ${JSON.stringify(entryArray)}`, 'utf8', (error) => {
consola.success(`${iconsReactDir} 文件列表配置写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/src/components/iconsConfig.ts`, `export const iconsConfig = ${JSON.stringify(entryArray)}`, 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} 文件列表配置写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactDir}/dist/es/index.es.js`, entryEs + 'import "../style_icon.css";', 'utf8', (error) => {
consola.success(`${iconsReactDir} ES 入口文件文件写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/dist/es/index.es.js`, entryEs + 'import "../style_icon.css";', 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} ES 入口文件文件写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactDir}/src/buildEntry/lib-new.ts`, entryLib, 'utf8', (error) => {
consola.success(`${iconsReactDir} buildEntry 文件写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/src/buildEntry/lib-new.ts`, entryLib, 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} buildEntry 文件写入成功`);
});

fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactDir}/src/buildEntry/lib-new-dts.ts`, entryLibDTS, 'utf8', (error) => {
consola.success(`${iconsReactDir} buildEntry dts 文件写入成功`);
});
fsExtra.outputFile(`${process.cwd()}/packages/${iconsReactTaroDir}/src/buildEntry/lib-new-dts.ts`, entryLibDTS, 'utf8', (error) => {
consola.success(`${iconsReactTaroDir} buildEntry dts 文件写入成功`);
});
})
Loading

0 comments on commit 15c2e31

Please sign in to comment.