Skip to content

Commit 3f1d190

Browse files
committed
Add dynamic import support, allow build script to pretify at runtime
1 parent 3d6692a commit 3f1d190

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+3010
-2628
lines changed

package-lock.json

Lines changed: 34 additions & 256 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"scripts": {
2727
"build": "npm run clean && rollup --config && npm run build:types",
2828
"build:types": "tsc --emitDeclarationOnly --declaration --outDir ./dist/types",
29-
"build:script": "node scripts/index.js",
29+
"build:script": "node scripts/index.mjs",
3030
"test": "vitest --config ./vitest.config.mjs run",
3131
"clean": "rm -rf dist",
3232
"reformat": "prettier . --write"
@@ -101,13 +101,10 @@
101101
"prettier": "3.2.5",
102102
"react": "^18.2.0",
103103
"react-dom": "^18.2.0",
104-
"react-element-to-jsx-string": "^15.0.0",
105104
"rollup": "^4.12.0",
106-
"rollup-plugin-dts": "^6.1.0",
107105
"rollup-plugin-visualizer": "^5.14.0",
108106
"typescript": "^5.3.3",
109107
"vite-plugin-dynamic-import": "^1.6.0",
110-
"vitest": "^1.3.1",
111-
"rollup-plugin-typescript2": "^0.36.0"
108+
"vitest": "^1.3.1"
112109
}
113110
}

scripts/index.js

Lines changed: 0 additions & 111 deletions
This file was deleted.

scripts/index.mjs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
3+
import shapeData from "./shapeData.mjs";
4+
import * as prettier from "prettier";
5+
6+
const cwd = import.meta.dirname;
7+
const outputDir = path.join(cwd, "../src/shapes");
8+
const prettierConfig = await prettier.resolveConfig(cwd);
9+
const dynamicImportsPath = path.join(cwd, "../src/dynamicImports.ts");
10+
11+
async function format(text) {
12+
return prettier.format(text, {
13+
prettierConfig,
14+
parser: "babel-ts",
15+
});
16+
}
17+
18+
async function createShapeFile(shapeKey, shapeData) {
19+
const [category, index] = shapeKey.split("-");
20+
const categoryDir = path.join(outputDir, category);
21+
if (!fs.existsSync(categoryDir)) {
22+
fs.mkdirSync(categoryDir, { recursive: true });
23+
}
24+
const filePath = path.join(categoryDir, `${index}.tsx`);
25+
const componentDataString = `
26+
{
27+
shape: "${shapeData.shape}",
28+
blur: ${shapeData.blur},
29+
gradientShapes: ${shapeData.gradientShapes},
30+
${shapeData.shapeFill ? `opacity: ${shapeData.opacity},` : ``}
31+
${shapeData.shapeFill ? `shapeFill: "${shapeData.shapeFill}",` : ``}
32+
gradient: ${JSON.stringify(shapeData.gradient)}
33+
}
34+
`;
35+
// Create file content
36+
const fileContent = `${shapeData.gradientShapes ? `import * as React from "react"` : ""};
37+
import { createShapeComponent } from "../../lib/utils/shape";
38+
import { ComponentDataType } from "../../lib/types";
39+
40+
const data: ComponentDataType = ${componentDataString};
41+
42+
const Component = createShapeComponent("${shapeKey}", data);
43+
export { data, Component as default};
44+
`;
45+
const formattedContent = await format(fileContent);
46+
fs.writeFileSync(filePath, formattedContent, "utf8");
47+
}
48+
49+
//
50+
let finishedShapes = {};
51+
await Promise.all(
52+
Object.entries(shapeData).map(async ([shapeKey, shapeData]) => {
53+
const category = shapeKey.split("-")[0];
54+
const categoryIndex = finishedShapes[category];
55+
if (!categoryIndex) {
56+
finishedShapes[category] = 0;
57+
}
58+
await createShapeFile(shapeKey, shapeData);
59+
finishedShapes[category]++;
60+
})
61+
);
62+
63+
await Promise.all(
64+
Object.entries(finishedShapes).map(async ([category, maxFiles]) => {
65+
const indexPath = path.join(outputDir, `${category}/index.tsx`);
66+
const isNumberCategory = category.startsWith("number");
67+
let imports = "";
68+
const CategoryName = category.charAt(0).toUpperCase() + category.slice(1);
69+
let shapeDataArrayEntries = "";
70+
[...Array(maxFiles)].forEach((_, i) => {
71+
const fileNumber = isNumberCategory ? i : i + 1;
72+
const nameCapital = `${CategoryName}${fileNumber}`;
73+
imports += `import * as ${nameCapital} from "./${fileNumber}"; \n`;
74+
shapeDataArrayEntries += ` "${category}-${fileNumber}": ${nameCapital}.data,\n`;
75+
});
76+
const content = `
77+
${imports}
78+
import { getComponentWithShapeType } from "../../lib/utils/shape";
79+
export const ${CategoryName}Data = {
80+
${shapeDataArrayEntries}
81+
};
82+
const ${CategoryName} = getComponentWithShapeType(
83+
"${category}",
84+
Object.values(${CategoryName}Data)
85+
);
86+
export {
87+
${CategoryName} as default,
88+
${CategoryName},
89+
};
90+
`;
91+
const formatedContent = await format(content);
92+
fs.writeFileSync(indexPath, formatedContent, "utf8");
93+
})
94+
);
95+
96+
let imports = "";
97+
const indexPath = path.join(outputDir, `index.tsx`);
98+
let dynamicImportsPropsString = ``;
99+
100+
Object.keys(shapeData).forEach((key) => {
101+
const seperatedKey = key.split("-");
102+
const index = seperatedKey[1];
103+
const name = seperatedKey[0];
104+
const categoryName = name.charAt(0).toUpperCase() + name.slice(1);
105+
imports += `export {default as ${categoryName}${index}} from './${name}/${index}.jsx'; \n`;
106+
dynamicImportsPropsString += `"${key}": () => import('./shapes/${name}/${index}'), \n`;
107+
});
108+
109+
Object.keys(finishedShapes).forEach((key) => {
110+
imports += `export * from './${key}'; \n`;
111+
});
112+
const formatedImports = await format(imports);
113+
114+
fs.writeFileSync(indexPath, formatedImports, "utf8");
115+
116+
const dynamicImportsString = await format(`
117+
export const dynamicImports = {
118+
${dynamicImportsPropsString}
119+
}
120+
`);
121+
fs.writeFileSync(dynamicImportsPath, dynamicImportsString, "utf-8");
File renamed without changes.

