liveBook · Manning

Webpack

As explained in Essential TypeScript 5, Third Edition

webpack is a JavaScript module bundler that transforms and packages modules and their dependencies into deployable bundles.

Webpack is a JavaScript module bundler that follows an application's dependencies from an entry file and packages the required assets into one or more bundles. A single request can deliver the JavaScript needed by the browser, while CSS and other content can also be included. Bundling can minify and compress content, and large applications can split optional code into separately loaded bundles. section 16.3

A practical TypeScript setup uses webpack for bundling and ts-loader for processing files with the .ts extension. The configuration names src/index.ts as the entry, produces bundle.js, resolves TypeScript and JavaScript files, and excludes node_modules from the loader rule. section 16.3

Minimal TypeScript configuration

Create webpack.config.js in the webapp folder:\n\nmodule.exports = {\n mode: "development",\n devtool: "inline-source-map",\n entry: "./src/index.ts",\n output: { filename: "bundle.js" },\n resolve: { extensions: [".ts", ".js"] },\n module: {\n rules: [\n { test: /\\.ts/, use: "ts-loader", exclude: /node_modules/ }\n ]\n }\n};\n\nInstall webpack, webpack-cli, and ts-loader as development dependencies, then run npx webpack in the webapp folder to create the bundle.

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/ }
       ]
   }
};

Inspect the complete webpack.config.js listing for the entry, output, resolution, source-map, and ts-loader settings. section 16.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains dependency traversal, loaders, configuration, bundling, compression, and code splitting, but defers the full configuration range.
Examples
Uses a webapp with src/index.ts, webpack.config.js, bundle.js, ts-loader, npx webpack, and a Node bundle-execution command.
What it assumes
Assumes a project containing TypeScript files and teaches the package installation, configuration, and command needed to bundle them.
What you can do after
Operate/configure webpack to bundle a TypeScript entry point and produce a named JavaScript output file.
How it connects
Connects bundling with TypeScript, loaders, JSX, CSS, minification, compression, code splitting, React, and Angular.

Also explained in

Compare how 2 books explain Webpack →

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.