-
Notifications
You must be signed in to change notification settings - Fork 294
/
index.js
156 lines (141 loc) · 4.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const childProcess = require("child_process");
const { prerelease } = require("semver");
const path = require("path");
const { StripFlowPlugin, DotEnvPlugin, nodeExternals } = require("esbuild-utils");
const { flowPlugin } = require("@bunchtogether/vite-plugin-flow");
const electronPlugin = require("vite-plugin-electron/renderer");
const reactPlugin = require("@vitejs/plugin-react");
const { defineConfig } = require("vite");
const SENTRY_URL = process.env?.SENTRY_URL;
const pkg = require("../../package.json");
const lldRoot = path.resolve(__dirname, "..", "..");
let GIT_REVISION = process.env?.GIT_REVISION;
if (!GIT_REVISION) {
GIT_REVISION = childProcess
.execSync("git rev-parse --short HEAD")
.toString("utf8")
.trim();
}
const parsed = prerelease(pkg.version);
let PRERELEASE = false;
let CHANNEL = null;
if (parsed) {
PRERELEASE = !!(parsed && parsed.length);
CHANNEL = parsed[0];
}
const DOTENV_FILE = process.env.TESTING
? ".env.testing"
: process.env.STAGING
? ".env.staging"
: process.env.NODE_ENV === "production"
? ".env.production"
: ".env";
// TODO: ADD BUNDLE ANALYZER
const buildMainEnv = (mode, argv) => {
const env = {
__DEV__: JSON.stringify(mode === "development"),
__APP_VERSION__: JSON.stringify(pkg.version),
__GIT_REVISION__: JSON.stringify(GIT_REVISION),
__SENTRY_URL__: JSON.stringify(SENTRY_URL || null),
// See: https://github.com/node-formidable/formidable/issues/337
"global.GENTLY": JSON.stringify(false),
__PRERELEASE__: JSON.stringify(PRERELEASE),
__CHANNEL__: JSON.stringify(CHANNEL),
};
if (mode === "development") {
env.INDEX_URL = JSON.stringify(`http://localhost:${argv.port}/index.html`);
}
return env;
};
const buildRendererEnv = mode => {
const env = {
__DEV__: JSON.stringify(mode === "development"),
__APP_VERSION__: JSON.stringify(pkg.version),
__GIT_REVISION__: JSON.stringify(GIT_REVISION),
__SENTRY_URL__: JSON.stringify(SENTRY_URL || null),
__PRERELEASE__: JSON.stringify(PRERELEASE),
__CHANNEL__: JSON.stringify(CHANNEL),
"process.env.NODE_ENV": JSON.stringify(mode),
};
return env;
};
const buildViteConfig = argv =>
defineConfig({
configFile: false,
root: path.resolve(lldRoot, "src", "renderer"),
server: {
port: argv.port,
// force: true,
},
define: {
...buildRendererEnv("development"),
...DotEnvPlugin.buildDefine(DOTENV_FILE),
},
resolve: {
conditions: ["node", "import", "module", "browser", "default"],
alias: {
"~": path.resolve(lldRoot, "src"),
qrloop: require.resolve("qrloop"),
"@ledgerhq/react-ui": path.join(
path.dirname(require.resolve("@ledgerhq/react-ui/package.json")),
"lib",
),
electron: path.join(__dirname, "electronRendererStubs.js"),
},
},
optimizeDeps: {
// The common.js dependencies and files need to be force-added below:
include: ["@ledgerhq/hw-app-eth/erc20"],
esbuildOptions: {
target: ["es2020"],
plugins: [
StripFlowPlugin(/.jsx?$/),
{
name: "Externalize Nodejs Standard Library",
setup(build) {
nodeExternals.forEach(external => {
build.onResolve({ filter: new RegExp(`^${external}$`) }, args => ({
path: external,
external: true,
}));
});
},
},
{
name: "fix require('buffer/') calls",
setup(build) {
build.onResolve({ filter: /^buffer\/$/ }, args => {
if (!args.importer) return;
const result = require.resolve(args.path, {
paths: [args.resolveDir],
});
return {
path: result,
};
});
},
},
],
},
},
plugins: [
flowPlugin(),
reactPlugin(),
electronPlugin(),
// {
// name: "override:electron:config-serve",
// apply: "serve",
// config(config) {
// // Override stubs to add missing remote and WebviewTag exports
// config.resolve.alias.electron = path.join(__dirname, "electronRendererStubs.js");
// },
// },
],
});
module.exports = {
buildMainEnv,
buildRendererEnv,
buildViteConfig,
lldRoot,
DOTENV_FILE,
};