Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: embedding icons #132

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"scripts": {
"prepare": "husky install",
"preinstall": "npx only-allow pnpm",
"build": "pnpm website build",
"build": "pnpm icons build && pnpm website build",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉build越来越复杂了...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还好吧,才两个build(
可以写下script,或者上turborepo

"build:types": "pnpm --filter @bangumi/types build",
"design": "pnpm --filter=@bangumi/design",
"design:doc": "pnpm design storybook",
"design:build-doc": "pnpm design build-storybook",
"design:build-doc": "pnpm icons build && pnpm design build-storybook",
"dev": "pnpm dev:csr",
"dev:csr": "pnpm website dev",
"dev:ssr": "pnpm server dev",
Expand All @@ -20,8 +20,9 @@
"prettier": "prettier --write --list-different ./",
"prettier:check": "prettier --list-different ./",
"server": "pnpm --filter=@bangumi/server",
"test": "jest",
"test": "pnpm icons build && jest",
"website": "pnpm --filter=@bangumi/website",
"icons": "pnpm --filter=@bangumi/icons",
"lint-staged": "lint-staged",
"type-check": "tsc --noEmit"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/icons/assets/vertical-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/icons/assets/vertical-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 17 additions & 8 deletions packages/icons/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import type { Story, ComponentMeta } from '@storybook/react';
import * as Icons from '.';
import * as Icons from './dist';

type IComponent = React.FC<
React.SVGProps<SVGSVGElement> & {
Expand All @@ -15,11 +15,11 @@ const componentMeta: ComponentMeta<IComponent> = {
(story) => (
<div
style={{
display: 'flex',
alignItems: 'center',
flex: 'auto',
flexWrap: 'wrap',
margin: '0 10rem',
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill,minmax(8rem,1fr))',
gap: '2rem',
maxWidth: '80rem',
margin: 'auto',
}}
>
{story()}
Expand Down Expand Up @@ -52,8 +52,17 @@ const Template: Story<{ height: number; width: number; style: React.CSSPropertie
{Object.keys(Icons).map((iconName) => {
const Icon = (Icons as any)[iconName] as IComponent;
return (
<div key={iconName} style={{ textAlign: 'center', width: 120 }}>
<p>{iconName}</p>
<div
key={iconName}
style={{
textAlign: 'center',
height: 100,
border: 'solid 2px #dedede',
borderRadius: '5px',
padding: '1rem',
}}
>
<p style={{ paddingBottom: '1.5rem', fontWeight: '600' }}>{iconName}</p>
<Icon height={height} width={width} style={style} />
</div>
);
Expand Down
22 changes: 0 additions & 22 deletions packages/icons/index.tsx

This file was deleted.

6 changes: 5 additions & 1 deletion packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"name": "@bangumi/icons",
"version": "0.0.0",
"private": true,
"main": "index.tsx",
"main": "./dist/index.js",
"scripts": {
"build": "node scripts/build.js"
},
"dependencies": {
"@svgr/core": "^6.5.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
48 changes: 48 additions & 0 deletions packages/icons/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { readdir, readFile, writeFile, mkdir, access, constants } = require('fs/promises');
const { resolve, join } = require('path');
const { transform } = require('@svgr/core');
const { rmSync } = require('fs');

const OUT_DIR = resolve('./dist');
const capitalize = (str) => str[0].toUpperCase() + str.slice(1);

async function main() {
try {
await access(OUT_DIR, constants.F_OK);
rmSync(OUT_DIR, { force: true, recursive: true });
} catch (error) {}

await mkdir(OUT_DIR);

try {
const icons = await readdir('./assets/');
let indexContent = '';

for (const icon of icons) {
const [filename] = icon.split('.');
const defaultName = filename.split('-').map(capitalize).join('');
const svg = (await readFile(resolve('./assets', icon))).toString();

const svgComponents = await transform(svg);
const svgComponentsDts = `import * as React from 'react';
declare function ${defaultName}(props: React.ComponentProps<'svg'>): JSX.Element;
export default ${defaultName};
`;
indexContent += `export { default as ${defaultName} } from "./${filename}.jsx"\n`;

// create jsx
await writeFile(join(OUT_DIR, `${filename}.jsx`), svgComponents);
await writeFile(join(OUT_DIR, `${filename}.d.ts`), svgComponentsDts);
}

// export all svg
await writeFile(join(OUT_DIR, 'index.js'), indexContent, { encoding: 'utf-8' });
await writeFile(join(OUT_DIR, 'index.d.ts'), indexContent, { encoding: 'utf-8' });
} catch (error) {
Copy link
Contributor

@trim21 trim21 Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不用catch吧,catch了之后构建失败只有一个console.error了,依旧会去构建后面的website

console.error(error);
}
}

main().catch(e => {
throw e;
});
Loading