Skip to content

Commit

Permalink
fix: add custom esbuild plugin to extract json
Browse files Browse the repository at this point in the history
  • Loading branch information
KVNLS committed Oct 13, 2023
1 parent a8f61e7 commit 407796a
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions apps/ledger-live-desktop/tools/config/renderer.esbuild.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require("path");
const fs = require("fs");
const crypto = require("crypto");
const {
AliasPlugin,
HtmlPlugin,
Expand All @@ -9,6 +11,15 @@ const {
const { DOTENV_FILE } = require("../utils");
const common = require("./common.esbuild");

const ensureDirectoryExistence = filePath => {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
};

module.exports = {
...common,
entryPoints: ["src/renderer/index.ts"],
Expand All @@ -18,6 +29,7 @@ module.exports = {
target: ["chrome114"],
format: "iife",
mainFields: ["browser", "module", "main"],
assetNames: "assets/[name]-[hash]",
external: [...nodeExternals, ...electronRendererExternals],
resolveExtensions: process.env.V3
? [".v3.tsx", ".v3.ts", ".tsx", ".ts", ".js", ".jsx", ".json"]
Expand All @@ -44,18 +56,31 @@ module.exports = {
],
}),
DotEnvPlugin(DOTENV_FILE),
// {
// name: "Side Effects",
// setup(build) {
// build.onResolve({ filter: /\.woff2$/ }, async args => {
// if (args.importer.endsWith("libs/ui/packages/react/lib/assets/fonts.js")) {
// console.log(args.path);
// return {
// sideEffects: true,
// };
// }
// });
// },
// },
{
name: "assets-plugin",
setup(build) {
build.onResolve({ filter: /\.(json)$/ }, args => {
if (args.resolveDir.includes("ledger-live-desktop/src")) {
const sourcePath = path.resolve(args.resolveDir, args.path);
const fileContent = fs.readFileSync(sourcePath);

const hash = crypto.createHash("sha1").update(fileContent).digest("hex");

const fileName = `${hash}-${path.basename(args.path)}`;
const targetPath = path.resolve(".webpack/assets", fileName);

ensureDirectoryExistence(targetPath);
fs.copyFileSync(sourcePath, targetPath);

return {
path: targetPath,
external: true,
};
}

return undefined;
});
},
},
],
};

0 comments on commit 407796a

Please sign in to comment.