Skip to content

Commit 8cce5bf

Browse files
fehmerMiodec
andauthored
build: combine vite config into a single file (@fehmer) (monkeytypegame#7190)
- **build: replace dotenv with vite env variables (@fehmer)** - **build: combine vite config into a single file (@fehmer)** --------- Co-authored-by: Miodec <[email protected]>
1 parent b746ef8 commit 8cce5bf

File tree

11 files changed

+281
-269
lines changed

11 files changed

+281
-269
lines changed

.github/labeler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ packages:
1818
- any: ["packages/**/*"]
1919

2020
local dev:
21-
- any: ["**/turbo.json", "**/tsconfig.json", "**/knip.json", "**/.prettierrc", "**/.oxlintrc.json", "**/.eslintrc.cjs", "**/vite.config.dev.js"]
21+
- any: ["**/turbo.json", "**/tsconfig.json", "**/knip.json", "**/.prettierrc", "**/.oxlintrc.json", "**/.eslintrc.cjs"]

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"@vitest/coverage-v8": "4.0.8",
5959
"autoprefixer": "10.4.20",
6060
"concurrently": "8.2.2",
61-
"dotenv": "16.4.5",
6261
"eslint": "8.57.1",
6362
"eslint-plugin-compat": "6.0.2",
6463
"firebase-tools": "13.15.1",

frontend/tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"virtual:env-config": ["./src/ts/types/virtual-env-config.d.ts"]
1616
}
1717
},
18-
"include": ["./src/**/*.ts", "./scripts/**/*.ts", "vite-plugins/**/*.ts"],
18+
"include": [
19+
"./src/**/*.ts",
20+
"./scripts/**/*.ts",
21+
"vite-plugins/**/*.ts",
22+
"vite.config.ts"
23+
],
1924
"exclude": ["node_modules", "build", "setup-tests.ts", "**/*.spec.ts"]
2025
}
Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
import { Plugin } from "vite";
22
import { EnvConfig } from "virtual:env-config";
3-
import { config as dotenvConfig } from "dotenv";
4-
5-
const envFile =
6-
process.env["NODE_ENV"] === "production" ? ".env.production" : ".env";
7-
dotenvConfig({ path: envFile });
83

94
const virtualModuleId = "virtual:env-config";
105
const resolvedVirtualModuleId = "\0" + virtualModuleId;
116

12-
const developmentConfig: EnvConfig = {
13-
isDevelopment: true,
14-
backendUrl: fallbackEnv("BACKEND_URL", "http://localhost:5005"),
15-
clientVersion: "DEVELOPMENT_CLIENT",
16-
recaptchaSiteKey: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI",
17-
quickLoginEmail: process.env["QUICK_LOGIN_EMAIL"],
18-
quickLoginPassword: process.env["QUICK_LOGIN_PASSWORD"],
19-
};
20-
const productionConfig: Omit<EnvConfig, "clientVersion"> = {
21-
isDevelopment: false,
22-
backendUrl: fallbackEnv("BACKEND_URL", "https://api.monkeytype.com"),
23-
recaptchaSiteKey: process.env["RECAPTCHA_SITE_KEY"] ?? "",
24-
quickLoginEmail: undefined,
25-
quickLoginPassword: undefined,
26-
};
27-
28-
export function envConfig(
29-
options:
30-
| {
31-
isDevelopment: true;
32-
}
33-
| {
34-
isDevelopment: false;
35-
clientVersion: string;
36-
},
37-
): Plugin {
7+
export function envConfig(options: {
8+
isDevelopment: boolean;
9+
clientVersion: string;
10+
env: Record<string, string>;
11+
}): Plugin {
3812
return {
3913
name: "virtual-env-config",
4014
resolveId(id) {
@@ -43,13 +17,31 @@ export function envConfig(
4317
},
4418
load(id) {
4519
if (id === resolvedVirtualModuleId) {
46-
const envConfig = options.isDevelopment
47-
? developmentConfig
48-
: {
49-
...productionConfig,
50-
clientVersion: options.clientVersion,
51-
};
20+
const devConfig: EnvConfig = {
21+
isDevelopment: true,
22+
backendUrl: fallback(
23+
options.env["BACKEND_URL"],
24+
"http://localhost:5005",
25+
),
26+
clientVersion: options.clientVersion,
27+
recaptchaSiteKey: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI",
28+
quickLoginEmail: options.env["QUICK_LOGIN_EMAIL"],
29+
quickLoginPassword: options.env["QUICK_LOGIN_PASSWORD"],
30+
};
31+
32+
const prodConfig: EnvConfig = {
33+
isDevelopment: false,
34+
backendUrl: fallback(
35+
options.env["BACKEND_URL"],
36+
"https://api.monkeytype.com",
37+
),
38+
recaptchaSiteKey: options.env["RECAPTCHA_SITE_KEY"] ?? "",
39+
quickLoginEmail: undefined,
40+
quickLoginPassword: undefined,
41+
clientVersion: options.clientVersion,
42+
};
5243

44+
const envConfig = options.isDevelopment ? devConfig : prodConfig;
5345
return `
5446
export const envConfig = ${JSON.stringify(envConfig)};
5547
`;
@@ -59,8 +51,7 @@ export function envConfig(
5951
};
6052
}
6153

62-
function fallbackEnv(envVariable: string, fallback: string): string {
63-
const value = process.env[envVariable];
54+
function fallback(value: string | undefined | null, fallback: string): string {
6455
if (value === null || value === undefined || value === "") return fallback;
6556
return value;
6657
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Plugin } from "vite";
2+
import MagicString from "magic-string";
3+
4+
export function jqueryInject(): Plugin {
5+
return {
6+
name: "simple-jquery-inject",
7+
async transform(src: string, id: string) {
8+
if (id.endsWith(".ts")) {
9+
//check if file has a jQuery or $() call
10+
if (/(?:jQuery|\$)\([^)]*\)/.test(src)) {
11+
const s = new MagicString(src);
12+
13+
//if file has "use strict"; at the top, add it below that line, if not, add it at the very top
14+
if (src.startsWith(`"use strict";`)) {
15+
s.appendRight(12, `\nimport $ from "jquery";`);
16+
} else {
17+
s.prepend(`import $ from "jquery";`);
18+
}
19+
20+
return {
21+
code: s.toString(),
22+
map: s.generateMap({ hires: true, source: id }),
23+
};
24+
}
25+
}
26+
return;
27+
},
28+
};
29+
}

frontend/vite-plugins/language-hashes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function languageHashes(options?: { skip: boolean }): Plugin {
1616
load(id) {
1717
if (id === resolvedVirtualModuleId) {
1818
if (options?.skip) {
19-
console.log("Skipping language hashing in dev environment.");
19+
console.log("Skipping language hashing.");
2020
}
2121

2222
const hashes: Record<string, string> = options?.skip ? {} : getHashes();

frontend/vite.config.dev.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

frontend/vite.config.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)