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 eb101d4 commit f7ada95
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions apps/ledger-live-desktop/tools/config/renderer.esbuild.js
@@ -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,32 @@ 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")) {
console.log(args);
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 f7ada95

Please sign in to comment.