Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix production build not loading configuration properly #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ CODETOTAL_WS_HOST=127.0.0.1
DEBUG_MODULES=actions,megalinter,stores,transport

# FRONTEND
UPLOAD_FILE_LIMIT_BYTES=10000000 # 10MB
CODETOTAL_UPLOAD_FILE_LIMIT_BYTES=10000000

4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "npm run build --workspaces",
"postbuild": "mv ./packages/backend/dist . && mkdir -p ./dist/public && mv ./packages/app/dist/* ./dist/public",
"clean": "rm -rf dist && npm run clean --workspaces",
"production": "node dist/index.js",
"production": "node -r dotenv/config dist/index.js dotenv_config_path=./.env dotenv_config_debug=true",
"build:be": "npm run build:st && npm run build --workspace=packages/backend",
"build:st": "npm run build --workspace=packages/shared-types",
"start:be": "npm start --workspace=packages/backend",
Expand Down
2 changes: 2 additions & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"zustand": "^4.3.8"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/md5": "^2.3.2",
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
Expand All @@ -39,6 +40,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"eslint-plugin-tss-unused-classes": "^0.0.4",
"express": "^4.18.2",
"typescript": "^5.0.2",
"vite": "^4.3.9",
"vite-plugin-checker": "^0.6.1"
Expand Down
10 changes: 4 additions & 6 deletions packages/app/src/analysis/components/FileUploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import config from "../../config";
import { startAnalysis } from "../actions/analysis-actions";
import { AnalysisStore } from "../stores/analysis-store";

const MAX_SIZE = parseInt(config.CODETOTAL_UPLOAD_FILE_LIMIT_BYTES);

export const FileUploadForm: FC = () => {
const { classes } = useStyles();
const navigate = useNavigate();
Expand All @@ -24,11 +26,7 @@ export const FileUploadForm: FC = () => {
fileRejections.length === 1 &&
fileRejections[0].errors[0].code === "file-too-large"
) {
setError(
`File too large (max allowed: ${filesize(
config.UPLOAD_FILE_LIMIT_BYTES
)})`
);
setError(`File too large (max allowed: ${filesize(MAX_SIZE)})`);
return;
}
if (fileRejections.length === 1) {
Expand All @@ -46,7 +44,7 @@ export const FileUploadForm: FC = () => {
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
multiple: false,
maxSize: config.UPLOAD_FILE_LIMIT_BYTES,
maxSize: MAX_SIZE,
});

return (
Expand Down
29 changes: 14 additions & 15 deletions packages/app/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
declare const process: {
env: {
CODETOTAL_HTTP_PORT: string;
CODETOTAL_HTTP_HOST: string;
CODETOTAL_WS_PORT: number;
CODETOTAL_WS_HOST: number;
UPLOAD_FILE_LIMIT_BYTES: number;
};
};
interface FEConfig {
CODETOTAL_HTTP_PORT: string;
CODETOTAL_HTTP_HOST: string;
CODETOTAL_WS_PORT: string;
CODETOTAL_WS_HOST: string;
CODETOTAL_UPLOAD_FILE_LIMIT_BYTES: string;
}

const config = {
CODETOTAL_HTTP_PORT: process.env.CODETOTAL_HTTP_PORT,
CODETOTAL_HTTP_HOST: process.env.CODETOTAL_HTTP_HOST,
CODETOTAL_WS_PORT: process.env.CODETOTAL_WS_PORT,
CODETOTAL_WS_HOST: process.env.CODETOTAL_WS_HOST,
UPLOAD_FILE_LIMIT_BYTES: process.env.UPLOAD_FILE_LIMIT_BYTES,
const config: FEConfig = {
CODETOTAL_HTTP_PORT: import.meta.env.CODETOTAL_HTTP_PORT,
CODETOTAL_HTTP_HOST: import.meta.env.CODETOTAL_HTTP_HOST,
CODETOTAL_WS_PORT: import.meta.env.CODETOTAL_WS_PORT,
CODETOTAL_WS_HOST: import.meta.env.CODETOTAL_WS_HOST,
CODETOTAL_UPLOAD_FILE_LIMIT_BYTES: import.meta.env
.CODETOTAL_UPLOAD_FILE_LIMIT_BYTES,
};

export default config;
3 changes: 1 addition & 2 deletions packages/app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2020",
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
Expand All @@ -23,6 +23,5 @@
"declaration": false
},
"include": ["src/**/*"],
"exclude": ["vite.config.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
2 changes: 1 addition & 1 deletion packages/app/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts", "src/config.ts"]
"include": ["vite.config.ts"]
}
14 changes: 2 additions & 12 deletions packages/app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/* eslint-disable import/first */
import dotenv from "dotenv";

// load ENV variables from .env file
dotenv.config({ path: "../../.env" });

import react from "@vitejs/plugin-react";
import { PluginOption, defineConfig } from "vite";
import checker from "vite-plugin-checker";
import config from "./src/config";

export default defineConfig(({ command }) => {
const plugins: PluginOption[] = [react()];
Expand All @@ -23,6 +16,8 @@ export default defineConfig(({ command }) => {
}

return {
envPrefix: "CODETOTAL_",
envDir: "../../",
mode: "production",
server: {
port: 3000,
Expand All @@ -41,11 +36,6 @@ export default defineConfig(({ command }) => {
},
},
},
define: {
"process.env": JSON.stringify({
...config,
}),
},
esbuild: {
// needed for keepNames
minifyIdentifiers: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"types": "./src/index.ts",
"main": "./dist/index.js",
"scripts": {
"start": "nodemon ./src/index.ts",
"start": "nodemon -r dotenv/config ./src/index.ts dotenv_config_path=../../.env dotenv_config_debug=true",
"build": "tsc",
"test": "jest",
"start:test": "jest --coverage --watch",
Expand Down
4 changes: 0 additions & 4 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import dotenv from "dotenv";
// load ENV variables from .env file
dotenv.config({ path: "../../.env" });

import { subscribeToReport } from "./actions/subscribe-to-report";
import config from "./config";
import { startHttpServer } from "./transport/http-server";
Expand Down
2 changes: 2 additions & 0 deletions packages/shared-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"author": "Itay",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.17",
"express": "^4.18.2",
"typescript": "^5.1.3"
}
}
3 changes: 2 additions & 1 deletion packages/shared-types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"outDir": "./dist",
"target": "ES5",
"moduleResolution": "nodenext",
"sourceMap": true
"sourceMap": true,
"module": "CommonJS"
},
"include": ["src/index.ts"],
"exclude": ["node_modules", "./dist"]
Expand Down