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
* 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
Copy file name to clipboardExpand all lines: src/content/guides/getting-started.md
+64-32Lines changed: 64 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -20,15 +20,18 @@ Webpack is used to compile JavaScript modules. Once [installed](/guides/installa
20
20
21
21
## Basic Setup
22
22
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)):
24
24
25
25
```bash
26
26
mkdir webpack-demo &&cd webpack-demo
27
27
npm init -y
28
28
npm install --save-dev webpack
29
+
npm install --save-dev webpack-cli
29
30
```
30
31
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:
32
35
33
36
__project__
34
37
@@ -69,7 +72,7 @@ __index.html__
69
72
</html>
70
73
```
71
74
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.
73
76
74
77
There are problems with managing JavaScript projects this way:
75
78
@@ -99,7 +102,7 @@ __project__
99
102
To bundle the `lodash` dependency with `index.js`, we'll need to install the library locally...
100
103
101
104
```bash
102
-
npm install --save lodash
105
+
npm install --save-dev lodash
103
106
```
104
107
105
108
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
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
158
166
```
159
167
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.
161
169
162
170
Open `index.html` in your browser and, if everything went right, you should see the following text: 'Hello webpack'.
163
171
@@ -173,7 +181,7 @@ Note that webpack will not alter any code other than `import` and `export` state
173
181
174
182
## Using a Configuration
175
183
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:
177
185
178
186
__project__
179
187
@@ -201,20 +209,25 @@ module.exports = {
201
209
};
202
210
```
203
211
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:
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
218
231
```
219
232
220
233
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
230
243
231
244
__package.json__
232
245
233
-
```json
246
+
```diff
234
247
{
235
-
...
248
+
"name": "webpack-demo",
249
+
"version": "1.0.0",
250
+
"description": "",
251
+
"main": "index.js",
236
252
"scripts": {
253
+
"test": "echo \"Error: no test specified\" && exit 1",
237
254
"build": "webpack"
238
255
},
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
+
}
240
266
}
267
+
241
268
```
242
269
243
270
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:
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
259
291
```
260
292
261
293
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
279
311
|- /node_modules
280
312
```
281
313
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.
283
315
284
316
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