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
+95-93Lines changed: 95 additions & 93 deletions
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,9 @@ contributors:
25
25
- anshumanv
26
26
---
27
27
28
-
webpack is used to compile JavaScript modules. Once [installed](/guides/installation), you can interface 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.
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> Since webpack v5.0.0-beta.1 the minimum Node.js version to run webpack is 10.13.0 (LTS)
30
+
W> The minimum Node.js version to run webpack 5 is 10.13.0 (LTS)
31
31
32
32
## Basic Setup
33
33
@@ -40,7 +40,13 @@ npm init -y
40
40
npm install webpack webpack-cli --save-dev
41
41
```
42
42
43
-
T> Throughout the Guides we will use `diff` blocks to show you what changes we're making to directories, files, and code.
43
+
Throughout the Guides we will use __`diff`__ blocks to show you what changes we're making to directories, files, and code. For instance:
44
+
45
+
```diff
46
+
+ this is a new line you shall copy into your code
47
+
- and this is a line to be removed from your code
48
+
and this is a line not to touch.
49
+
```
44
50
45
51
Now we'll create the following directory structure, files and their contents:
@@ -92,24 +98,23 @@ T> If you want to learn more about the inner workings of `package.json`, then we
92
98
__package.json__
93
99
94
100
```diff
95
-
{
96
-
"name": "webpack-demo",
97
-
"version": "1.0.0",
98
-
"description": "",
99
-
+ "private": true,
100
-
- "main": "index.js",
101
-
"scripts": {
102
-
"test": "echo \"Error: no test specified\" && exit 1"
103
-
},
104
-
"keywords": [],
105
-
"author": "",
106
-
"license": "ISC",
107
-
"devDependencies": {
108
-
"webpack": "^4.20.2",
109
-
"webpack-cli": "^3.1.2"
110
-
},
111
-
"dependencies": {}
112
-
}
101
+
{
102
+
"name": "webpack-demo",
103
+
"version": "1.0.0",
104
+
"description": "",
105
+
- "main": "index.js",
106
+
+ "private": true,
107
+
"scripts": {
108
+
"test": "echo \"Error: no test specified\" && exit 1"
109
+
},
110
+
"keywords": [],
111
+
"author": "",
112
+
"license": "ISC",
113
+
"devDependencies": {
114
+
"webpack": "^5.4.0",
115
+
"webpack-cli": "^4.2.0"
116
+
}
117
+
}
113
118
```
114
119
115
120
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.
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:
In this setup, `index.js` explicitly requires `lodash` to be present, and binds it as `_` (no global scope pollution). By stating what dependencies a module needs, webpack can use this information to build a dependency graph. It then uses the graph to generate an optimized bundle where scripts will be executed in the correct order.
189
194
190
195
With that said, let's run `npx webpack`, which will take our script at `src/index.js` as the [entry point](/concepts/entry-points), and will generate `dist/main.js` as the [output](/concepts/output). The `npx` command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (`./node_modules/.bin/webpack`) of the webpack package we installed in the beginning:
191
196
192
197
```bash
193
-
npx webpack
194
-
195
-
...
196
-
Built at: 13/06/2018 11:52:07
197
-
Asset Size Chunks Chunk Names
198
-
main.js 70.4 KiB 0 [emitted] main
199
-
...
200
-
201
-
WARNING in configuration
202
-
The 'mode' option has not been set, webpack will fallback to 'production'for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
203
-
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
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.
207
209
208
-
Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: 'Hello webpack'.
210
+
Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: `'Hello webpack'`.
209
211
210
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/).
211
213
@@ -214,7 +216,7 @@ W> If you are getting a syntax error in the middle of minified JavaScript when o
214
216
215
217
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.
216
218
217
-
Behind the scenes, webpack actually "transpiles" the code so that older browsers can also run it. If you inspect `dist/main.js`, you might be able to see how webpack does this, it's quite ingenious! Besides `import` and `export`, webpack supports various other module syntaxes as well, see [Module API](/api/module-methods) for more information.
219
+
Behind the scenes, webpack actually "__transpiles__" the code so that older browsers can also run it. If you inspect `dist/main.js`, you might be able to see how webpack does this, it's quite ingenious! Besides `import` and `export`, webpack supports various other module syntaxes as well, see [Module API](/api/module-methods) for more information.
218
220
219
221
Note that webpack will not alter any code other than `import` and `export` statements. If you are using other [ES2015 features](http://es6-features.org/), make sure to [use a transpiler](/loaders/#transpiling) such as [Babel](https://babeljs.io/) or [Bublé](https://buble.surge.sh/guide/) via webpack's [loader system](/concepts/loaders/).
220
222
@@ -252,16 +254,14 @@ module.exports = {
252
254
Now, let's run the build again but instead using our new configuration file:
253
255
254
256
```bash
255
-
npx webpack --config webpack.config.js
256
-
257
-
...
258
-
Asset Size Chunks Chunk Names
259
-
main.js 70.4 KiB 0 [emitted] main
260
-
...
261
-
262
-
WARNING in configuration
263
-
The 'mode' option has not been set, webpack will fallback to 'production'for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
264
-
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
257
+
$ npx webpack --config webpack.config.js
258
+
[webpack-cli] Compilation finished
259
+
asset main.js 69.3 KiB [compared for emit] [minimized] (name: main) 1 related asset
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,46 +276,48 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we
276
276
__package.json__
277
277
278
278
```diff
279
-
{
280
-
"name": "webpack-demo",
281
-
"version": "1.0.0",
282
-
"description": "",
283
-
"scripts": {
284
-
- "test": "echo \"Error: no test specified\" && exit 1"
285
-
+ "test": "echo \"Error: no test specified\" && exit 1",
286
-
+ "build": "webpack"
287
-
},
288
-
"keywords": [],
289
-
"author": "",
290
-
"license": "ISC",
291
-
"devDependencies": {
292
-
"webpack": "^4.20.2",
293
-
"webpack-cli": "^3.1.2"
294
-
},
295
-
"dependencies": {
296
-
"lodash": "^4.17.5"
297
-
}
298
-
}
279
+
{
280
+
"name": "webpack-demo",
281
+
"version": "1.0.0",
282
+
"description": "",
283
+
"private": true,
284
+
"scripts": {
285
+
- "test": "echo \"Error: no test specified\" && exit 1"
286
+
+ "test": "echo \"Error: no test specified\" && exit 1",
287
+
+ "build": "webpack"
288
+
},
289
+
"keywords": [],
290
+
"author": "",
291
+
"license": "ISC",
292
+
"devDependencies": {
293
+
"webpack": "^5.4.0",
294
+
"webpack-cli": "^4.2.0"
295
+
},
296
+
"dependencies": {
297
+
"lodash": "^4.17.20"
298
+
}
299
+
}
299
300
```
300
301
301
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).
302
303
303
304
Now run the following command and see if your script alias works:
304
305
305
306
```bash
306
-
npm run build
307
+
$ npm run build
307
308
308
-
...
309
-
Asset Size Chunks Chunk Names
310
-
main.js 70.4 KiB 0 [emitted] main
311
309
...
312
310
313
-
WARNING in configuration
314
-
The 'mode' option has not been set, webpack will fallback to 'production'for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
315
-
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/.
311
+
[webpack-cli] Compilation finished
312
+
asset main.js 69.3 KiB [compared for emit] [minimized] (name: main) 1 related asset
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`.
320
+
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`.
319
321
320
322
321
323
## Conclusion
@@ -336,7 +338,7 @@ webpack-demo
336
338
|- /node_modules
337
339
```
338
340
339
-
T> If you're using npm 5, you'll probably also see a `package-lock.json` file in your directory.
341
+
T> If you're using npm 5+, you'll probably also see a `package-lock.json` file in your directory.
340
342
341
343
W> Do not compile untrusted code with webpack. It could lead to execution of malicious code on your computer, remote servers, or in the Web browsers of the end users of your application.
0 commit comments