Skip to content

Commit ef43912

Browse files
authored
Merge branch 'master' into SplitChunksPluginTypo
2 parents 3184b71 + 9e959d2 commit ef43912

File tree

4 files changed

+208
-169
lines changed

4 files changed

+208
-169
lines changed

src/content/concepts/entry-points.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ contributors:
55
- TheLarkInn
66
- chrisVillanueva
77
- byzyk
8+
- sokra
89
---
910

1011
As mentioned in [Getting Started](/guides/getting-started/#using-a-configuration), 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.
@@ -47,7 +48,7 @@ Usage: `entry: {[entryChunkName: string]: string|Array<string>}`
4748
module.exports = {
4849
entry: {
4950
app: './src/app.js',
50-
vendors: './src/vendors.js'
51+
adminApp: './src/adminApp.js'
5152
}
5253
};
5354
```
@@ -61,26 +62,9 @@ T> **"Scalable webpack configurations"** are ones that can be reused and combine
6162

6263
Below is a list of entry configurations and their real-world use cases:
6364

64-
6565
### Separate App and Vendor Entries
6666

67-
**webpack.config.js**
68-
69-
```javascript
70-
module.exports = {
71-
entry: {
72-
app: './src/app.js',
73-
vendors: './src/vendors.js'
74-
}
75-
};
76-
```
77-
78-
**What does this do?** At face value, this tells webpack to create dependency graphs starting at both `app.js` and `vendors.js`. These graphs are completely separate and independent of each other (there will be a webpack bootstrap in each bundle). This is commonly seen with single page applications which have only one entry point (excluding vendors).
79-
80-
**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](/guides/caching).
81-
82-
?> Consider removing this scenario in favor of the DllPlugin, which provides a better vendor-splitting.
83-
67+
T> In webpack version < 4 it was common to add vendors as separate entrypoint to compile it as separate file (in combination with the `CommonsChunkPlugin`). This is discouraged in webpack 4. Instead the `optimization.splitChunks` option takes care of separating vendors and app modules and creating a separate file. **Do not** create a entry for vendors or other stuff which is not the starting point of execution.
8468

8569
### Multi Page Application
8670

@@ -100,6 +84,6 @@ module.exports = {
10084

10185
**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:
10286

103-
- 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.
87+
- Use `optimization.splitChunks` 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.
10488

10589
T> As a rule of thumb: for each HTML document use exactly one entry point.
Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
title: Plugin Patterns
33
sort: 5
4+
contributors:
5+
- nveenjain
46
---
57

68
Plugins grant unlimited opportunity to perform customizations within the webpack build system. This allows you to create custom asset types, perform unique build modifications, or even enhance the webpack runtime while using middleware. The following are some features of webpack that become useful while writing plugins.
@@ -10,63 +12,66 @@ Plugins grant unlimited opportunity to perform customizations within the webpack
1012
After a compilation is sealed, all structures within the compilation may be traversed.
1113

1214
```javascript
13-
function MyPlugin() {}
14-
15-
MyPlugin.prototype.apply = function(compiler) {
16-
compiler.plugin('emit', function(compilation, callback) {
17-
18-
// Explore each chunk (build output):
19-
compilation.chunks.forEach(function(chunk) {
20-
// Explore each module within the chunk (built inputs):
21-
chunk.modules.forEach(function(module) {
22-
// Explore each source file path that was included into the module:
23-
module.fileDependencies.forEach(function(filepath) {
24-
// we've learned a lot about the source structure now...
15+
class MyPlugin {
16+
apply(compiler) {
17+
compiler.hooks.emit.tapAsync('MyPlugin', (compilation, callback) => {
18+
// Explore each chunk (build output):
19+
compilation.chunks.forEach(chunk => {
20+
// Explore each module within the chunk (built inputs):
21+
chunk.modules.forEach(module => {
22+
// Explore each source file path that was included into the module:
23+
module.fileDependencies.forEach(filepath => {
24+
// we've learned a lot about the source structure now...
25+
});
2526
});
26-
});
2727

28-
// Explore each asset filename generated by the chunk:
29-
chunk.files.forEach(function(filename) {
30-
// Get the asset source for each file generated by the chunk:
31-
var source = compilation.assets[filename].source();
28+
// Explore each asset filename generated by the chunk:
29+
chunk.files.forEach(filename => {
30+
// Get the asset source for each file generated by the chunk:
31+
var source = compilation.assets[filename].source();
32+
});
3233
});
33-
});
34-
35-
callback();
36-
});
37-
};
3834

35+
callback();
36+
});
37+
}
38+
}
3939
module.exports = MyPlugin;
4040
```
4141

42-
- `compilation.modules`: An array of modules (built inputs) in the compilation. Each module manages the build of a raw file from your source library.
43-
- `module.fileDependencies`: An array of source file paths included into a module. This includes the source JavaScript file itself (ex: `index.js`), and all dependency asset files (stylesheets, images, etc) that it has required. Reviewing dependencies is useful for seeing what source files belong to a module.
44-
- `compilation.chunks`: An array of chunks (build outputs) in the compilation. Each chunk manages the composition of a final rendered assets.
45-
- `chunk.modules`: An array of modules that are included into a chunk. By extension, you may look through each module's dependencies to see what raw source files fed into a chunk.
46-
- `chunk.files`: An array of output filenames generated by the chunk. You may access these asset sources from the `compilation.assets` table.
42+
* `compilation.modules`: An array of modules (built inputs) in the compilation. Each module manages the build of a raw file from your source library.
43+
* `module.fileDependencies`: An array of source file paths included into a module. This includes the source JavaScript file itself (ex: `index.js`), and all dependency asset files (stylesheets, images, etc) that it has required. Reviewing dependencies is useful for seeing what source files belong to a module.
44+
* `compilation.chunks`: An array of chunks (build outputs) in the compilation. Each chunk manages the composition of a final rendered assets.
45+
* `chunk.modules`: An array of modules that are included into a chunk. By extension, you may look through each module's dependencies to see what raw source files fed into a chunk.
46+
* `chunk.files`: An array of output filenames generated by the chunk. You may access these asset sources from the `compilation.assets` table.
4747

4848
### Monitoring the watch graph
4949

5050
While running webpack middleware, each compilation includes a `fileDependencies` array (what files are being watched) and a `fileTimestamps` hash that maps watched file paths to a timestamp. These are extremely useful for detecting what files have changed within the compilation:
5151

5252
```javascript
53-
function MyPlugin() {
54-
this.startTime = Date.now();
55-
this.prevTimestamps = {};
53+
class MyPlugin {
54+
constructor() {
55+
this.startTime = Date.now();
56+
this.prevTimestamps = {};
57+
}
58+
apply(compiler) {
59+
compiler.hooks.emit.tapAsync('MyPlugin', (compilation, callback) => {
60+
var changedFiles = Object.keys(compilation.fileTimestamps).filter(
61+
watchfile => {
62+
return (
63+
(this.prevTimestamps[watchfile] || this.startTime) <
64+
(compilation.fileTimestamps[watchfile] || Infinity)
65+
);
66+
}
67+
);
68+
69+
this.prevTimestamps = compilation.fileTimestamps;
70+
callback();
71+
});
72+
}
5673
}
5774

58-
MyPlugin.prototype.apply = function(compiler) {
59-
compiler.plugin('emit', function(compilation, callback) {
60-
61-
var changedFiles = Object.keys(compilation.fileTimestamps).filter(function(watchfile) {
62-
return (this.prevTimestamps[watchfile] || this.startTime) < (compilation.fileTimestamps[watchfile] || Infinity);
63-
}.bind(this));
64-
65-
this.prevTimestamps = compilation.fileTimestamps;
66-
callback();
67-
}.bind(this));
68-
};
69-
7075
module.exports = MyPlugin;
7176
```
7277

@@ -77,22 +82,21 @@ You may also feed new file paths into the watch graph to receive compilation tri
7782
Similar to the watch graph, it's fairly simple to monitor changed chunks (or modules, for that matter) within a compilation by tracking their hashes.
7883

7984
```javascript
80-
function MyPlugin() {
81-
this.chunkVersions = {};
85+
class MyPlugin {
86+
constructor() {
87+
this.chunkVersions = {};
88+
}
89+
apply(compiler) {
90+
compiler.hooks.emit.tapAsync('MyPlugin', (compilation, callback) => {
91+
var changedChunks = compilation.chunks.filter(chunk => {
92+
var oldVersion = this.chunkVersions[chunk.name];
93+
this.chunkVersions[chunk.name] = chunk.hash;
94+
return chunk.hash !== oldVersion;
95+
});
96+
callback();
97+
});
98+
}
8299
}
83100

84-
MyPlugin.prototype.apply = function(compiler) {
85-
compiler.plugin('emit', function(compilation, callback) {
86-
87-
var changedChunks = compilation.chunks.filter(function(chunk) {
88-
var oldVersion = this.chunkVersions[chunk.name];
89-
this.chunkVersions[chunk.name] = chunk.hash;
90-
return chunk.hash !== oldVersion;
91-
}.bind(this));
92-
93-
callback();
94-
}.bind(this));
95-
};
96-
97101
module.exports = MyPlugin;
98102
```

0 commit comments

Comments
 (0)