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/getting-started.md
+97-40Lines changed: 97 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -20,15 +20,17 @@ 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 (the tool used to run webpack on the command line):
24
24
25
25
```bash
26
26
mkdir webpack-demo &&cd webpack-demo
27
27
npm init -y
28
-
npm install --save-dev webpack
28
+
npm install webpack webpack-cli --save-dev
29
29
```
30
30
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:
32
34
33
35
__project__
34
36
@@ -69,7 +71,33 @@ __index.html__
69
71
</html>
70
72
```
71
73
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.
73
101
74
102
There are problems with managing JavaScript projects this way:
75
103
@@ -96,13 +124,15 @@ __project__
96
124
|- index.js
97
125
```
98
126
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:
100
128
101
129
```bash
102
130
npm install --save lodash
103
131
```
104
132
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:
106
136
107
137
__src/index.js__
108
138
@@ -144,20 +174,25 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds
144
174
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:
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
158
193
```
159
194
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.
161
196
162
197
Open `index.html` in your browser and, if everything went right, you should see the following text: 'Hello webpack'.
163
198
@@ -173,7 +208,7 @@ Note that webpack will not alter any code other than `import` and `export` state
173
208
174
209
## Using a Configuration
175
210
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:
177
212
178
213
__project__
179
214
@@ -201,20 +236,25 @@ module.exports = {
201
236
};
202
237
```
203
238
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:
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
218
258
```
219
259
220
260
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
230
270
231
271
__package.json__
232
272
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
+
241
293
```
242
294
243
295
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:
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
259
316
```
260
317
261
318
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