Skip to content

Commit 6d1c758

Browse files
committed
Update Writing plugin and Plugin pattern page
1 parent 3ed2596 commit 6d1c758

File tree

2 files changed

+210
-155
lines changed

2 files changed

+210
-155
lines changed
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(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...
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(function(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 = {};
82-
}
83-
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-
85+
class MyPlugin{
86+
constructor(){
87+
this.chunkVersions = {};
88+
}
89+
apply(compiler){
90+
compiler.hooks.emit.tapAsync((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+
});
9397
callback();
94-
}.bind(this));
95-
};
98+
}
99+
}
96100

97101
module.exports = MyPlugin;
98102
```

0 commit comments

Comments
 (0)