Skip to content

Commit

Permalink
Clean up a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Apr 21, 2024
1 parent 9b8384c commit cdd923c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/r18gs/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { defineConfig } from "tsup";
import fs from "node:fs";
import path from "node:path";

export default defineConfig(options => ({
format: ["cjs", "esm"],
Expand All @@ -8,4 +10,37 @@ export default defineConfig(options => ({
bundle: true,
minify: !options.watch,
legacyOutput: true,
esbuildPlugins: [
{
name: "improve-minify",
setup(build) {
build.onLoad({ filter: /use-rgs.ts/ }, args => {
let contents = fs.readFileSync(args.path, "utf8");
const lines = contents.split("\n");
const hackLine = lines.find(line => line.startsWith("const [VALUE,"));

if (!hackLine) return { contents, loader: "ts" };
/** remove hackLine */
contents = contents.replace(hackLine, "");

/** clean up */
const tokens = hackLine
.replace("const", "")
.split("=")[0]
.trim()
.replace(/\]|\[/g, "")
.split(",")
.map(t => t.trim());

tokens.sort((a, b) => a.length - b.length);

for (let i = 0; i < tokens.length; i++) {
contents = contents.replace(new RegExp(`${tokens[i]}`, "g"), i + "");
}
console.log({ contents });
return { contents, loader: "ts" };
});
},
},
],
}));

0 comments on commit cdd923c

Please sign in to comment.