Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Entry, Output and Loaders

Output and Filenames

Control where Webpack writes files and how they are named: output.path, filename templates, [contenthash], publicPath, clean, chunkFilename and building a library.

The output section tells Webpack where to write the bundles and what to call them. It looks small, but it decides three important things: where files land on disk, what URL the browser uses to fetch them, and whether browsers can cache them safely.

JavaScript
const path = require('node:path');

module.exports = {
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[contenthash].js',
    chunkFilename: '[name].[contenthash].chunk.js',
    publicPath: '/',
    clean: true,
  },
};

path and filename

  • output.path is the absolute folder on disk (default dist).
  • output.filename is a template for entry bundles. Placeholders are replaced per file.
Placeholder Meaning
[name] the entry name (main, home, ...)
[id] the internal chunk id
[contenthash] a hash of the file's content
[fullhash] a hash of the whole build
[ext] the extension (asset modules)
[path], [base] folder / file name (asset modules)

You can add a hash length: [contenthash:8] keeps eight characters, which is plenty and shortens URLs.

Node.js Stand-in: fill in a filename template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Why content hashes matter

Browsers and CDNs cache files by URL. If main.js never changes its name, you must choose between caching it (users see stale code after a deploy) and not caching it (every visit re-downloads). With [contenthash] the URL changes only when the content changes, so you can send Cache-Control: max-age=31536000, immutable for every hashed file and still ship updates instantly: the HTML points to the new name. Chapter 6 builds on this in the caching lessons.

A related rule: [contenthash] is stable only if other things do not leak into the file. Webpack's runtime and module ids can change a hash, which is why production mode uses deterministic ids by default.

publicPath: the URL, not the folder

output.path is where files go on disk. output.publicPath is the URL prefix the browser uses to request them at runtime, for lazy chunks and for assets such as images referenced from CSS.

JavaScript
// served from the site root
publicPath: '/',

// served from a sub-path
publicPath: '/my-app/',

// served from a CDN
publicPath: 'https://cdn.example.com/assets/',

// decide at runtime (set before any dynamic import)
// __webpack_public_path__ = window.APP_ASSET_URL;

Get this wrong and the page loads, but lazy chunks and images 404. The default 'auto' works out the URL from the current script tag, which is fine for many setups. Use an explicit value when assets live on another host or when the app is hosted in a sub-folder.

Node.js Stand-in: how publicPath builds a request URL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

chunkFilename and assetModuleFilename

filename names entry files. Other files have their own templates:

  • chunkFilename: lazy-loaded chunks and split chunks (Chapter 5).
  • assetModuleFilename: images, fonts and other files copied by asset modules.
  • cssFilename and cssChunkFilename: used by the experimental built-in CSS support. Most projects use the MiniCssExtractPlugin with its own filename.
JavaScript
output: {
  filename: 'js/[name].[contenthash:8].js',
  chunkFilename: 'js/[id].[contenthash:8].js',
  assetModuleFilename: 'media/[name].[contenthash:8][ext]',
},

A folder in the template (js/) is created automatically.

Cleaning, library output and other options

  • clean: true removes old files from output.path before emitting. Use clean: { keep: /\.git/ } to preserve some.
  • charset, crossOriginLoading (for lazy chunks on another origin) and scriptType: 'module' tune the loader code.
  • library turns your entry into an importable library:
JavaScript
output: {
  library: { name: 'MyLib', type: 'umd' },
  globalObject: 'this',
},

type can be umd, commonjs2, module (ES module output, needs experiments.outputModule), var and others. A library build also usually lists dependencies in externals so they are not bundled into your package.

The shape of a dist folder

flowchart TB
  D["dist/"] --> J["js/main.4be2f1a0.js"]
  D --> C["js/dashboard.9c07d3be.js (lazy chunk)"]
  D --> M["media/logo.12ab34cd.png"]
  D --> I["index.html (names the files above)"]

Only index.html has a stable name; every other file's name carries its content hash.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between output.path and output.publicPath?

    path controls where files are written; publicPath controls the URLs Webpack-generated code requests.
  2. 2. Why use [contenthash] in production file names?

    Stable names for unchanged content allow long-lived caching while updates still get new URLs.
  3. 3. A lazy chunk returns 404 but the main bundle loads. What should you check first?

    Chunks are requested using publicPath, so a wrong prefix breaks only dynamically loaded files.
  4. 4. Which option names lazy-loaded chunks?

    filename is for entry bundles; chunkFilename is for additional chunks.

Summary

  • output.path is an absolute disk folder; publicPath is the browser-side URL prefix.
  • filename, chunkFilename and assetModuleFilename are templates with [name], [contenthash] and friends.
  • Content hashes allow immutable caching without stale updates.
  • clean: true keeps dist tidy; output.library builds importable libraries.
  • A wrong publicPath breaks lazy chunks and assets, not the main bundle.

Next: Loaders Basics