Skip to content

Commit 650f69e

Browse files
Updated getting-started.md
* Changes the guide so it reflects webpack 4 output. * Changes some wordings here and there and adds some clarifications. * Adds `webpack-cli` install which is required with v4
1 parent 2c75064 commit 650f69e

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

src/content/guides/getting-started.md

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ 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 (which we'll need to run webpack from the command line (CLI)):
2424

2525
``` bash
2626
mkdir webpack-demo && cd webpack-demo
2727
npm init -y
2828
npm install --save-dev webpack
29+
npm install --save-dev webpack-cli
2930
```
3031

31-
Now we'll create the following directory structure and contents:
32+
T> Throughout the Guides we will use `diff` blocks to show you what changes we're making to directories, files, code, etc. For example: below we're adding `index.html` (file), `/src` (directory), and `index.js` (file).
33+
34+
Now we'll create the following directory structure, files and their contents:
3235

3336
__project__
3437

@@ -69,7 +72,7 @@ __index.html__
6972
</html>
7073
```
7174

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.
75+
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.
7376

7477
There are problems with managing JavaScript projects this way:
7578

@@ -99,7 +102,7 @@ __project__
99102
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally...
100103

101104
``` bash
102-
npm install --save lodash
105+
npm install --save-dev lodash
103106
```
104107

105108
and then import it in our script...
@@ -146,18 +149,23 @@ With that said, let's run `npx webpack` with our script as the [entry point](/co
146149
``` bash
147150
npx webpack src/index.js --output dist/bundle.js
148151

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]
152+
Hash: dabab1bac2b940c1462b
153+
Version: webpack 4.0.1
154+
Time: 3003ms
155+
Built at: 2018-2-26 22:42:11
156+
Asset Size Chunks Chunk Names
157+
bundle.js 69.6 KiB 0 [emitted] main
158+
Entrypoint main = bundle.js
159+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
155160
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
156-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
161+
[3] ./src/index.js 256 bytes {0} [built]
157162
+ 1 hidden module
163+
164+
WARNING in configuration
165+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
158166
```
159167

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

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

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

174182
## Using a Configuration
175183

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:
184+
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 on the CLI, so let's create one to replace the CLI line options used above:
177185

178186
__project__
179187

@@ -201,20 +209,25 @@ module.exports = {
201209
};
202210
```
203211

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

206214
``` bash
207215
npx webpack --config webpack.config.js
208216

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]
217+
Hash: dabab1bac2b940c1462b
218+
Version: webpack 4.0.1
219+
Time: 328ms
220+
Built at: 2018-2-26 22:47:43
221+
Asset Size Chunks Chunk Names
222+
bundle.js 69.6 KiB 0 [emitted] main
223+
Entrypoint main = bundle.js
224+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
215225
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
216-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
226+
[3] ./src/index.js 256 bytes {0} [built]
217227
+ 1 hidden module
228+
229+
WARNING in configuration
230+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
218231
```
219232

220233
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 +243,28 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we
230243

231244
__package.json__
232245

233-
``` json
246+
``` diff
234247
{
235-
...
248+
"name": "webpack-demo",
249+
"version": "1.0.0",
250+
"description": "",
251+
"main": "index.js",
236252
"scripts": {
253+
"test": "echo \"Error: no test specified\" && exit 1",
237254
"build": "webpack"
238255
},
239-
...
256+
"keywords": [],
257+
"author": "",
258+
"license": "ISC",
259+
"devDependencies": {
260+
"webpack": "^4.0.1",
261+
"webpack-cli": "^2.0.9"
262+
},
263+
"dependencies": {
264+
"lodash": "^4.17.5"
265+
}
240266
}
267+
241268
```
242269

243270
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 +274,20 @@ Now run the following command and see if your script alias works:
247274
``` bash
248275
npm run build
249276

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]
277+
Hash: dabab1bac2b940c1462b
278+
Version: webpack 4.0.1
279+
Time: 323ms
280+
Built at: 2018-2-26 22:50:25
281+
Asset Size Chunks Chunk Names
282+
bundle.js 69.6 KiB 0 [emitted] main
283+
Entrypoint main = bundle.js
284+
[1] (webpack)/buildin/module.js 519 bytes {0} [built]
256285
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
257-
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
286+
[3] ./src/index.js 256 bytes {0} [built]
258287
+ 1 hidden module
288+
289+
WARNING in configuration
290+
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
259291
```
260292

261293
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`.
@@ -279,6 +311,6 @@ webpack-demo
279311
|- /node_modules
280312
```
281313

282-
T> If you're using npm 5, you'll probably also see a `package-lock.json` file in your directory.
314+
T> If you're using npm 5, you'll probably also see a `package-lock.json` file in your directory as well.
283315

284316
If you want to learn more about webpack's design, you can check out the [basic concepts](/concepts) and [configuration](/configuration) pages. Furthermore, the [API](/api) section digs into the various interfaces webpack offers.

0 commit comments

Comments
 (0)