Skip to content

Commit cbd073d

Browse files
docs: improve Getting Started guide clarity and consistency (#8199)
1 parent d8c9350 commit cbd073d

File tree

7 files changed

+74
-58
lines changed

7 files changed

+74
-58
lines changed

src/content/guides/asset-management.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ cacheable modules 539 KiB
166166
modules by path ./src/ 965 bytes
167167
./src/index.js + 1 modules 639 bytes [built] [code generated]
168168
./node_modules/css-loader/dist/cjs.js!./src/style.css 326 bytes [built] [code generated]
169-
webpack 5.4.0 compiled successfully in 2231 ms
169+
webpack 5.x.x compiled successfully in 2231 ms
170170
```
171171

172172
Open up `dist/index.html` in your browser again and you should see that `Hello webpack` is now styled in red. To see what webpack did, inspect the page (don't view the page source, as it won't show you the result, because the `<style>` tag is dynamically created by JavaScript) and look at the page's head tags. It should contain the style block that we imported in `index.js`.
@@ -285,7 +285,7 @@ cacheable modules 540 KiB (javascript) 9.88 KiB (asset)
285285
./src/index.js + 1 modules 794 bytes [built] [code generated]
286286
./src/icon.png 42 bytes (javascript) 9.88 KiB (asset) [built] [code generated]
287287
./node_modules/css-loader/dist/cjs.js!./src/style.css 648 bytes [built] [code generated]
288-
webpack 5.4.0 compiled successfully in 1972 ms
288+
webpack 5.x.x compiled successfully in 1972 ms
289289
```
290290

291291
If all went well, you should now see your icon as a repeating background, as well as an `img` element beside our `Hello webpack` text. If you inspect this element, you'll see that the actual filename has changed to something like `29822eaa871e8eadeaa4.png`. This means webpack found our file in the `src` folder and processed it!
@@ -396,7 +396,7 @@ cacheable modules 541 KiB (javascript) 43.1 KiB (asset)
396396
./src/icon.png 42 bytes (javascript) 9.88 KiB (asset) [built] [code generated]
397397
./src/my-font.woff2 42 bytes (javascript) 14.5 KiB (asset) [built] [code generated]
398398
./src/my-font.woff 42 bytes (javascript) 18.8 KiB (asset) [built] [code generated]
399-
webpack 5.4.0 compiled successfully in 2142 ms
399+
webpack 5.x.x compiled successfully in 2142 ms
400400
```
401401

402402
Open up `dist/index.html` again and see if our `Hello webpack` text has changed to the new font. If all is well, you should see the changes.

src/content/guides/code-splitting.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ cacheable modules 530 KiB
118118
./src/index.js 257 bytes [built] [code generated]
119119
./src/another-module.js 84 bytes [built] [code generated]
120120
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
121-
webpack 5.4.0 compiled successfully in 245 ms
121+
webpack 5.x.x compiled successfully in 245 ms
122122
```
123123

124124
As mentioned there are some pitfalls to this approach:
@@ -216,7 +216,7 @@ cacheable modules 530 KiB
216216
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
217217
./src/another-module.js 84 bytes [built] [code generated]
218218
./src/index.js 257 bytes [built] [code generated]
219-
webpack 5.4.0 compiled successfully in 249 ms
219+
webpack 5.x.x compiled successfully in 249 ms
220220
```
221221

222222
As you can see there's another `runtime.bundle.js` file generated besides `shared.bundle.js`, `index.bundle.js` and `another.bundle.js`.
@@ -271,7 +271,7 @@ cacheable modules 530 KiB
271271
./src/index.js 257 bytes [built] [code generated]
272272
./src/another-module.js 84 bytes [built] [code generated]
273273
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
274-
webpack 5.4.0 compiled successfully in 241 ms
274+
webpack 5.x.x compiled successfully in 241 ms
275275
```
276276

277277
Here are some other useful plugins and loaders provided by the community for splitting code:
@@ -373,7 +373,7 @@ runtime modules 7.37 KiB 11 modules
373373
cacheable modules 530 KiB
374374
./src/index.js 434 bytes [built] [code generated]
375375
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
376-
webpack 5.4.0 compiled successfully in 268 ms
376+
webpack 5.x.x compiled successfully in 268 ms
377377
```
378378

379379
T> You can also use magic comment `webpackExports` with `import()` to expose specific exports from a dynamically imported module:

src/content/guides/development.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ cacheable modules 530 KiB
122122
./src/index.js 406 bytes [built] [code generated]
123123
./src/print.js 83 bytes [built] [code generated]
124124
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
125-
webpack 5.4.0 compiled successfully in 706 ms
125+
webpack 5.x.x compiled successfully in 706 ms
126126
```
127127

128128
Now open the resulting `index.html` file in your browser. Click the button and look in your console where the error is displayed. The error should say something like this:
@@ -171,12 +171,12 @@ Let's add an npm script that will start webpack's Watch Mode:
171171
"author": "",
172172
"license": "ISC",
173173
"devDependencies": {
174-
"html-webpack-plugin": "^5.0.0",
175-
"webpack": "^5.4.0",
176-
"webpack-cli": "^6.0.0"
174+
"html-webpack-plugin": "^5.6.6",
175+
"webpack": "^5.105.0",
176+
"webpack-cli": "^7.0.0"
177177
},
178178
"dependencies": {
179-
"lodash": "^4.17.20"
179+
"lodash": "^4.17.21"
180180
}
181181
}
182182
```
@@ -273,13 +273,13 @@ Let's add a script to easily run the dev server as well:
273273
"author": "",
274274
"license": "ISC",
275275
"devDependencies": {
276-
"html-webpack-plugin": "^5.0.0",
277-
"webpack": "^5.4.0",
278-
"webpack-cli": "^6.0.0",
279-
"webpack-dev-server": "^5.0.0"
276+
"html-webpack-plugin": "^5.6.6",
277+
"webpack": "^5.105.0",
278+
"webpack-cli": "^7.0.0",
279+
"webpack-dev-server": "^5.2.3"
280280
},
281281
"dependencies": {
282-
"lodash": "^4.17.20"
282+
"lodash": "^4.17.21"
283283
}
284284
}
285285
```
@@ -399,15 +399,15 @@ Now add an npm script to make it a little easier to run the server:
399399
"author": "",
400400
"license": "ISC",
401401
"devDependencies": {
402-
"express": "^4.17.1",
403-
"html-webpack-plugin": "^5.0.0",
404-
"webpack": "^5.4.0",
405-
"webpack-cli": "^6.0.0",
406-
"webpack-dev-middleware": "^4.0.2",
407-
"webpack-dev-server": "^5.0.0"
402+
"express": "^5.2.1",
403+
"html-webpack-plugin": "^5.6.6",
404+
"webpack": "^5.105.0",
405+
"webpack-cli": "^7.0.0",
406+
"webpack-dev-middleware": "^8.0.3",
407+
"webpack-dev-server": "^5.2.3"
408408
},
409409
"dependencies": {
410-
"lodash": "^4.17.20"
410+
"lodash": "^4.17.21"
411411
}
412412
}
413413
```
@@ -425,12 +425,12 @@ Example app listening on port 3000!
425425
<i> ./src/index.js 406 bytes [built] [code generated]
426426
<i> ./src/print.js 83 bytes [built] [code generated]
427427
<i> ./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
428-
<i> webpack 5.4.0 compiled successfully in 709 ms
428+
<i> webpack 5.x.x compiled successfully in 709 ms
429429
<i> [webpack-dev-middleware] Compiled successfully.
430430
<i> [webpack-dev-middleware] Compiling...
431431
<i> [webpack-dev-middleware] assets by status 1.38 MiB [cached] 2 assets
432432
<i> cached modules 530 KiB (javascript) 1.9 KiB (runtime) [cached] 12 modules
433-
<i> webpack 5.4.0 compiled successfully in 19 ms
433+
<i> webpack 5.x.x compiled successfully in 19 ms
434434
<i> [webpack-dev-middleware] Compiled successfully.
435435
```
436436

