Introduction

At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.

Core Concepts

Webpack has several core concepts that you should understand:

Entry

An entry point indicates which module webpack should use to begin building out its internal dependency graph. Webpack will figure out which other modules and libraries that entry point depends on (directly and indirectly).

By default its value is ./src/index.js, but you can specify a different (or multiple) entry points by setting an entry property in the webpack configuration.

Output

The output property tells webpack where to emit the bundles it creates and how to name these files. It defaults to ./dist/main.js for the main output file and to the ./dist folder for any other generated file.

Loaders

Out of the box, webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.

Plugins

While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.

Mode

By setting the mode parameter to either development, production or none, you can enable webpack's built-in optimizations that correspond to each environment. The default value is production.

Browser Compatibility

Webpack supports all browsers that are ES5-compliant (IE8 and below are not supported). Webpack needs Promise for import() and require.ensure(). If you want to support older browsers, you will need to load a polyfill before using these expressions.

Webpack 4 vs Webpack 5

Major Version Differences

FeatureWebpack 4Webpack 5
Node.js RequirementNode.js 6.x.x or laterNode.js 10.13.0 or later
Module SyntaxES6, CommonJS, AMDES6, CommonJS, AMD (improved harmony support)
Configurationmodule.exports = {}ES modules support (export default {})
CachingBasic cachingPersistent caching with default cache directory
Module FederationNot availableBuilt-in Module Federation for微前端 (micro-frontend)
Asset ManagementRequires file-loader, url-loaderBuilt-in Asset Modules (resource, inline, emitted)
Code SplittingDynamic imports, require.ensureImproved dynamic imports with fetch priority controls
Chunk ManagementChunk IDs based on dependency graphChunk IDs using deterministic hashes by default
Source MapsVarious optionsImproved source map options, hidden-source-map
Module Variables__webpack_require__, __webpack_modules__Same, but with better optimization

Key Changes from Webpack 4 to Webpack 5

1. Module Federation (Webpack 5)

Webpack 5 introduced Module Federation, a feature that allows sharing code between different builds at runtime. This is particularly useful for micro-frontend architectures.

Webpack 4 equivalent: Required third-party plugins like @ awesome-cdk/webpack-remote or complex workarounds.

2. Persistent Caching

Webpack 5 introduced persistent caching by default, which stores cached builds to the file system. This significantly speeds up rebuild times.

Webpack 4: Required cache-loader or similar plugins for persistent caching.

3. Asset Modules

Webpack 5 replaced the need for file-loader, url-loader, and raw-loader with built-in Asset Modules:

// Webpack 4 (requires file-loader)
{
  test: /\.(png|jpg|gif)$/,
  use: 'file-loader'
}

// Webpack 5 (built-in)
{
  test: /\.(png|jpg|gif)$/,
  type: 'asset/resource'
}

4. ES Modules Support

Webpack 5 improved support for ES modules, including:

  • Better handling of import() expressions
  • Improved tree shaking for ES modules
  • Better module detection (sideEffects)

5. Module Variables

Both versions support module variables like __webpack_require__ and __webpack_modules__, but Webpack 5 provides better optimization and more consistent behavior.

6. Chunk Management

Webpack 4: Chunk IDs were based on the dependency graph size, which could be unpredictable.

Webpack 5: Chunk IDs use deterministic hashes by default, providing more consistent and predictable chunk naming.

7. Configuration Syntax

Webpack 4: Required CommonJS syntax (module.exports = {})

Webpack 5: Supports both CommonJS and ES modules (export default {}):

// Webpack 5 with ES modules
export default {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  }
};

Migration Considerations

When migrating from Webpack 4 to Webpack 5:

  1. Update Node.js to version 10.13.0 or later
  2. Replace loaders with Asset Modules for images and files
  3. Configure persistent caching if not using defaults
  4. Update plugins that may have been replaced by built-in features
  5. Review chunk naming strategies as defaults have changed
  6. Update dependencies that may have compatibility issues

For detailed migration information, refer to the official Webpack migration guide for version 5.