Webpack interview questions

31 questions from the Webpack: Module Bundling and Build Optimization course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting started

1. What is the primary purpose of bundling?

  1. Make code harder to understand.
  2. Reduce HTTP requests and improve load time.
  3. Add extra features to modules.
  4. Replace JavaScript with a different language.
Show answer

Answer: Reduce HTTP requests and improve load time.

Bundling combines multiple modules into fewer files, reducing network overhead and speeding up application load.

Lesson: Introduction to bundling

2. What does [contenthash] do in output filename?

  1. Compresses the file automatically.
  2. Adds metadata to the bundle.
  3. Selects which loader processes the file.
  4. Generates unique hash based on file content for cache busting.
Show answer

Answer: Generates unique hash based on file content for cache busting.

Content hash ensures cache busting by changing the filename when content changes, forcing fresh downloads.

Lesson: Core concepts: entry and output

Module 2: Foundations

3. In which direction do loaders execute in the 'use' array?

  1. Right to left
  2. Left to right
  3. Random order
  4. Alphabetical order
Show answer

Answer: Right to left

Loaders execute from right to left, so the rightmost loader runs first and passes output to the next loader.

Lesson: Loaders and rules

4. What is the main difference between loaders and plugins?

  1. Plugins process files; loaders extend lifecycle.
  2. Loaders transform specific files; plugins hook into build lifecycle.
  3. They do the same thing differently.
  4. Plugins only run after the build finishes.
Show answer

Answer: Loaders transform specific files; plugins hook into build lifecycle.

Loaders work on individual files, while plugins hook into the Webpack compilation process to perform broader operations.

Lesson: Plugins and extensions

5. When should you use production mode?

  1. During development for fast rebuilds.
  2. To enable better debugging.
  3. For final deployments to minimize bundle size.
  4. Only when running unit tests.
Show answer

Answer: For final deployments to minimize bundle size.

Production mode optimizes for size and performance, making bundles suitable for deployment. Development mode prioritizes rebuild speed and debugging.

Lesson: Configuration and modes

Module 3: Core mechanics

6. What does module resolution do?

  1. Fixes errors in module code.
  2. Converts modules to different formats.
  3. Uploads modules to the npm registry.
  4. Finds and loads modules according to Webpack's algorithm.
Show answer

Answer: Finds and loads modules according to Webpack's algorithm.

Resolution is the process of finding a module file when you import it, following Webpack's configured search paths and extensions.

Lesson: Module resolution

7. What is tree-shaking?

  1. Shaking the dependency tree to find bugs.
  2. Removing unused code from the bundle.
  3. Reordering imports in files.
  4. Renaming files to shorter names.
Show answer

Answer: Removing unused code from the bundle.

Tree-shaking eliminates unused exports in production builds, reducing bundle size by removing dead code.

Lesson: Tree-shaking and code splitting

Module 4: Transforming assets

8. What is the advantage of extracting CSS?

  1. Reduces CSS file size.
  2. Prevents CSS from being used.
  3. Enables caching and parallel downloads.
  4. Removes the need for CSS-in-JS.
Show answer

Answer: Enables caching and parallel downloads.

Extracted CSS files can be cached separately and downloaded in parallel with JavaScript, improving load performance.

Lesson: CSS and styling

9. When should images be inlined?

  1. All images should always be inlined.
  2. Large images should be inlined for compression.
  3. Never, because inlining breaks the build.
  4. When the files are small, to save extra HTTP requests.
Show answer

Answer: When the files are small, to save extra HTTP requests.

Small files are inlined as base64 to save HTTP requests. Large files stay separate for better caching.

Lesson: Images and assets

Module 5: Development workflow

10. What does HMR do?

  1. Replaces modules without full page reload.
  2. Compresses bundles automatically.
  3. Minifies JavaScript code.
  4. Restarts the Node.js process on every change.
Show answer

Answer: Replaces modules without full page reload.

HMR (Hot Module Replacement) updates modules in the running application without requiring a full page reload, preserving state.

Lesson: Dev server and hot reload

