跪拜 Guibai
← All articles
Frontend

Webpack Bootstrapping: Three Commands, Five Concepts, One Bundle

By Kevin666 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Webpack configuration trips up developers at the loader-order and package-name level; this note condenses the setup into three commands and five concepts, so a developer who needs a working build can get it running and know what each piece does.

Summary

A practical note walks through setting up a webpack build from an empty directory. Three commands get the project running: initialize with `npm init -y`, install webpack, the CLI, loaders, and plugins, then run `npx webpack`. A script entry in `package.json` swaps the direct call for `npm run build` and adds a watch mode with `npm run dev`. The project structure places source files in `src/` and configuration in `webpack.config.js` and `.babelrc`.

Five core concepts frame the configuration: entry, output, loader, plugin, and mode. Loaders translate non-JS files into modules webpack understands; plugins extend functionality at build lifecycle hooks. The bundling flow starts at the entry, follows `import` statements recursively, applies loaders, and merges everything into a single bundle. An example config shows `babel-loader` for JavaScript and `style-loader` plus `css-loader` for CSS, with the `use` array executing right to left.

The output lands in `dist/` as `bundle.js` and an auto-injected `index.html`. Development mode leaves the bundle unminified at about 24KB; production mode compresses it to a few kilobytes. The guide closes with a memory rhyme that reduces the workflow to `npm install` for dependencies and `npx webpack` for bundling, everything else being configuration.

Takeaways
— Three commands set up a webpack project: `npm init -y`, `npm install webpack webpack-cli babel-loader @babel/core @babel/preset-env css-loader style-loader html-webpack-plugin --save-dev`, and `npx webpack`.
— Loaders in the `use` array execute right to left; CSS goes through `css-loader` first, then `style-loader`.
— Webpack's five core concepts are entry, output, loader, plugin, and mode.
— Development mode leaves the bundle unminified at roughly 24KB; production mode compresses it to a few kilobytes.
— The build starts at the entry file, follows `import` statements recursively, applies loaders to non-JS files, and produces a single `bundle.js` in `dist/`.
— `HtmlWebpackPlugin` injects the script tag into the generated HTML from the provided template.
— A memory rhyme captures the workflow: `npm install` installs dependencies, `npx webpack` bundles; the rest is configuration.
Concepts & terms
entry
The file where webpack begins dependency analysis and the bundling process.
output
The configuration that defines where the bundled files are written and what they are named.
loader
A transformer that converts non-JS files, such as CSS, images, or TypeScript, into modules webpack can process.
plugin
A module that extends webpack's behavior by hooking into lifecycle events, like generating HTML or cleaning directories.
mode
A configuration that sets the build environment; development keeps code unminified for debugging, production minifies and optimizes.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