src/content/guides/getting-started.mdx

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ contributors:
3030
- zowiebeha
3131
---
3232

33+
Webpack is a good fit when your application needs a customizable build pipeline: bundling JavaScript modules, processing assets, integrating loaders and plugins, and shaping output for different environments. For a very small page with one or two scripts, a bundler may be unnecessary at first; for an application with shared dependencies, npm packages, assets, and production builds, webpack gives you explicit control over how everything is assembled.
34+
3335
Webpack is used to efficiently compile JavaScript modules. Once [installed](/guides/installation), you can interact with webpack either from its [CLI](/api/cli) or [API](/api/node). If you're still new to webpack, please read through the [core concepts](/concepts) and [this comparison](/comparison) to learn why you might use it over the other tools that are out in the community.
3436

35-
W> The minimum supported Node.js version to run webpack 5 is 10.13.0 (LTS)
37+
W> The examples in this guide use `webpack-cli` 7, which requires Node.js 20.9.0 or later.
3638

3739
<StackBlitzPreview example="getting-started?terminal=" />
3840

3941
## Quick Start (Minimal Working Example)
4042

41-
If you just want to get a working webpack project up and running quickly, the easiest way is to scaffold one using `create-webpack-app`.
43+
If you want to get a working webpack project up and running quickly, the easiest way is to scaffold one using `create-webpack-app`.
4244

