JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

What is Webpack and why is it used?

Asked on Sep 15, 2024

Answer

Webpack is a powerful module bundler for JavaScript applications. It is used to bundle JavaScript files for usage in a browser and can also transform, bundle, or package any resource or asset.
<!-- BEGIN COPY / PASTE -->
        // Webpack configuration example
        const path = require('path');

        module.exports = {
            entry: './src/index.js',
            output: {
                filename: 'bundle.js',
                path: path.resolve(__dirname, 'dist')
            },
            module: {
                rules: [
                    {
                        test: /\.css$/,
                        use: ['style-loader', 'css-loader']
                    }
                ]
            }
        };
        <!-- END COPY / PASTE -->
Additional Comment:
  • Webpack's "entry" property specifies the entry point of your application.
  • The "output" property defines the output file and location.
  • The "module" property allows you to define loaders, which transform files before they are added to the bundle.
  • Webpack can handle not only JavaScript but also CSS, images, and other assets.
  • It is commonly used in modern web development to optimize and manage dependencies efficiently.
✅ Answered with JavaScript best practices.
← Back to All Questions