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
Copy file name to clipboardExpand all lines: src/content/guides/asset-management.md
+24-24Lines changed: 24 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ If you've been following the guides from the start, you will now have a small pr
17
17
18
18
Prior to webpack, front-end developers would use tools like [grunt](https://gruntjs.com/) and [gulp](https://gulpjs.com/) to process these assets and move them from their `/src` folder into their `/dist` or `/build` directory. The same idea was used for JavaScript modules, but tools like webpack will __dynamically bundle__ all dependencies (creating what's known as a [dependency graph](/concepts/dependency-graph)). This is great because every module now _explicitly states its dependencies_ and we'll avoid bundling modules that aren't in use.
19
19
20
-
One of the coolest webpack features is that you can also _include any other type of file_, besides JavaScript, for which there is a loader. This means that the same benefits listed above for JavaScript (e.g. explicit dependencies) can be applied to everything used in building a website or web app. Let's start with CSS, as you may already be familiar with that setup.
20
+
One of the coolest webpack features is that you can also _include any other type of file_, besides JavaScript, for which there is a loader or built-in [Asset Modules](/guides/asset-modules/) support. This means that the same benefits listed above for JavaScript (e.g. explicit dependencies) can be applied to everything used in building a website or web app. Let's start with CSS, as you may already be familiar with that setup.
21
21
22
22
## Setup
23
23
@@ -145,18 +145,18 @@ $ npm run build
145
145
146
146
...
147
147
[webpack-cli] Compilation finished
148
-
asset bundle.js 72.6 KiB [compared for emit] [minimized] (name: main) 1 related asset
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`.
@@ -166,7 +166,7 @@ Note that you can, and in most cases should, [minimize css](/plugins/mini-css-ex
166
166
167
167
## Loading Images
168
168
169
-
So now we're pulling in our CSS, but what about our images like backgrounds and icons? As of webpack 5, using the [Asset Modules](/guides/asset-modules/) we can easily incorporate those in our system as well:
169
+
So now we're pulling in our CSS, but what about our images like backgrounds and icons? As of webpack 5, using the built-in [Asset Modules](/guides/asset-modules/) we can easily incorporate those in our system as well:
170
170
171
171
__webpack.config.js__
172
172
@@ -249,32 +249,32 @@ __src/style.css__
249
249
}
250
250
```
251
251
252
-
Let's create a new build and open up the index.html file again:
252
+
Let's create a new build and open up the `index.html` file again:
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 `3b7bf087cbac835e6f7d.png`. This means webpack found our file in the `src` folder and processed it!
277
+
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:
@@ -134,8 +134,8 @@ It quickly becomes a hassle to manually run `npm run build` every time you want
134
134
There are a couple of different options available in webpack that help you automatically compile your code whenever it changes:
Copy file name to clipboardExpand all lines: src/content/guides/getting-started.md
+11-14Lines changed: 11 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -27,11 +27,11 @@ contributors:
27
27
28
28
webpack is used to 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.
29
29
30
-
W> The minimum Node.js version to run webpack 5 is 10.13.0 (LTS)
30
+
W> The minimum supported Node.js version to run webpack 5 is 10.13.0 (LTS)
31
31
32
32
## Basic Setup
33
33
34
-
First let's create a directory, initialize npm, [install webpack locally](/guides/installation/#local-installation), and install the webpack-cli (the tool used to run webpack on the command line):
34
+
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):
35
35
36
36
```bash
37
37
mkdir webpack-demo
@@ -129,7 +129,7 @@ Let's use webpack to manage these scripts instead.
129
129
130
130
## Creating a Bundle
131
131
132
-
First we'll tweak our directory structure slightly, separating the "source" code (`/src`) from our "distribution" code (`/dist`). The "source" code is the code that we'll write and edit. The "distribution" code is the minimized and optimized `output` of our build process that will eventually be loaded in the browser. Tweak the directory structure as follows:
132
+
First we'll tweak our directory structure slightly, separating the "source" code (`./src`) from our "distribution" code (`./dist`). The "source" code is the code that we'll write and edit. The "distribution" code is the minimized and optimized `output` of our build process that will eventually be loaded in the browser. Tweak the directory structure as follows:
133
133
134
134
__project__
135
135
@@ -151,7 +151,7 @@ npm install --save lodash
151
151
152
152
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).
153
153
154
-
Now, lets import `lodash` in our script:
154
+
Now, let's import `lodash` in our script:
155
155
156
156
__src/index.js__
157
157
@@ -171,7 +171,7 @@ __src/index.js__
171
171
document.body.appendChild(component());
172
172
```
173
173
174
-
Now, since we'll be bundling our scripts, we have to update our `index.html` file. Let's remove the lodash `<script>`, as we now `import` it, and modify the other `<script>` tag to load the bundle, instead of the raw `/src` file:
174
+
Now, since we'll be bundling our scripts, we have to update our `index.html` file. Let's remove the lodash `<script>`, as we now `import` it, and modify the other `<script>` tag to load the bundle, instead of the raw `./src` file:
T> Your output may vary a bit, but if the build is successful then you are good to go. Also, don't worry about the warning, we'll tackle that later.
208
+
T> Your output may vary a bit, but if the build is successful then you are good to go.
209
209
210
210
Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: `'Hello webpack'`.
211
211
212
-
W> If you are getting a syntax error in the middle of minified JavaScript when opening `index.html` in the browser, set [`development mode`](/configuration/mode/#mode-development) and run `npx webpack` again. This is related to running `npx webpack` on latest Node.js (v12.5+) instead of [LTS version](https://nodejs.org/en/).
213
-
214
-
215
212
## Modules
216
213
217
214
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.
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.
@@ -276,7 +273,7 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we
276
273
__package.json__
277
274
278
275
```diff
279
-
{
276
+
{
280
277
"name": "webpack-demo",
281
278
"version": "1.0.0",
282
279
"description": "",
@@ -299,7 +296,7 @@ __package.json__
299
296
}
300
297
```
301
298
302
-
Now the `npm run build` command can be used in place of the `npx` command we used earlier. Note that within `scripts` we can reference locally installed npm packages by name the same way we did with `npx`. This convention is the standard in most npm-based projects because it allows all contributors to use the same set of common scripts (each with flags like `--config` if necessary).
299
+
Now the `npm run build` command can be used in place of the `npx` command we used earlier. Note that within `scripts` we can reference locally installed npm packages by name the same way we did with `npx`. This convention is the standard in most npm-based projects because it allows all contributors to use the same set of common scripts.
303
300
304
301
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`.
0 commit comments