4345
```bash
4446
npx create-webpack-app webpack-demo
@@ -54,6 +56,8 @@ T> This command generates a ready-to-use webpack project with a sensible default
5456
First let's create a directory, initialize npm, [install webpack locally](/guides/installation/#local-installation), and install the [`webpack-cli`](https://github.com/webpack/webpack-cli) (the tool used to run webpack on the command line):
5557

5658
```bash
59+
# Run the commands for one package manager only.
60+
5761
mkdir webpack-demo
5862
cd webpack-demo
5963

@@ -114,15 +118,17 @@ document.body.appendChild(component());
114118
<head>
115119
<meta charset="utf-8" />
116120
<title>Getting Started</title>
117-
<script src="https://unpkg.com/lodash@4.17.20"></script>
121+
<script src="https://unpkg.com/lodash@4.17.21"></script>
118122
</head>
119123
<body>
120124
<script src="./src/index.js"></script>
121125
</body>
122126
</html>
123127
```
124128

125-
We also need to adjust our `package.json` file in order to make sure we mark our package as `private`, as well as removing the `main` entry. We also add `"type": "module"` so that Node.js knows to process our configuration files as ES Modules. This is to prevent an accidental publish of your code, as well as enabling ES Module support for our scripts.
129+
We also need to adjust our `package.json` file in order to make sure we mark our package as `private`, as well as removing the `main` entry. This is to prevent an accidental publish of your code.
130+
131+
We also add `"type": "module"` so that Node.js treats `.js` files in this project as ES modules. That setting applies project-wide, including future Node.js scripts and webpack configuration files. If you would rather keep Node's default CommonJS behavior, omit `"type": "module"` and write the configuration later in this guide with `require(...)` and `module.exports` instead of `import` and `export default`.
126132

127133
T> If you want to learn more about the inner workings of `package.json`, then we recommend reading the [npm documentation](https://docs.npmjs.com/files/package.json).
128134

@@ -143,13 +149,13 @@ T> If you want to learn more about the inner workings of `package.json`, then we
143149
"author": "",
144150
"license": "MIT",
145151
"devDependencies": {
146-
"webpack": "^5.38.1",
147-
"webpack-cli": "^6.0.0"
152+
"webpack": "^5.105.0",
153+
"webpack-cli": "^7.0.0"
148154
}
149155
}
150156
```
151157

152-
In this example, there are implicit dependencies between the `<script>` tags. Our `index.js` file depends on `lodash` being included in the page before it runs. This is because `index.js` never explicitly declared a need for `lodash`; it assumes that the global variable `_` exists.
158+
In this example, there are implicit dependencies between the `<script>` tags. Our `index.js` file depends on `lodash` being included in the page before it runs. This creates an implicit dependency on a global variable (`_`), making script execution order critical and harder to maintain.
153159

154160
There are problems with managing JavaScript projects this way:
155161

@@ -176,13 +182,15 @@ First we'll tweak our directory structure slightly, separating the "source" code
176182
└── index.js
177183
```
178184

179-
T> You may have noticed that `index.html` was created manually, even though it is now placed in the `dist` directory. Later on in [another guide](/guides/output-management/#setting-up-htmlwebpackplugin), we will generate `index.html` rather than edit it manually. Once this is done, it should be safe to empty the `dist` directory and to regenerate all the files within it.
185+
The `dist` directory is build output, so you usually do not hand-edit files there in a mature project. We are moving `index.html` into `dist` for now only as temporary scaffolding, so the browser has an HTML file that loads the first generated bundle. Later on in [another guide](/guides/output-management/#setting-up-htmlwebpackplugin), we will generate `index.html` rather than edit it manually. Once this is done, it should be safe to empty the `dist` directory and regenerate all the files within it.
180186

181187
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally:
182188

183189
```bash
190+
# Run the command for one package manager only.
191+
184192
# npm
185-
npm install --save lodash
193+
npm install lodash
186194

187195
# yarn
188196
yarn add lodash
@@ -191,7 +199,7 @@ yarn add lodash
191199
pnpm add lodash
192200
```
193201

194-
T> When installing a package that will be bundled into your production bundle, you should use `npm install --save`. If you're installing a package for development purposes (e.g. a linter, testing libraries, etc.) then you should use `npm install --save-dev`. More information can be found in the [npm documentation](https://docs.npmjs.com/cli/install).
202+
T> With npm 5 and later, packages installed with `npm install <package>` are saved to `dependencies` by default. If you're installing a package for development purposes (e.g. a linter, testing libraries, etc.) then you should use `npm install --save-dev`. More information can be found in the [npm documentation](https://docs.npmjs.com/cli/install).
195203

196204
Now, let's import `lodash` in our script:
197205

@@ -223,7 +231,7 @@ Now, since we'll be bundling our scripts, we have to update our `index.html` fil
223231
<head>
224232
<meta charset="utf-8" />
225233
<title>Getting Started</title>
226-
- <script src="https://unpkg.com/lodash@4.17.20"></script>
234+
- <script src="https://unpkg.com/lodash@4.17.21"></script>
227235
</head>
228236
<body>
229237
- <script src="./src/index.js"></script>
@@ -239,6 +247,8 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds
239247
With that said, let's run `npx webpack` from the project root. If webpack is installed locally, `npx` will run the local binary from `node_modules/.bin`; otherwise, it may download and execute it. This command takes our script at `src/index.js` as the [entry point](/concepts/entry-points) and generates `dist/main.js` as the [output](/concepts/output).
240248

241249
```bash
250+
# Run the command for one package manager only.
251+
242252
# npm
243253
npx webpack
244254

@@ -254,18 +264,20 @@ runtime modules 1000 bytes 5 modules
254264
cacheable modules 530 KiB
255265
./src/index.js 257 bytes [built] [code generated]
256266
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
257-
webpack 5.4.0 compiled successfully in 1851 ms
267+
webpack 5.x.x compiled successfully in 1851 ms
258268
```
259269

260270
T> Your output may vary a bit, but if the build is successful then you are good to go.
261271

272+
T> Webpack runs in production mode by default when no mode is set, so the output above includes `[minimized]` and the generated `dist/main.js` will be optimized for browsers rather than easy reading. While learning, you can run `npx webpack --mode development`, `yarn webpack --mode development`, or `pnpm exec webpack --mode development` to produce a more readable bundle.
273+
262274
Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: `'Hello webpack'`.
263275

264276
## Modules
265277

266-
The [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) statements have been standardized in [ES2015](https://babeljs.io/docs/en/learn/). They are supported in most of the browsers at this moment, however there are some browsers that don't recognize the new syntax. But don't worry, webpack does support them out of the box.
278+
The [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) statements have been standardized in [ES2015](https://babeljs.io/docs/en/learn/). They are supported in most browsers at this moment, however there are some browsers that don't recognize the new syntax. But don't worry, webpack does support them out of the box.
267279

268-
Behind the scenes, webpack actually "**transpiles**" the code so that older browsers can also run it. If you inspect `dist/main.js`, you might be able to see how webpack does this, it's quite ingenious! Besides `import` and `export`, webpack supports various other module syntaxes as well, see [Module API](/api/module-methods) for more information.
280+
Behind the scenes, webpack analyzes your module graph and bundles the modules into code that the browser can load in the right order. It handles module syntax such as `import` and `export`, and supports various other module syntaxes as well. See [Module API](/api/module-methods) for more information.
269281

270282
Note that webpack will not alter any code other than `import` and `export` statements. If you are using other [ES2015 features](http://es6-features.org/), make sure to [use a transpiler](/loaders/#transpiling) such as [Babel](https://babeljs.io/) via webpack's [loader system](/concepts/loaders/).
271283

@@ -312,6 +324,8 @@ export default {
312324
Now, let's run the build again but instead using our new configuration file:
313325
314326
```bash
327+
# Run the command for one package manager only.
328+
315329
# npm
316330
npx webpack --config webpack.config.js
317331

