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

migrate icons from bun to ts-node #490

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file modified bun.lockb
Binary file not shown.
34 changes: 18 additions & 16 deletions packages/icons/build-icons.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import * as prettier from "prettier";
import metadata from "@navikt/aksel-icons/metadata";
import { optimize } from "svgo";
import { Glob } from "bun";

const glob = new Glob("./src/*.svg");
import * as fs from "fs";

const items = fs.readdirSync("./src");
const files = [];
for await (const path of glob.scan(".")) {
// Prevent build errors on windows file systems
const unixPath = path.replaceAll("\\", "/");
files.push({
name: /([^/]+)\.svg$/.exec(unixPath)?.[1] ?? "wat",
path: unixPath,
});
for (const item of items) {
if (item.endsWith(".svg")) {
const filePath = `./src/${item}`;
const unixPath = filePath.replaceAll("\\", "/");
files.push({
name: /([^/]+)\.svg$/.exec(unixPath)?.[1] ?? "wat",
path: unixPath,
});
}
}

const jsString =
'${htmlAttributes({ ariaHidden: ariaLabel ? "false" : "true", ...props })} ${ariaLabel ? html`aria-label="${ariaLabel}"` : ""}';

Expand All @@ -33,8 +35,8 @@ ${svg}
})),
...files,
].forEach(async ({ name, path }) => {
const iconPath = (await import(path)).default;
const icon = await Bun.file(iconPath).text();
const iconPath = require.resolve(path);
const icon = fs.readFileSync(iconPath, "utf-8");
const result = optimize(icon, {
path: iconPath,
plugins: [
Expand All @@ -53,7 +55,7 @@ ${svg}

const optimizedSvgString = result.data;

Bun.write(
fs.writeFileSync(
`./dist/${name}.ts`,
await prettier.format(
fileTemplate({
Expand All @@ -65,7 +67,8 @@ ${svg}
);
});

Bun.write(
fs.mkdirSync("./dist", { recursive: true });
fs.writeFileSync(
"./dist/index.ts",
`
export * from '../src';
Expand All @@ -75,5 +78,4 @@ ${[...Object.keys(metadata), ...files.map(({ name }) => name)]
`,
);

const file = Bun.file("./src/types.ts");
await Bun.write("./dist/types.ts", file);
fs.copyFileSync("./src/types.ts", "./dist/types.ts");
7 changes: 4 additions & 3 deletions packages/icons/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "decorator-icons",
"version": "0.0.1",
"type": "module",
"type": "commonjs",
"exports": {
".": "./dist/index.ts"
},
"scripts": {
"build": "bun run build-icons.ts"
"build": "ts-node build-icons.ts"
},
"dependencies": {
"decorator-shared": "workspace:*"
},
"devDependencies": {
"@navikt/aksel-icons": "7.5.1",
"svgo": "3.3.2"
"svgo": "3.3.2",
"ts-node": "10.9.2"
}
}
35 changes: 1 addition & 34 deletions packages/icons/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,

"plugins": [
{
"name": "typescript-plugin-css-modules",
"options": {
"postcssOptions": {
"useConfig": true
}
}
}
]
"esModuleInterop": true
}
}