Skip to content

Commit

Permalink
[FEATURE]: configuring gzip and brotli compression
Browse files Browse the repository at this point in the history
  • Loading branch information
helabenkhalfallah committed Jun 4, 2024
1 parent aab25ff commit 45b0199
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ dist
.pnp.*
*.iml
*.idea
**/Bundle.**
**/Bundle.**
**/meta.**
**/build/**
30 changes: 22 additions & 8 deletions esbuild-app/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Importing required modules
import esbuild from 'esbuild'; // esbuild is a JavaScript bundler and minifier. It packages up JavaScript and TypeScript code for distribution on the web.
import fs from 'fs';
import process from 'node:process'; // The process object is a global that provides information about, and control over, the current Node.js process.
import esbuild from 'esbuild'; // esbuild is a JavaScript bundler and minifier. It packages up JavaScript and TypeScript code for distribution on the web.
import { compress } from 'esbuild-plugin-compress';

// Get the command line arguments
const args = process.argv;
Expand All @@ -9,23 +11,31 @@ const args = process.argv;
const config = {
logLevel: 'info', // The level of logging to use
entryPoints: ['src/index.js'], // The entry point of the application
outfile: 'public/build/Bundle.js', // The output file for the bundled code
bundle: true, // Whether to bundle the code or not
loader: { '.js': 'jsx' }, // The loader to use for JavaScript files
define: {
// Define global constants for the bundled code
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'production'), // The NODE_ENV environment variable defines the environment in which an application is running (development, testing, production).
},
};

// If the '--build' argument is passed in, build the application
if (args.includes('--build')) {
esbuild
.build({
...config, // Use the defined configuration
minify: true, // Minify the code
sourcemap: false, // Don't generate a source map
minify: true, // Minify the code
treeShaking: true, // dead code elimination
metafile: true, // stats about bundle
splitting: true, // create chunks
format: 'esm',
outdir: 'public/build',
write: false,
plugins: [
compress({
outputDir: '.',
exclude: ['**/*.map'],
}),
],
})
.then(result => fs.writeFileSync('meta.json', JSON.stringify(result.metafile)))
.catch((e) => {
// If there's an error, log it and exit the process with a status of 1
console.error(e);
Expand All @@ -39,6 +49,7 @@ if (args.includes('--start')) {
.context({
...config, // Use the defined configuration
minify: false, // Don't minify the code
outfile: 'public/build/Bundle.js', // The output file for the bundled code
sourcemap: true, // Generate a source map
})
.then(async (ctx) => {
Expand All @@ -64,4 +75,7 @@ if (args.includes('--start')) {
console.error(e);
process.exit(1);
});
}
}



1 change: 1 addition & 0 deletions esbuild-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@eslint/js": "=9.4.0",
"concurrently": "=8.2.2",
"esbuild": "=0.21.4",
"esbuild-plugin-compress": "^1.0.1",
"eslint": "=9.4.0",
"eslint-plugin-react": "=7.32.2",
"globals": "=15.3.0"
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 45b0199

Please sign in to comment.