@@ -327,7 +341,7 @@ runtime modules 1000 bytes 5 modules
327341
cacheable modules 530 KiB
328342
./src/index.js 257 bytes [built] [code generated]
329343
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
330-
webpack 5.4.0 compiled successfully in 1934 ms
344+
webpack 5.x.x compiled successfully in 1934 ms
331345
```
332346
333347
T> If a `webpack.config.js` is present, the `webpack` command picks it up by default. We use the `--config` option here only to show that you can pass a configuration of any name. This will be useful for more complex configurations that need to be split into multiple files.
@@ -355,11 +369,11 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we
355369
"author": "",
356370
"license": "ISC",
357371
"devDependencies": {
358-
"webpack": "^5.4.0",
359-
"webpack-cli": "^6.0.0"
372+
"webpack": "^5.105.0",
373+
"webpack-cli": "^7.0.0"
360374
},
361375
"dependencies": {
362-
"lodash": "^4.17.20"
376+
"lodash": "^4.17.21"
363377
}
364378
}
365379
```
@@ -369,6 +383,8 @@ Now the `npm run build` command can be used in place of the `npx` command we use
369383
Now run the following command and see if your script alias works:
370384
371385
```bash
386+
# Run the command for one package manager only.
387+
372388
# npm
373389
npm run build
374390

@@ -385,7 +401,7 @@ runtime modules 1000 bytes 5 modules
385401
cacheable modules 530 KiB
386402
./src/index.js 257 bytes [built] [code generated]
387403
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
388-
webpack 5.4.0 compiled successfully in 1940 ms
404+
webpack 5.x.x compiled successfully in 1940 ms
389405
```
390406
391407
T> Custom parameters can be passed to webpack by adding two dashes between the `npm run build` command and your parameters, e.g. `npm run build -- --color`.

src/content/guides/output-management.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ cacheable modules 530 KiB
127127
./src/index.js 406 bytes [built] [code generated]
128128
./src/print.js 83 bytes [built] [code generated]
129129
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
130-
webpack 5.4.0 compiled successfully in 1996 ms
130+
webpack 5.x.x compiled successfully in 1996 ms
131131
```
132132

133133
We can see that webpack generates our `print.bundle.js` and `index.bundle.js` files, which we also specified in our `index.html` file. if you open `index.html` in your browser, you can see what happens when you click the button.
@@ -187,7 +187,7 @@ cacheable modules 530 KiB
187187
./src/index.js 406 bytes [built] [code generated]
188188
./src/print.js 83 bytes [built] [code generated]
189189
./node_modules/lodash/lodash.js 530 KiB [built] [code generated]
190-
webpack 5.4.0 compiled successfully in 2189 ms
190+
webpack 5.x.x compiled successfully in 2189 ms
191191
```
192192

193193
If you open `index.html` in your code editor, you'll see that the `HtmlWebpackPlugin` has created an entirely new file for you and that all the bundles are automatically added.

0 commit comments

Comments
 (0)