Skip to content

Commit

Permalink
docs(concepts): Update plugins.md (#1871)
Browse files Browse the repository at this point in the history
Update the plugins page in the concepts section to be compatible with webpack 4. Also adds a note about referencing the plugin name in camel-case when calling the `tap` method.
  • Loading branch information
arjendeblok authored and TheDutchCoder committed Mar 1, 2018
1 parent 6de8690 commit 7d1f662
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/content/concepts/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ A webpack **plugin** is a JavaScript object that has an [`apply`](https://develo
**ConsoleLogOnBuildWebpackPlugin.js**

```javascript
function ConsoleLogOnBuildWebpackPlugin() {

};

ConsoleLogOnBuildWebpackPlugin.prototype.apply = function(compiler) {
compiler.plugin('run', function(compiler, callback) {
console.log("The webpack build process is starting!!!");

callback();
});
};
const pluginName = 'ConsoleLogOnBuildWebpackPlugin';

class ConsoleLogOnBuildWebpackPlugin {
apply(compiler) {
compiler.hooks.run.tap(pluginName, compilation => {
console.log("The webpack build process is starting!!!");
});
}
}
```

T> As a clever JavaScript developer you may remember the [`Function.prototype.apply`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/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.

First parameter of the tap method of the compiler hook should be a camelized version of the plugin name. It is advisable to use a constant for this so it can be reused in all hooks.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function parseAnchor(string) {

return {
title: clean,
id: clean.replace(/[^\w]+/g, '-').toLowerCase()
id: clean.replace(/[^\w\u4e00-\u9fa5]+/g, '-').toLowerCase()
};
}

Expand Down

0 comments on commit 7d1f662

Please sign in to comment.