Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pluginapi): implement separate directory structure for plugins api #361

Merged
merged 3 commits into from
Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions antwar.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ module.exports = {
);
}
),
pluginsapi: section(
'Plugins API',
function() {
return require.context(
'json-loader!yaml-frontmatter-loader!./content/pluginsapi',
false,
/^\.\/.*\.md$/
);
}
),
loaders: section(
'Loaders',
function() {
Expand Down
2 changes: 1 addition & 1 deletion content/api/plugins.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Plugin API
sort: 5
sort: 4
---

> TODO
6 changes: 6 additions & 0 deletions content/pluginsapi/compilation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Compilation
sort: 3
---

> TODO
52 changes: 52 additions & 0 deletions content/pluginsapi/compiler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Compiler
sort: 2
---

## Compiler

## Watching

## MultiCompiler

## Event Hooks

`environment()`

`after-environment()`

`before-run(compiler: Compiler, callback)`

`run(callback)`

`watch-run(watching: Watching, callback)`

`normal-module-factory(normalModuleFactory: NormalModuleFactory)`

`context-module-factory(contextModuleFactory: ContextModuleFactory)`

`compilation(compilation: Compilation, params: Object)`

`this-compilation(compilation: Compilation, params: Object)`

`after-plugins(compiler: Compiler)`

`after-resolvers(compiler: Compiler)`

`should-emit(compilation: Compilation)`

`emit(compilation: Compilation, callback)`

`after-emit(compilation: Compilation, callback)`

`done(stats: Stats)`

`additional-pass(callback)`

`failed(err: Error)`

`invalid(fileName: string, changeTime)`

`entry-option(context, entry)`

## Examples
6 changes: 6 additions & 0 deletions content/pluginsapi/dependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Dependency
sort: 7
---

> TODO
6 changes: 6 additions & 0 deletions content/pluginsapi/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Examples
sort: 9
---

> TODO
14 changes: 14 additions & 0 deletions content/pluginsapi/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Plugins API
sort: 1
---

## Tapable & Tapable instances
**Tapable Instances** are classes in the webpack source code which have been extended or mixed in from class `Tapable`.
For more information on `Tapable` visit the [tapable repository](https://github.com/webpack/tapable) or visit the [complete overview](./tapable)

Throughout this section are a list of all of the webpack Tapable instances (and their event hooks), which plugin authors can utilize.

## Creating a Plugins

### Different Plugin Shapes
6 changes: 6 additions & 0 deletions content/pluginsapi/module-factories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Context/Normal Module Factories
sort: 5
---

> TODO
6 changes: 6 additions & 0 deletions content/pluginsapi/parser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Parser
sort: 8
---

> TODO
6 changes: 6 additions & 0 deletions content/pluginsapi/resolver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Resolver
sort: 5
---

> TODO
71 changes: 71 additions & 0 deletions content/pluginsapi/tapable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: Tapable
sort: 1
contributors:
- thelarkinn
- pksjce
- e-cloud
---

[Tapable](https://github.com/webpack/tapable) is small library that allows you to add and apply plugins to a javascript module.
It can be inherited or mixed in to other modules. It is similar to NodeJS's `EventEmitter` class, focusing on custom event emission and manipulation.
However, in addition to this, `Tapable` allows you to have access to the "emittee" or "producer" of the event through callbacks arguments.

`Tapable` has four groups of member functions:

* `plugin(name:string, handler:function)` - This allows a custom plugin to register into a **Tapable instance**'s event.
This acts as the same as `on()` of `EventEmitter`, for registering a handler/listener to do something when the signal/event happens.

* `apply(...pluginInstances: (AnyPlugin|function)[])` - `AnyPlugin` should be subclass of [AbstractPlugin](https://github.com/webpack/webpack/blob/master/lib/AbstractPlugin.js), or a class (or object, rare case) has an `apply` method, or just a function with some registration code inside.
This method is just to **apply** plugins' definition, so that the real event listeners can be registered into the **Tapable instance**'s registry.

* `applyPlugins*(name:string, ...)` - The **Tapable instance** can apply all the plugins under a particular hash using these functions.
These group of method act like `emit()` of `EventEmitter`, to control the event emission meticulously with various strategy for various use cases.

* `mixin(pt: Object)` - a simple method to extend `Tapable`'s prototype as a mixin rather than inheritance.

The different `applyPlugins*` methods cover the following use cases:

* Plugins can run serially

* Plugins can run in parallel

* Plugins can run one after the other but taking input from the previous plugin (waterfall)

* Plugins can run asynchronously

* Quit runing plugins on bail: that is once one plugin returns non-`undefined`, jump out of the run flow and return *the return of that plugin*. This sounds like `once()` of `EventEmitter` but is totally different.

## Example
One of webpack's **Tapable instances**, [Compiler](./compiler), is responsible for compiling the webpack configuration object and returning a [Compilation](./compilation) instance. When the Compilation instance runs, it creates the required bundles.

See below is a simplified version of how this looks using `Tapable`.

**node_modules/webpack/lib/Compiler.js**

```javascript
var Tapable = require("tapable");
function Compiler() {
Tapable.call(this);
}
Compiler.prototype = Object.create(Tapable.prototype);
```

Now to write a plugin on the compiler,

**my-custom-plugin.js**

```javascript
function CustomPlugin() {}
CustomPlugin.prototype.apply = function(compiler) {
compiler.plugin('emit', pluginFunction);
}
```

The compiler executes the plugin at the appropriate point in its lifecycle by

**node_modules/webpack/lib/Compiler.js**

```javascript
this.apply*("emit",options) // will fetch all plugins under 'emit' name and run them.
```
18 changes: 18 additions & 0 deletions content/pluginsapi/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Template
sort: 8
---

## MainTemplate

## HotUpdateChunkTemplate

## ChunkTemplate

## ModuleTemplate

## FunctionModuleTemplate

## Event Hooks

## Examples