Interview Questions

Get ready for your next interview with our comprehensive question library

Webpack Interview Questions

Filter by Difficulty

1.

Explain the core concepts of Webpack

beginner

Webpack has four core concepts:

  1. Entry: The starting point where Webpack begins building the dependency graph
  2. Output: Where and how to emit the bundles
  3. Loaders: Transform files from different languages/formats into modules
  4. Plugins: Extend Webpack's functionality for optimization, asset management, etc.
module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [{ test: /\.js$/, use: 'babel-loader' }]
  },
  plugins: [new HtmlWebpackPlugin()]
};
2.

Explain the basic structure of a Webpack configuration file

beginner

A basic webpack.config.js structure:

const path = require('path');

module.exports = {
  mode: 'development', // or 'production'
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      // Loader configurations
    ]
  },
  plugins: [
    // Plugin instances
  ],
  resolve: {
    // Module resolution options
  },
  devServer: {
    // Dev server configuration
  }
};
3.

What are loaders and how do they work?

beginner

Loaders transform files from different languages or formats into modules that Webpack can understand. They run during the build process and transform files on a per-file basis.

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  }
};

Loaders are processed right-to-left (or bottom-to-top) in the use array.

4.

Explain the difference between style-loader and css-loader

beginner

css-loader:

  • Interprets @import and url() like import/require()
  • Resolves CSS dependencies and returns CSS as a string

style-loader:

  • Injects CSS into the DOM by adding <style> tags
  • Usually used after css-loader in the chain
// This processes CSS files and injects them into the DOM
{
  test: /\.css$/,
  use: ['style-loader', 'css-loader']
}

For production, you might use MiniCssExtractPlugin.loader instead of style-loader to extract CSS into separate files.

5.

What are plugins and how do they differ from loaders?

beginner

Plugins:

  • Perform actions on the entire bundle or compilation
  • Can modify the build process, add files, or optimize output
  • Work at the bundle level

Loaders:

  • Transform individual files during the build process
  • Work at the file level
  • Process files before they're added to the bundle
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
};
6.

What is HtmlWebpackPlugin and why is it useful?

beginner

HtmlWebpackPlugin automatically generates HTML files that include your Webpack bundles. It's useful because:

  • Automatic injection: Automatically adds script and link tags for your bundles
  • Template support: Can use HTML templates with variables
  • Multiple pages: Can generate multiple HTML files
  • Cache busting: Works with filename hashing for cache invalidation
new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  chunks: ['app'], // Only include specific chunks
  minify: process.env.NODE_ENV === 'production'
})
7.

How do you configure webpack-dev-server?

beginner
module.exports = {
  devServer: {
    static: './dist',
    port: 3000,
    open: true,
    hot: true,
    compress: true,
    historyApiFallback: true, // For SPA routing
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true
      }
    }
  }
};

Key options:

  • static: Serve static files from directory
  • hot: Enable HMR
  • proxy: Proxy API requests to another server
  • historyApiFallback: Handle client-side routing
8.

What is the difference between Webpack and other bundlers like Rollup or Parcel?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
9.

How does Webpack's dependency graph work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
10.

What are multiple entry points and when would you use them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

How do you configure different environments (development vs production)?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

How do you handle different file types (images, fonts, etc.) in Webpack?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

What is the purpose of babel-loader and how do you configure it?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

Explain some commonly used Webpack plugins

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How do you extract CSS into separate files?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What is code splitting and why is it important?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

How do you implement dynamic imports for code splitting?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is tree shaking and how does it work in Webpack?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is Hot Module Replacement (HMR) and how does it work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What are source maps and how do you configure them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 49 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime