This repository contains a JavaScript Code Minifier example. Follow these steps to minify your JavaScript code.
Clone this repository using the following command:
git clone https://github.com/AkashFulari/JSCode-Minifier.git
Initialize the project and install the necessary dependencies:
npm install
To minify your JavaScript code file, replace scripts/script.js with YOUR_JS_FILE.JS. Add your JavaScript code content into YOUR_JS_FILE.JS.
Create a configuration file named webpack.config.js in your current git repository and add the following content:
// webpack.config.js
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
entry: './src/script.js', // replace your script file absolute location here
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-library.min.js', // replace your export library file name here
library: 'AKLib', // replace export library name here
libraryTarget: 'umd',
globalObject: 'this',
},
mode: 'production',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: true, // Mangle variable and function names
},
}),
],
},
};
To minify your library or code, use the following command:
npx webpack --config webpack.config.js
This will generate a minified and obfuscated version of your library in the dist directory.
Create an index.html file to include your minified library and use the exported class.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Player Library</title>
</head>
<body>
<script src="dist/my-library.min.js"></script>
<script>
// Access the exported class from the MyLibrary global object
const conetnt = new AKLib.displayMessage();
console.log(content);
</script>
</body>
</html>
Open the index.html file in a web browser. You should see the console log as your Hello guys!!! content.
THANK YOU!!!.