Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Entry, Output and Loaders

Entry Points

Learn Webpack's entry option: string, array and object forms, named entries for multi-page apps, entry descriptors with dependOn, dynamic entries and the context folder.

The entry point tells Webpack where to start building the dependency graph. Everything reachable by imports from an entry ends up in that entry's bundle. A project can have one entry (a single-page app) or many (a site with several pages, or an app with a separate admin area).

The four shapes of entry

JavaScript
// 1. a string: one entry, named "main"
entry: './src/index.js',

// 2. an array: several files that form ONE bundle, in order
entry: ['./src/polyfills.js', './src/index.js'],

// 3. an object: several named entries, one bundle each
entry: {
  home: './src/home.js',
  admin: './src/admin.js',
},

// 4. an object with descriptors for more control
entry: {
  app: { import: './src/app.js', dependOn: 'shared' },
  shared: ['react', 'react-dom'],
},

The name of an entry becomes [name] in output.filename. With a string, the name is main, which is why the default file is main.js. With the object form and filename: '[name].js' you get home.js and admin.js.

Node.js Stand-in: entries become named bundles

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.

Array entries: one bundle from several files

An array makes a single bundle that runs the files in order. It is handy for a polyfill that must execute before everything else, or to add a development client during webpack serve. It is not a way to make several bundles; use the object form for that.

Multi-page applications

A site with real separate HTML pages (home, pricing, dashboard) usually gets one entry per page, so each page downloads only what it needs.

JavaScript
// webpack.config.js
module.exports = {
  entry: {
    home: './src/pages/home.js',
    pricing: './src/pages/pricing.js',
    dashboard: './src/pages/dashboard.js',
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

The catch: if all three pages import the same library, each bundle contains its own copy. Chapter 5 solves that with code splitting; for now, dependOn and SplitChunksPlugin are the tools to look up (Code Splitting later in this course).

flowchart LR
  H["home.js"] --> L["lodash"]
  P["pricing.js"] --> L
  D["dashboard.js"] --> L
  L --> X["copied into 3 bundles"]

Without splitting, a shared dependency is duplicated per entry.

Entry descriptors

Instead of a path, an entry can be an object with these fields:

Field Meaning
import module(s) to load at startup
filename an output name for just this entry
dependOn entries that must be loaded first and are shared
runtime a name for a separate runtime chunk (or false)
library options for building a library

dependOn is the quickest fix for the duplication above. The shared entry is bundled once and the dependants rely on it, so on each page you load shared.js first and then the page file.

JavaScript
entry: {
  app: { import: './src/app.js', dependOn: 'vendor' },
  admin: { import: './src/admin.js', dependOn: 'vendor' },
  vendor: ['react', 'react-dom'],
},
optimization: { runtimeChunk: 'single' },

When using several entries on one page, add optimization.runtimeChunk: 'single' so the Webpack runtime exists once instead of being copied into every bundle.

Dynamic entries

Because entry can be a function (or return a promise), you can discover pages from the file system.

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

module.exports = {
  entry: () => {
    const pagesDir = path.resolve(__dirname, 'src/pages');
    return Object.fromEntries(
      fs.readdirSync(pagesDir)
        .filter((f) => f.endsWith('.js'))
        .map((f) => [path.basename(f, '.js'), path.join(pagesDir, f)]),
    );
  },
};

Adding src/pages/contact.js then creates a contact bundle with no config change.

Node.js Stand-in: build entries from a file list

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.

The context option

Relative entry paths are resolved against context, which defaults to the directory where you run Webpack. Setting it makes builds independent of the working directory:

JavaScript
module.exports = {
  context: path.resolve(__dirname, 'src'),
  entry: './index.js', // now relative to src/
};

Loader rules and some plugins also use context, so set it early in a project and keep it.

Choosing an entry layout

  • Single-page app: one entry; use dynamic imports to split by route (Chapter 5).
  • Multi-page site: one entry per page, plus shared chunks.
  • Separate concerns: an extra entry for a service worker or a tracking script that must load independently.
  • Libraries and tests: also entries, with output.library settings covered in Output and Filenames.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which entry form produces several separate bundles?

    Each key of an entry object becomes a named entry and a separate bundle; an array makes ONE bundle.
  2. 2. What is the name of the entry when entry is the string './src/index.js'?

    A string (or array) entry is named main, which gives the default main.js.
  3. 3. What does dependOn do?

    dependOn lets several entries share a common entry instead of duplicating its modules.
  4. 4. Why set optimization.runtimeChunk to 'single' with several entries on one page?

    Without it each entry carries its own runtime, which can break shared module state.

Summary

  • entry can be a string, array, object, descriptor object or function.
  • Entry names become [name] in output file names; strings are named main.
  • Arrays build one bundle in order; objects build one bundle per key.
  • dependOn and runtimeChunk: 'single' avoid duplicated shared code and runtimes.
  • context anchors relative entry paths.

Next: Output and Filenames