Skip to content

Commit 6de8690

Browse files
docs(Guides): Update Getting Started to use webpack 4
Updates the Get Started guide to use webpack 4 and adds some additional information about npm.
1 parent b1d2779 commit 6de8690

File tree

1 file changed

+97
-40
lines changed

1 file changed

+97
-40
lines changed

src/content/guides/getting-started.md

Lines changed: 97 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ Webpack is used to compile JavaScript modules. Once [installed](/guides/installa
2020

2121
## Basic Setup
2222

23-
First let's create a directory, initialize npm, and [install webpack locally](/guides/installation#local-installation):
23+
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):
2424

2525
``` bash
2626
mkdir webpack-demo && cd webpack-demo
2727
npm init -y
28-
npm install --save-dev webpack
28+
npm install webpack webpack-cli --save-dev
2929
```
3030

31-
Now we'll create the following directory structure and contents:
31+
T> Throughout the Guides we will use `diff` blocks to show you what changes we're making to directories, files, and code.
32+
33+
Now we'll create the following directory structure, files and their contents:
3234

3335
__project__
3436

@@ -69,7 +71,33 @@ __index.html__
6971
</html>
7072
```
7173

72-
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 declared a need for `lodash`; it just assumes that the global variable `_` exists.
74+
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.
75+
76+
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).
77+
78+
__package.json__
79+
``` diff
80+
{
81+
"name": "webpack-demo",
82+
"version": "1.0.0",
83+
"description": "",
84+
+ "private": true,
85+
- "main": "index.js",
86+
"scripts": {
87+
"test": "echo \"Error: no test specified\" && exit 1"
88+
},
89+
"keywords": [],
90+
"author": "",
91+
"license": "ISC",
92+
"devDependencies": {
93+
"webpack": "^4.0.1",
94+
"webpack-cli": "^2.0.9"
95+
},
96+
"dependencies": {}
97+
}
98+
```
99+
100+
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 just assumes that the global variable `_` exists.
73101

74102
There are problems with managing JavaScript projects this way:
75103

@@ -96,13 +124,15 @@ __project__
96124
|- index.js
97125
```
98126

99-
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally...
127+
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally:
100128

101129
``` bash
102130
npm install --save lodash
103131
```
104132

105-
and then import it in our script...
133+
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).
134+
135+
Now, lets import `lodash` in our script:
106136

107137
__src/index.js__
108138

@@ -144,20 +174,25 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds
144174
With that said, let's run `npx webpack` with our script as the [entry point](/concepts/entry-points) and `bundle.js` as the [output](/concepts/output). The `npx` command, which ships with Node 8.2 or higher, runs the webpack binary (`./node_modules/.bin/webpack`) of the webpack package we installed in the beginning:
145175

146176
``` bash
147-
npx webpack src/index.js dist/bundle.js
148-
149-
Hash: 857f878815ce63ad5b4f
150-
Version: webpack 3.9.1
151-
Time: 332ms
152-
Asset Size Chunks Chunk Names
153-
bundle.js 544 kB 0 [emitted] [big] main
154-
[0] ./src/index.js 222 bytes {0} [built]
177+
npx webpack
178+
179+
Hash: dabab1bac2b940c1462b
180+
Version: webpack 4.0.1
181+
Time: 3003ms
182+
Built at: 2018-2-26 22:42:11
183+
Asset Size Chunks Chunk Names
184+
bundle.js 69.6 KiB 0 [emitted] main
185+
Entrypoint main = bundle.js
186+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
155187
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
156-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
188+
[3] ./src/index.js 256 bytes {0} [built]
157189
+ 1 hidden module
190+
191+
WARNING in configuration
192+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
158193
```
159194

160-
T> Your output may vary a bit, but if the build is successful then you are good to go.
195+
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.
161196

162197
Open `index.html` in your browser and, if everything went right, you should see the following text: 'Hello webpack'.
163198

@@ -173,7 +208,7 @@ Note that webpack will not alter any code other than `import` and `export` state
173208

174209
## Using a Configuration
175210

176-
Most projects will need a more complex setup, which is why webpack supports a [configuration file](/concepts/configuration). This is much more efficient than having to type in a lot of commands in the terminal, so let's create one to replace the CLI options used above:
211+
As of version 4, webpack doesn't require any configuration, but most projects will need a more complex setup, which is why webpack supports a [configuration file](/concepts/configuration). This is much more efficient than having to manually type in a lot of commands in the terminal, so let's create one to replace the CLI line options used above:
177212

178213
__project__
179214

@@ -201,20 +236,25 @@ module.exports = {
201236
};
202237
```
203238

204-
Now, let's run the build again but instead using our new configuration:
239+
Now, let's run the build again but instead using our new configuration file:
205240

206241
``` bash
207242
npx webpack --config webpack.config.js
208243

209-
Hash: 857f878815ce63ad5b4f
210-
Version: webpack 3.9.1
211-
Time: 298ms
212-
Asset Size Chunks Chunk Names
213-
bundle.js 544 kB 0 [emitted] [big] main
214-
[0] ./src/index.js 222 bytes {0} [built]
244+
Hash: dabab1bac2b940c1462b
245+
Version: webpack 4.0.1
246+
Time: 328ms
247+
Built at: 2018-2-26 22:47:43
248+
Asset Size Chunks Chunk Names
249+
bundle.js 69.6 KiB 0 [emitted] main
250+
Entrypoint main = bundle.js
251+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
215252
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
216-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
253+
[3] ./src/index.js 256 bytes {0} [built]
217254
+ 1 hidden module
255+
256+
WARNING in configuration
257+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
218258
```
219259

220260
W> Note that when calling `webpack` via its path on windows, you must use backslashes instead, e.g. `node_modules\.bin\webpack --config webpack.config.js`.
@@ -230,14 +270,26 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we
230270

231271
__package.json__
232272

233-
``` json
234-
{
235-
...
236-
"scripts": {
237-
"build": "webpack"
238-
},
239-
...
240-
}
273+
``` diff
274+
{
275+
"name": "webpack-demo",
276+
"version": "1.0.0",
277+
"description": "",
278+
"main": "index.js",
279+
"scripts": {
280+
"test": "echo \"Error: no test specified\" && exit 1",
281+
+ "build": "webpack"
282+
},
283+
"keywords": [],
284+
"author": "",
285+
"license": "ISC",
286+
"devDependencies": {
287+
"webpack": "^4.0.1",
288+
"webpack-cli": "^2.0.9",
289+
"lodash": "^4.17.5"
290+
}
291+
}
292+
241293
```
242294

243295
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).
@@ -247,15 +299,20 @@ Now run the following command and see if your script alias works:
247299
``` bash
248300
npm run build
249301

250-
Hash: 857f878815ce63ad5b4f
251-
Version: webpack 3.9.1
252-
Time: 294ms
253-
Asset Size Chunks Chunk Names
254-
bundle.js 544 kB 0 [emitted] [big] main
255-
[0] ./src/index.js 222 bytes {0} [built]
302+
Hash: dabab1bac2b940c1462b
303+
Version: webpack 4.0.1
304+
Time: 323ms
305+
Built at: 2018-2-26 22:50:25
306+
Asset Size Chunks Chunk Names
307+
bundle.js 69.6 KiB 0 [emitted] main
308+
Entrypoint main = bundle.js
309+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
256310
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
257-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
311+
[3] ./src/index.js 256 bytes {0} [built]
258312
+ 1 hidden module
313+
314+
WARNING in configuration
315+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
259316
```
260317

261318
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 -- --colors`.

0 commit comments

Comments
 (0)