liveBook · Manning

Bundling

As explained in Essential TypeScript 5, Third Edition

A bundle is a generated file or set of files containing packaged application modules and their dependencies for delivery to a client.

A browser cannot resolve application imports by searching local files as a JavaScript runtime can. Each web file must instead be requested over HTTP. Bundling resolves dependencies during compilation and packages the files an application uses for efficient delivery. A bundle can include JavaScript and other content, such as CSS; large applications can split optional content into separate bundles loaded when needed. section 16.3

For a TypeScript web application using webpack:

  • Install webpack, webpack-cli and ts-loader as development dependencies. The loader compiles TypeScript and feeds the JavaScript into the bundle. section 16.3
  • Set an entry file, an output filename and a loader rule in webpack.config.js. Run npx webpack to create dist/bundle.js. section 16.3
  • For production, run npx webpack --mode "production". Webpack writes a minified bundle to dist, optimizing its contents for size rather than readability. section 17.4.5
Listing 16.7 The contents of the webpack.config.js file in the webapp folder
module.exports = {
   mode: "development",
   devtool: "inline-source-map",
   entry: "./src/index.ts",
   output: { filename: "bundle.js" },    
   resolve: { extensions: [".ts", ".js"] },          
   module: {
       rules: [
           { test: /\.ts/, use: "ts-loader", exclude: /node_modules/ }
       ]
   }
};

The development configuration starts at ./src/index.ts, writes bundle.js, resolves .ts and .js imports, and passes TypeScript files through ts-loader. section 16.3

Delivering the bundle during development

Place index.html in assets with <script src="bundle.js"></script>, configure Webpack Dev Server to serve assets on port 4500, and run npx webpack serve. The browser requests bundle.js when it processes the script element. During development, the generated bundle is served from memory rather than written to dist; changing a code file triggers a new bundle and an automatic browser reload. section 16.4

The compilation path depends on the toolchain. In a React setup, Babel transforms TypeScript into JavaScript while the TypeScript compiler has noEmit enabled; webpack then bundles the resulting JavaScript and resources. Do not assume that ts-loader is required in every TypeScript application. section 20.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through webpack setup, development serving and production output without detailing webpack’s internal bundling algorithm.
Examples
Uses webpack.config.js, an HTML script reference, development-server commands and a production build command.
What it assumes
Assumes familiarity with JavaScript imports and TypeScript files while supplying the webpack packages and configuration needed to begin bundling.
What you can do after
Build it from scratch with webpack and ts-loader, then serve a development bundle or create a minified production bundle.
How it connects
Connects module dependencies, TypeScript compilation, CSS, Webpack Dev Server and React tooling.

Also explained in

Compare how 8 books explain bundling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.