-
Notifications
You must be signed in to change notification settings - Fork 1
/
electron.vite.config.ts
141 lines (130 loc) · 4.79 KB
/
electron.vite.config.ts
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
import react from '@vitejs/plugin-react';
import { createHash } from 'crypto';
import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
// eslint-disable-next-line no-restricted-imports
import { i18nextHMRPlugin } from 'i18next-hmr/vite';
import { basename, dirname, join, relative, resolve, sep } from 'path';
import { ConfigEnv } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
const localesDir = resolve(__dirname, 'resources', 'locales');
export default defineConfig({
main: (env) => ({
plugins: [
tsconfigPaths({
root: __dirname,
projects: ['tsconfig.node.json'],
}),
externalizeDepsPlugin(),
i18nextHMRPlugin({ localesDir }),
],
define: getDefines('main', env),
}),
preload: (env) => ({
plugins: [
tsconfigPaths({
root: __dirname,
projects: ['tsconfig.node.json', 'tsconfig.web.json'],
}),
externalizeDepsPlugin(),
],
define: getDefines('preload', env),
}),
renderer: (env) => ({
publicDir: join(__dirname, 'resources'),
css: {
modules: {
// use this instead to generate just hashed names in production (without paths/local names)
// generateScopedName: env.command === 'build' ? getHashedScopedName() : getNiceScopedName(),
generateScopedName: getNiceScopedName(),
},
},
plugins: [
tsconfigPaths({
root: __dirname,
projects: ['tsconfig.web.json'],
}),
react(),
i18nextHMRPlugin({ localesDir }),
],
define: getDefines('renderer', env),
}),
});
/**
* @param type Type of build to provide defines to
* @return Map of global constants to define (to be applied in the build)
*/
function getDefines(
type: 'main' | 'preload' | 'renderer',
env: ConfigEnv
): Record<string, unknown> {
/* eslint-disable @typescript-eslint/naming-convention */
return jsonify({
BUILD_TYPE: type,
IS_PREVIEW: env.isPreview,
});
/* eslint-enable @typescript-eslint/naming-convention */
}
/**
* Provide "obfuscated" class names (just the hash)
*/
// @ts-ignore unless explicitly set to be used, this will fail as "declared byt never read" in TSC
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getHashedScopedName(prefix = '', hashLength = 8) {
return (className: string, resourcePath: string): string => {
const hash = getHash(resourcePath, className, hashLength);
return `${prefix}${hash}`;
};
}
/**
* Provide nice class names used by CSS modules
*
* The default classnames are like `${CLASSNAME_HASH}, which might be confusing in development
* This provides `${FILEPATH_CLASSNAME_HASH}` for easier debugging in development
*
* The difference with using `[name]__[local]__[hash:base64:5]` is that `[name]` translates to
* something like `app-module` in a file like `app.module.scss` which doesn't provide much
* information and the `module` part is redundant as it's going to exist everywhere *
*/
function getNiceScopedName(prefix = '', hashLength = 5) {
// ideally, every `.module.scss` file will be part of a react component
const components = join(__dirname, 'src', 'renderer', 'components');
return (className: string, resourcePath: string): string => {
const hash = getHash(resourcePath, className, hashLength);
const rel = basename(relative(components, resourcePath)).replaceAll(sep, '-');
const folder = basename(dirname(resourcePath));
const resourceFilename = basename(resourcePath).replace(/\.module\.((c|sa|sc)ss)$/i, '');
const filename =
rel === basename(resourcePath)
? // if the file was not in components (couldn't resolve relative)
resourceFilename
: resourceFilename === folder || /^(index|styles?)$/.test(resourceFilename)
? // if the filename is the same as the folder (i.e. app/app.module.scss)
resourceFilename
: // if the filename is not the same as the folder (i.e. app/foo.module.scss)
`${rel}-${resourceFilename}`;
return `${prefix}${filename}__${className}__${hash}`;
};
}
function getHash(resourcePath: string, className: string, length: number): string {
const hashContent = `filepath:${resourcePath}|classname:${className}`;
const hash = createHash('md5')
.update(hashContent)
.digest('base64')
// base64 can include "+" and "/" which are not acceptable for css class names
.replace(/[+]/g, 'x')
.replace(/[/]/g, 'X');
return hash.substring(0, length);
}
/**
* Returns an object with the same fields as the provided `obj` but with every
* value stringified in json
*/
function jsonify<T extends Record<string, unknown>>(obj: T): Record<keyof T, string> {
return Object.entries(obj).reduce(
(res, [key, value]) => {
res[key as keyof T] = JSON.stringify(value);
return res;
},
{} as Record<keyof T, string>
);
}