-
Notifications
You must be signed in to change notification settings - Fork 42
/
vite.config.common.mjs
193 lines (188 loc) · 6.46 KB
/
vite.config.common.mjs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// vendors
import ncc from "@vercel/ncc";
import * as dotenv from "dotenv";
import merge from "lodash.merge";
import MagicString from "magic-string";
import { checker } from "vite-plugin-checker";
import topLevelAwait from "vite-plugin-top-level-await";
import tsconfigPaths from "vite-tsconfig-paths";
// pkg
import pkg from "./package.json";
export function createSingleFile(patch) {
let currentOutputConfig = {};
return [
{
name: "create-single-file",
apply: "build",
async writeBundle(outputConfig) {
currentOutputConfig = outputConfig;
},
async closeBundle() {
const outFile = path.join(currentOutputConfig.dir, currentOutputConfig.entryFileNames);
if (!outFile.endsWith(".mjs")) {
console.debug("Skipped from bundling", outFile);
return;
}
await new Promise((resolve, reject) => {
ncc(outFile, {
externals: ["electron"],
cache: false,
minify: true,
sourceMap: false,
quiet: false,
target: "es2020",
debugLog: true
})
.then(({ code, map, assets }) => {
// ssh2 issues
const singleFile = outFile;
const s = new MagicString(code);
s.prepend(`
import __path from 'path';
import { fileURLToPath as __fileURLToPath } from 'url';
import { createRequire as __createRequire } from 'module';
if (typeof __dirname === 'undefined') {
const __getFilename = () => __fileURLToPath(import.meta.url);
const __getDirname = () => __path.dirname(__getFilename());
const __dirname = __getDirname();
const __filename = __getFilename();
const self = globalThis;
const require = __createRequire(import.meta.url);
self.__filename = __filename;
self.__dirname = __dirname;
}
`);
fs.writeFileSync(singleFile, patch ? s.toString() : code, "utf8");
Object.keys(assets).forEach((asset) => {
const assetFilePath = path.join(currentOutputConfig.dir, asset);
fs.mkdirSync(path.dirname(assetFilePath), { recursive: true });
fs.writeFileSync(assetFilePath, assets[asset].source);
});
console.debug("Single file generated");
resolve();
})
.catch(reject);
});
console.debug("<<< BUNDLE CLOSED >>>", outFile);
}
}
];
}
// module
export const ENVIRONMENT = process.env.ENVIRONMENT || "development";
export const PROJECT_HOME = path.resolve(__dirname);
export const APP_MAIN = "application";
export const ELECTRON_VENDORS_CACHE_PATH = path.join(PROJECT_HOME, ".electron-vendors.cache.json");
export function getElectronVendorsCache() {
if (fs.existsSync(ELECTRON_VENDORS_CACHE_PATH)) {
const cache = JSON.parse(fs.readFileSync(ELECTRON_VENDORS_CACHE_PATH, "utf8"));
return cache;
}
return { chrome: "128", node: "20" };
}
export function sourceEnv(env) {
// template
dotenv.config({ path: path.join(PROJECT_HOME, ".env") });
dotenv.config({ path: path.join(PROJECT_HOME, ".env.local"), override: true });
// target env
dotenv.config({ path: path.join(PROJECT_HOME, `.env.${env}`), override: true });
dotenv.config({ path: path.join(PROJECT_HOME, `.env.${env}.local`), override: true });
}
export function createEJSContext() {
return {
name: pkg.name,
title: pkg.title,
description: pkg.description,
version: pkg.version,
environment: ENVIRONMENT,
PUBLIC_URL: ".",
PROJECT_VERSION: pkg.version,
PROJECT_TITLE: pkg.title,
PROJECT_DESCRIPTION: pkg.description,
ENVIRONMENT
};
}
export const sourcemap = ENVIRONMENT === "development";
/**
* @type {import('vite').UserConfig}
* @see https://vitejs.dev/config/
*/
export function getCommonViteConfig({ mode, define, resolve, outputName, outputFormat, plugins, rollupOptions }) {
const userDefine = {
// Define default environment variables
...createDefine(mode),
// Define user overridden environment variables
...define,
// Fix
__dirname: "import.meta.dirname"
};
const minify = false; // mode === "production";
const outputExtension = outputFormat == "umd" ? "js" : "mjs";
const config = {
clearScreen: false,
plugins: [
// viteCommonjs(),
topLevelAwait(),
checker({
typescript: true
}),
tsconfigPaths(),
...(plugins ?? [])
],
define: userDefine,
build: {
target: "es2020",
outDir: path.join(__dirname, "build"),
emptyOutDir: false,
sourcemap: sourcemap,
chunkSizeWarningLimit: 50 * 1024,
reportCompressedSize: mode === "production",
minify: minify,
cssMinify: minify,
rollupOptions: merge({}, rollupOptions || {}, {
output: {
manualChunks: (filename) => outputName,
preserveModules: false,
format: outputFormat == "umd" ? "umd" : "es",
inlineDynamicImports: false,
assetFileNames: `assets/${outputName}-${pkg.version}.[ext]`,
entryFileNames: `${outputName}-${pkg.version}.${outputExtension}`,
chunkFileNames: `${outputName}-${pkg.version}.[hash].${outputExtension}`
}
})
},
resolve: merge(
{},
{
alias: {
"@": path.resolve(__dirname, "src")
}
},
resolve ?? {}
)
};
return config;
}
export const createDefine = (mode) => {
// Bootstrap
const define = {
"import.meta.env.TARGET": `"${os.type()}"`,
// Define environment variables
"import.meta.env.NODE_ENV": `"${mode}"`,
"import.meta.env.ENVIRONMENT": JSON.stringify(ENVIRONMENT),
"import.meta.env.PUBLIC_URL": JSON.stringify("."),
"import.meta.env.PROJECT_VERSION": JSON.stringify(pkg.version),
"import.meta.env.PROJECT_NAME": JSON.stringify(pkg.name),
"import.meta.env.PROJECT_TITLE": JSON.stringify(pkg.title),
"import.meta.env.PROJECT_DESCRIPTION": JSON.stringify(pkg.description),
"import.meta.env.ONLINE_API": JSON.stringify(process.env.ONLINE_API),
// Features
"import.meta.env.FEATURE_WSL_RELAY_METHOD": JSON.stringify(process.env.FEATURE_WSL_RELAY_METHOD),
// Bugs
"process.env.NODE_DEBUG": JSON.stringify(false)
};
return define;
};