11. What do source maps provide?

  1. Compression of JavaScript files.
  2. Mapping from bundled code back to original sources.
  3. Security protection for code.
  4. Automatic bug fixing at runtime.
Show answer

Answer: Mapping from bundled code back to original sources.

Source maps enable debugging by mapping minified/bundled code back to original source files for readability.

Lesson: Debugging and source maps

Module 6: Practical usage

12. Which target should you use for a Node.js library?

  1. 'web' for browser compatibility.
  2. 'electron' for cross-platform.
  3. 'node' to enable Node built-ins like fs.
  4. 'webworker' to avoid Node built-ins.
Show answer

Answer: 'node' to enable Node built-ins like fs.

The 'node' target ensures Node.js built-ins like fs and path are available and not bundled into the output.

Lesson: Building for different targets

13. What is a benefit of using framework CLIs?

  1. Faster runtime performance.
  2. Smaller bundle sizes guaranteed.
  3. They remove the need for Node.js.
  4. Pre-configured Webpack for the framework's needs.
Show answer

Answer: Pre-configured Webpack for the framework's needs.

Framework CLIs provide pre-configured Webpack setups optimized for the specific framework, avoiding manual configuration.

Lesson: Working with frameworks

Module 7: Optimization

14. What is minification?

  1. Removing unnecessary characters while preserving functionality.
  2. Making code harder to understand.
  3. Splitting bundles into smaller pieces.
  4. Encrypting the bundle for security.
Show answer

Answer: Removing unnecessary characters while preserving functionality.

Minification reduces file size by removing whitespace, comments, and shortening variable names without changing what the code does.

Lesson: Minification and compression

15. Why separate vendor code?

  1. Vendor code must be in a separate file.
  2. Vendors change rarely, can be cached longer.
  3. It makes bundles smaller in size.
  4. Webpack requires vendor files to be separate.
Show answer

Answer: Vendors change rarely, can be cached longer.

Vendor code changes infrequently, so separating it allows browsers to cache it indefinitely while application code updates frequently.

Lesson: Caching strategies

Module 8: Advanced patterns

16. What does a loader receive as input?

  1. The file path only.
  2. Compiled JavaScript output.
  3. Source code content of a file.
  4. The browser DOM tree.
Show answer

Answer: Source code content of a file.

Loaders receive the file content as a string and return transformed content, enabling flexible file processing.

Lesson: Custom loaders and plugins

17. Why configure resolve.modules for monorepos?

  1. To speed up bundling process.
  2. To compress shared dependencies.
  3. To disable symlinks entirely.
  4. To find packages in workspace locations besides node_modules.
Show answer

Answer: To find packages in workspace locations besides node_modules.

resolve.modules tells Webpack where to search for packages, enabling resolution of local workspace packages.

Lesson: Monorepos and workspaces

Module 9: Integration

18. What does fork-ts-checker-webpack-plugin do?

  1. Runs type checking in parallel for faster builds.
  2. Removes TypeScript from the bundle.
  3. Enables decorators in TypeScript.
  4. Converts TypeScript to Python.
Show answer

Answer: Runs type checking in parallel for faster builds.

fork-ts-checker-webpack-plugin offloads type checking to a separate process, allowing faster compilation during development.

Lesson: TypeScript and transpilation

19. Why analyze bundles in CI?

  1. To make builds faster.
  2. To catch performance regressions early.
  3. To reduce deployment time.
  4. To automatically fix lint errors.
Show answer

Answer: To catch performance regressions early.

Bundle analysis in CI detects size increases and performance issues before code reaches production.

Lesson: Testing and CI/CD integration

Module 10: Revision

20. What's a common Webpack pitfall?

  1. Using too many loaders.
  2. Minification is always bad.
  3. Wrong loader order causing transformation failures.
  4. Running builds in CI at all.
Show answer

Answer: Wrong loader order causing transformation failures.

Loader order is critical because they execute right-to-left, and incorrect order breaks the transformation pipeline.

Lesson: Common pitfalls and solutions

