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: content/concepts/configuration.md
+20-3Lines changed: 20 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,25 @@
1
1
---
2
2
title: Configuration
3
+
contributors:
3
4
---
4
5
5
-
You may have noticed that few webpack configurations look exactly alike. This is because **webpack's configuration file is a JavaScript file that exports an object.** This object, is then parsed by webpack based upon its defined properties.
6
+
You may have noticed that few webpack configurations look exactly alike. This is because **webpack's configuration file is a JavaScript file that exports an object.** This object is then processed by webpack based upon its defined properties.
7
+
8
+
Because it's a standard node.js CommonJs module, you **can do the following**:
9
+
10
+
* import other files via `require(...)`
11
+
* use utilities on npm via `require(...)`
12
+
* use JS controlflow expressions i. e. the `?:` operator
13
+
* use constants or variables for often used values
14
+
* write and execute function to generate a part of the configuration
15
+
16
+
Use these features when appropriate.
17
+
18
+
**You should NOT use the following things**. Technically you could use them, but it's **not recommended**:
19
+
20
+
* Access CLI arguments, when using the webpack CLI (instead write your own CLI, or use `--env`)
21
+
* Export non-deterministic values (calling webpack twice should result in the same output files)
22
+
* Write very long configurations (instead split the configuration into multiple files)
6
23
7
24
The following examples below describe how webpack's configuration object can be both expressive and configurable because _it is code_:
Copy file name to clipboardExpand all lines: content/concepts/entry-points.md
+7-4Lines changed: 7 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
---
2
2
title: Entry Points
3
3
sort: 0
4
+
contributors:
5
+
- TheLarkInn
4
6
---
5
7
6
8
Like we mentioned in the [introduction](./), there are multiple ways to define the `entry` property in your webpack configuration. We will show you the ways you **can** configure the `entry` property, in addition to explaining why it may be useful to you.
@@ -73,6 +75,8 @@ const config = {
73
75
74
76
**Why?** This setup allows you to leverage `CommonsChunkPlugin` and extract any vendor references from your app bundle into your vendor bundle, replacing them with `__webpack_require__()` calls. If there is no vendor code in your application bundle, then you can achieve a common pattern in webpack known as [long-term vendor-caching](/how-to/cache).
75
77
78
+
?> Consider removing this scenario in favor of the DllPlugin, which provides a better vendor-splitting.
79
+
76
80
#### Multi Page Application
77
81
78
82
**webpack.config.js**
@@ -82,16 +86,15 @@ const config = {
82
86
entry: {
83
87
pageOne: './src/pageOne/index.js',
84
88
pageTwo: './src/pageTwo/index.js',
85
-
pageThree: './src/pageThree/index.js',
86
-
vendors: './src/vendors.js'
89
+
pageThree: './src/pageThree/index.js'
87
90
}
88
91
};
89
92
```
90
93
91
-
**What does this do?** We are telling webpack that we would like 4 separate dependency graphs (like the above example).
94
+
**What does this do?** We are telling webpack that we would like 3 separate dependency graphs (like the above example).
92
95
93
96
**Why?** In a multi-page application, the server is going to fetch a new HTML document for you. The page reloads this new document and assets are redownloaded. However, this gives us the unique opportunity to do multiple things:
94
97
95
98
- Use `CommonsChunkPlugin` to create bundles of shared application code between each page. Multi-page applications that reuse a lot of code/modules between entry points can greatly benefit from these techniques, as the amount of entry points increase.
96
99
97
-
- Set up [long-term vendor-caching](/how-to/cache) with the same plugin and techniques seen in the first example.
100
+
T> As a rule of thumb: for one HTML use exactly one entry point.
Copy file name to clipboardExpand all lines: content/concepts/index.md
+16-18Lines changed: 16 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -20,16 +20,14 @@ The simplest example is seen below:
20
20
**webpack.config.js**
21
21
22
22
```javascript
23
-
const config = {
23
+
module.exports = {
24
24
entry: './path/to/my/entry/file.js'
25
25
};
26
-
27
-
module.exports = config;
28
26
```
29
27
30
28
There are multiple ways to declare your `entry` property that are specific to your application's needs.
31
29
32
-
[**Learn more!**](/concepts/entry-points)
30
+
[Learn more!](/concepts/entry-points)
33
31
34
32
## Output
35
33
@@ -38,11 +36,11 @@ Once you've bundled all of your assets together, we still need to tell webpack *
38
36
**webpack.config.js**
39
37
40
38
```javascript
41
-
const config = {
39
+
module.exports = {
42
40
entry: './path/to/my/entry/file.js',
43
41
output: {
44
-
filename: 'my-first-webpack.bundle.js',
45
-
path: './dist'
42
+
path: path.resolve(__dirname, 'dist'),
43
+
filename: 'my-first-webpack.bundle.js'
46
44
}
47
45
};
48
46
```
@@ -53,7 +51,7 @@ T> You may see the term **emitted** or **emit** used throughout our documentatio
53
51
54
52
The `output` property has [many more configurable features](/configuration), but let's spend some time understanding some of the most common use cases for the `output` property.
55
53
56
-
[**Learn more!**](/concepts/output)
54
+
[Learn more!](/concepts/output)
57
55
58
56
59
57
## Loaders
@@ -65,34 +63,34 @@ The goal is to have all of the assets in your project to be **webpack's** concer
65
63
At a high level, they have two purposes in your webpack config.
66
64
67
65
1. Identify what files should be transformed by a certain loader. (`test` property)
68
-
2. Transform that file so that it can be added to your dependency graph (and eventually your bundle). (`loader` property)
66
+
2. Transform that file so that it can be added to your dependency graph (and eventually your bundle). (`use` property)
69
67
70
68
**webpack.config.js**
71
69
72
70
```javascript
73
71
const config = {
74
72
entry: './path/to/my/entry/file.js',
75
73
output: {
76
-
filename: 'my-first-webpack.bundle.js',
77
-
path: './dist'
74
+
path: path.resolve(__dirname, 'dist'),
75
+
filename: 'my-first-webpack.bundle.js'
78
76
},
79
77
module: {
80
-
loaders: [
81
-
{test: /\.(js|jsx)$/, loader: 'babel-loader'}
78
+
rules: [
79
+
{test: /\.(js|jsx)$/, use: 'babel-loader'}
82
80
]
83
81
}
84
82
};
85
83
```
86
84
87
-
In the configuration above we have defined our loader with it's two required properties: `test`, and `loader`. This tells webpack's compiler the following:
85
+
In the configuration above we have defined a rules which used our loader with it's two required properties: `test`, and `use`. This tells webpack's compiler the following:
88
86
89
-
> "Hey webpack compiler, when you come across a path that resolves to a '.js' or '.jsx' file inside of a `require()`/`import` statement, use the `babel-loader` to transform it before you add it to the bundle".
87
+
> "Hey webpack compiler, when you come across a path that resolves to a '.js' or '.jsx' file inside of a `require()`/`import` statement, **use** the `babel-loader` to transform it before you add it to the bundle".
90
88
91
-
W> It is important to remember when defining loaders in your webpack config, you are defining them under `module.loaders`, and not `loaders`.
89
+
W> It is important to remember when defining rules in your webpack config, you are defining them under `module.rules`, and not `rules`. But webpack will yell at you when doing this incorrectly.
92
90
93
91
There are more specific properties to define on loaders that we haven't yet covered.
94
92
95
-
[**Learn more!**](/concepts/loaders)
93
+
[Learn more!](/concepts/loaders)
96
94
97
95
## Plugins
98
96
@@ -130,4 +128,4 @@ There are many plugins that webpack provides out of the box! Check out our [list
130
128
131
129
Using plugins in your webpack config is straight-forward, however there are many use-cases that are worth discussing further.
Copy file name to clipboardExpand all lines: content/concepts/output.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
---
2
2
title: Output
3
3
sort: 1
4
+
contributors:
4
5
---
5
6
6
7
Options affecting the output of the compilation. `output` options tell Webpack how to write the compiled files to disk. Note, that while there can be multiple `entry` points, only one `output` configuration is specified.
Copy file name to clipboardExpand all lines: content/concepts/plugins.md
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ contributors:
6
6
- jhnns
7
7
---
8
8
9
-
**Plugins** are the [backbone](https://github.com/webpack/tapable) of webpack. webpack itself is built on the **same plugin system** that you use in your webpack configuration!!
9
+
**Plugins** are the [backbone](https://github.com/webpack/tapable) of webpack. webpack itself is built on the **same plugin system** that you use in your webpack configuration!
10
10
11
11
They also serve the purpose of doing **anything else** that a [loader](/concepts/loaders) cannot do.
T> As clever JS developer you may remember the `Function.prototype.apply` method. Because of this method you can pass any function as plugin (`this` will point to the `compiler`). You can use this style to inline custom plugins in your configuration.
35
+
34
36
## Usage
35
37
36
38
Since **plugins** can take arguments/options, you must pass a `new` instance to the `plugins` property in your webpack configuration.
@@ -70,6 +72,8 @@ module.exports = config;
70
72
71
73
### Node API
72
74
75
+
?> Even when using the Node API, users should pass plugins via the `plugins` property in the configuration. Using `compiler.apply` should not be the recommended way.
0 commit comments