You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`.
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!
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:
171
171
"author": "",
172
172
"license": "ISC",
173
173
"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"
177
177
},
178
178
"dependencies": {
179
-
"lodash": "^4.17.20"
179
+
"lodash": "^4.17.21"
180
180
}
181
181
}
182
182
```
@@ -273,13 +273,13 @@ Let's add a script to easily run the dev server as well:
273
273
"author": "",
274
274
"license": "ISC",
275
275
"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"
280
280
},
281
281
"dependencies": {
282
-
"lodash": "^4.17.20"
282
+
"lodash": "^4.17.21"
283
283
}
284
284
}
285
285
```
@@ -399,15 +399,15 @@ Now add an npm script to make it a little easier to run the server:
399
399
"author": "",
400
400
"license": "ISC",
401
401
"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"
408
408
},
409
409
"dependencies": {
410
-
"lodash": "^4.17.20"
410
+
"lodash": "^4.17.21"
411
411
}
412
412
}
413
413
```
@@ -425,12 +425,12 @@ Example app listening on port 3000!
Copy file name to clipboardExpand all lines: src/content/guides/getting-started.mdx
+35-19Lines changed: 35 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,15 +30,17 @@ contributors:
30
30
- zowiebeha
31
31
---
32
32
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
+
33
35
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.
34
36
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.
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`.
42
44
43
45
```bash
44
46
npx create-webpack-app webpack-demo
@@ -54,6 +56,8 @@ T> This command generates a ready-to-use webpack project with a sensible default
54
56
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):
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`.
126
132
127
133
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).
128
134
@@ -143,13 +149,13 @@ T> If you want to learn more about the inner workings of `package.json`, then we
143
149
"author": "",
144
150
"license": "MIT",
145
151
"devDependencies": {
146
-
"webpack": "^5.38.1",
147
-
"webpack-cli": "^6.0.0"
152
+
"webpack": "^5.105.0",
153
+
"webpack-cli": "^7.0.0"
148
154
}
149
155
}
150
156
```
151
157
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.
153
159
154
160
There are problems with managing JavaScript projects this way:
155
161
@@ -176,13 +182,15 @@ First we'll tweak our directory structure slightly, separating the "source" code
176
182
└── index.js
177
183
```
178
184
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.
180
186
181
187
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally:
182
188
183
189
```bash
190
+
# Run the command for one package manager only.
191
+
184
192
# npm
185
-
npm install --save lodash
193
+
npm install lodash
186
194
187
195
# yarn
188
196
yarn add lodash
@@ -191,7 +199,7 @@ yarn add lodash
191
199
pnpm add lodash
192
200
```
193
201
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).
195
203
196
204
Now, let's import `lodash` in our script:
197
205
@@ -223,7 +231,7 @@ Now, since we'll be bundling our scripts, we have to update our `index.html` fil
@@ -239,6 +247,8 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds
239
247
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).
T> Your output may vary a bit, but if the build is successful then you are good to go.
261
271
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
+
262
274
Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: `'Hello webpack'`.
263
275
264
276
## Modules
265
277
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.
267
279
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.
269
281
270
282
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/).
271
283
@@ -312,6 +324,8 @@ export default {
312
324
Now, let's run the build again but instead using our new configuration file:
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
355
369
"author":"",
356
370
"license":"ISC",
357
371
"devDependencies": {
358
-
"webpack":"^5.4.0",
359
-
"webpack-cli":"^6.0.0"
372
+
"webpack":"^5.105.0",
373
+
"webpack-cli":"^7.0.0"
360
374
},
361
375
"dependencies": {
362
-
"lodash":"^4.17.20"
376
+
"lodash":"^4.17.21"
363
377
}
364
378
}
365
379
```
@@ -369,6 +383,8 @@ Now the `npm run build` command can be used in place of the `npx` command we use
369
383
Now run the following command and see if your script alias works:
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`.
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.
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