src/CoolShape.tsx

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
import React, { forwardRef, useEffect, useState } from "react";
2-
import { shapesData } from "./shapes";
2+
import shapesData from "./shapes/data";
33
import ShapeBase from "./lib/shapeBase";
4-
import { ShapeMetadataProps } from "./lib/types";
5-
import { ComponentDataType, ShapesType } from "./shapes/types";
4+
import { ComponentDataType, CoolshapeComponentProps } from "./lib/types";
65
import { getRandomShape } from "./lib/utils/shape";
76

8-
const Coolshape = forwardRef<
9-
SVGSVGElement,
10-
ShapeMetadataProps & { shapeId?: string } & {
11-
random?: boolean;
12-
index?: number;
13-
type?: ShapesType;
14-
}
15-
>(({ random, index, type, ...rest }, ref) => {
16-
const [shapeId, setShapeId] = useState(`${type}-${index}`);
17-
useEffect(() => {
18-
if (random || !type || !index) {
19-
const randomShape = getRandomShape({
20-
type,
21-
});
22-
setShapeId(randomShape.shapeId);
23-
}
24-
}, []);
7+
const Coolshape = forwardRef<SVGSVGElement, CoolshapeComponentProps>(
8+
({ random, index, type, unstyled, ...rest }, ref) => {
9+
const toDefault = unstyled || (!type && !index && !random);
10+
const [shapeId, setShapeId] = useState(
11+
toDefault ? "star-1" : `${type}-${index}`
12+
);
13+
useEffect(() => {
14+
if (random || (!index && type)) {
15+
const randomShape = getRandomShape({
16+
type,
17+
});
18+
setShapeId(randomShape.shapeId);
19+
}
20+
}, []);
2521

26-
const ElementData: ComponentDataType = shapesData[shapeId];
27-
28-
if (ElementData) {
29-
return <ShapeBase ref={ref} {...ElementData} shapeId={shapeId} {...rest} />;
30-
} else {
31-
return null;
22+
const ElementData: ComponentDataType = shapesData[shapeId];
23+
const InitialProps = toDefault ? { shape: ElementData.shape } : ElementData;
24+
if (InitialProps) {
25+
return (
26+
<ShapeBase ref={ref} {...InitialProps} shapeId={shapeId} {...rest} />
27+
);
28+
} else {
29+
return null;
30+
}
3231
}
33-
});
32+
);
3433

3534
Coolshape.displayName = "Coolshape";
3635
export { Coolshape, Coolshape as CoolShape };

0 commit comments

Comments
 (0)