21. How do you measure build performance?

  1. Count the number of files in bundle.
  2. Check console logs during build.
  3. Ask teammates how long it feels.
  4. Use --profile flag or speed-measure-webpack-plugin.
Show answer

Answer: Use --profile flag or speed-measure-webpack-plugin.

The --profile flag and speed-measure-webpack-plugin provide detailed timing information for each build step.

Lesson: Performance monitoring and profiling

22. When should you use Vite over Webpack?

  1. For fast dev experience with simpler projects.
  2. When you need maximum customization.
  3. For production builds only.
  4. When you must support Internet Explorer 6.
Show answer

Answer: For fast dev experience with simpler projects.

Vite excels with fast development experience through native ESM, making it ideal for new projects. Webpack is better for projects needing extensive customization.

Lesson: Alternative bundlers and migration

23. What makes a configuration production-ready?

  1. Using the largest bundle size.
  2. Minification, sourcemaps, caching, and code splitting.
  3. Disabling all optimizations.
  4. Hard-coded absolute paths and no hashing.
Show answer

Answer: Minification, sourcemaps, caching, and code splitting.

Production-ready configs optimize for performance, caching, debugging, and reliability through minification, code splitting, and proper sourcemaps.

Lesson: Real-world applications and best practices

24. Which option tells Webpack where to start building the dependency graph?

  1. output
  2. entry
  3. resolve
  4. devtool
Show answer

Answer: entry

The entry point is the root from which Webpack follows imports.

Lesson: Real-world applications and best practices

25. Which loader order correctly processes CSS in a rule's use array?

  1. ['css-loader', 'style-loader']
  2. ['style-loader', 'css-loader']
  3. ['css-loader'] only
  4. ['babel-loader', 'style-loader']
Show answer

Answer: ['style-loader', 'css-loader']

Loaders run right to left, so css-loader parses first and style-loader injects the result.

Lesson: Real-world applications and best practices

26. What is the main difference between a loader and a plugin?

  1. Loaders hook into the whole build lifecycle; plugins transform single files.
  2. Loaders transform individual files; plugins hook into the build lifecycle.
  3. Plugins run only in development.
  4. There is no difference.
Show answer

Answer: Loaders transform individual files; plugins hook into the build lifecycle.

Loaders work per file type; plugins handle broader build tasks.

Lesson: Real-world applications and best practices

27. Which technique removes unused exports from a production bundle?

  1. Tree-shaking
  2. Hot module replacement
  3. Source mapping
  4. Aliasing
Show answer

Answer: Tree-shaking

Tree-shaking relies on static ES module imports to drop dead code.

Lesson: Real-world applications and best practices

28. How do you load a heavy module only when the user needs it?

  1. Use a dynamic import()
  2. Use a static import at the top
  3. Set mode to development
  4. Rename the entry file
Show answer

Answer: Use a dynamic import()

Dynamic import() creates a separate chunk that loads on demand.

Lesson: Real-world applications and best practices

29. Why use [contenthash] in output filenames?

  1. It encrypts the bundle.
  2. It changes only when content changes, enabling long-term caching.
  3. It speeds up the dev server.
  4. It enables source maps.
Show answer

Answer: It changes only when content changes, enabling long-term caching.

An unchanged file keeps its name, so browsers reuse the cached copy.

Lesson: Real-world applications and best practices

30. What does HMR do in webpack-dev-server?

  1. Minifies code on save.
  2. Replaces changed modules without a full page reload.
  3. Uploads source maps to production.
  4. Splits vendor chunks.
Show answer

Answer: Replaces changed modules without a full page reload.

HMR swaps updated modules in place and preserves application state.

Lesson: Real-world applications and best practices

31. Which tool helps you find what is making a bundle large?

  1. webpack-bundle-analyzer
  2. html-webpack-plugin
  3. css-loader
  4. fork-ts-checker-webpack-plugin
Show answer

Answer: webpack-bundle-analyzer

The analyzer visualises bundle composition so large dependencies stand out.

Lesson: Real-world applications and best practices

← Back to the course