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

Add gzip and brotli to create static files #42

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions tdm-be/package-lock.json

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

1 change: 1 addition & 0 deletions tdm-be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"express": "^4.19.2",
"express-basic-auth": "^1.2.1",
"express-rate-limit": "^7.2.0",
"express-static-gzip": "^2.1.7",
"md5": "^2.3.0",
"multer": "^1.4.5-lts.1",
"node-cron": "^3.0.3",
Expand Down
3 changes: 2 additions & 1 deletion tdm-be/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import cors from 'cors';
import express from 'express';
import basicAuth from 'express-basic-auth';
import rateLimit from 'express-rate-limit';
import expressStaticGzip from 'express-static-gzip';
import cron from 'node-cron';
import swaggerUi from 'swagger-ui-express';

Expand Down Expand Up @@ -62,7 +63,7 @@ app.use('/swagger-config', auth, swaggerUi.serve, swaggerUi.setup(swaggerFile));

const dirname = process.cwd();

app.use(express.static(path.join(dirname, 'public')));
app.use(expressStaticGzip(path.join(dirname, 'public'), { enableBrotli: true }));

// Rewrite reverse proxy, this is required because we use a single page application
// We need to declare all route used by the front application
Expand Down
13 changes: 13 additions & 0 deletions tdm-fe/package-lock.json

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

1 change: 1 addition & 0 deletions tdm-fe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"md5": "^2.3.0",
"rollup-plugin-gzip": "^3.1.2",
"sass": "^1.75.0",
"stylelint": "^16.3.1",
"stylelint-config-standard-scss": "^13.1.0",
Expand Down
41 changes: 31 additions & 10 deletions tdm-fe/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import react from '@vitejs/plugin-react';
import md5 from 'md5';
import gzipPlugin from 'rollup-plugin-gzip';
import { defineConfig } from 'vite';
import eslint from 'vite-plugin-eslint';
import stylelint from 'vite-plugin-stylelint';
import tsconfigPaths from 'vite-tsconfig-paths';

const regex = /(.*node_modules\/)([^\/]+)(.*)/;
import { promisify } from 'node:util';
import { brotliCompress } from 'node:zlib';

const brotliPromise = promisify(brotliCompress);

const regex = /(.*node_modules\/)([^/]+)(.*)/;

const linter =
process.env.VITE_ENV === 'prod'
Expand All @@ -18,12 +24,27 @@ const linter =
];

export default defineConfig({
plugins: [react(), tsconfigPaths(), ...linter],
css: {
devSourcemap: process.env.VITE_SOURCE_MAP === 'true',
},
plugins: [
react(),
tsconfigPaths(),
gzipPlugin({
customCompression: (content) => brotliPromise(Buffer.from(content)),
fileName: '.br',
minSize: 1000,
gzipOptions: {
level: 9,
},
}),
gzipPlugin({
minSize: 1000,
gzipOptions: {
level: 9,
},
}),
...linter,
],
build: {
sourcemap: process.env.VITE_SOURCE_MAP === 'true',
sourcemap: true,
rollupOptions: {
output: {
manualChunks: (id, meta) => {
Expand All @@ -36,13 +57,13 @@ export default defineConfig({
},
minify: 'terser',
terserOptions: {
sourceMap: process.env.VITE_SOURCE_MAP === 'true',
ecma: 2018,
sourceMap: true,
ecma: 2020,
compress: {
ecma: 2018,
ecma: 2020,
},
format: {
ecma: 2018,
ecma: 2020,
},
},
},
Expand Down
Loading