Skip to content

Commit c2bda85

Browse files
committed
Update the "Build a Pipeline" documentation
1 parent 7cf25fe commit c2bda85

File tree

1 file changed

+110
-70
lines changed

1 file changed

+110
-70
lines changed

src/build-pipeline.md

Lines changed: 110 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,129 @@ layout: default
33
title: Build a Pipeline
44
---
55

6-
If you want to support other JS dialects or CSS preprocessors, you can
7-
contribute an additional pipeline to the project. We recommend that you read the
8-
source for the
9-
[faucet-pipeline-static](https://github.com/faucet-pipeline/faucet-pipeline-static)
10-
project. It is the simplest of the existing pipelines, because it just copies
11-
file from one place to another.
6+
You can add support for other file types or CSS/JS dialects by writing your own
7+
pipeline. Here we present a quick hands-on tutorial and then a reference to the
8+
parameters provided.
129

13-
## Registering your pipeline
10+
## Tutorial
1411

15-
The faucet-pipeline project has a list of known pipelines that it automatically
16-
requires and initializes. When you write a new plugin it won't be on that list.
17-
In this case, you need to configure faucet to load your plugin. In your
18-
faucet.config.js you put:
12+
Let's start by initializing a new npm repository for your pipeline, and add
13+
`faucet-pipeline-core` as a dependency:
1914

20-
```javascript
21-
module.exports = {
22-
name: [{
23-
a: 1,
24-
b: 2
25-
}]
26-
27-
plugins: {
28-
name: {
29-
plugin: path.resolve("./path/to/your/plugin"),
30-
bucket: "chosen-bucket"
31-
}
32-
}
33-
}
15+
```
16+
mkdir faucet-pipeline-example
17+
cd faucet-pipeline-example
18+
npm init -y
19+
npm i --save faucet-pipeline-core
3420
```
3521

36-
With this configuration, your plugin with the name `name` will be loaded from
37-
the provided path when running `faucet`. You also need to choose one of our four
38-
"buckets". It is a categorization of the pipelines that determines the order in
39-
which they will be run. You have the choice between the following buckets – the
40-
choice depends on the kind of files that your pipeline generates:
22+
Adjust the type to be a `module` in the package.json; we have left the Stone
23+
Age. Let's start by setting it up in an example faucet configuration
24+
`faucet.config.js`:
25+
26+
```js
27+
export const example = [{
28+
a: 1,
29+
b: 2
30+
}];
31+
32+
export const plugins = [
33+
{
34+
key: "example",
35+
bucket: "static",
36+
plugin: function(config) {
37+
return function() {
38+
console.log(config);
39+
}
40+
}
41+
}
42+
]
43+
```
4144

42-
* static: Static files like images or fonts
43-
* scripts: JavaScript/WebAssembly files
44-
* styles: CSS files
45-
* markup: HTML files
45+
If you run `npx faucet` now, your output will be:
46+
47+
```
48+
[ { a: 1, b: 2 } ]
49+
```
4650

47-
expect your main module to export a function with the following signature:
51+
The `plugins` that are exported are an array of additional plugins added to the
52+
default ones that faucet knows about. Each entry is an object with a key, bucket
53+
and plugin (we get back to buckets later). faucet will then check if in the
54+
configuration there is a variable exported with the `key` you provided (in our
55+
case, that would be `example`). If so, it will initialize your plugin function.
56+
We expect that the plugin function has the following signature:
4857

4958
```javascript
50-
module.exports = function(pluginConfig, assetManager, options) {
59+
export function plugin(pluginConfig, assetManager, options) {
60+
// initialize your pipeline
61+
62+
return function(filepaths) {
63+
// run the pipeline
64+
}
5165
}
5266
```
5367

5468
The `pluginConfig` is whatever you write in the config file (in the example
55-
above this would be `[{ a: 1, b: 2 }]`). The convention is to expect an array of
56-
tasks. Each of them is an object with a `source` and a `target` plus any other
57-
things you need to configure. The assetManager will be described below in its
58-
own section. `options` is an object with the following keys:
59-
60-
* `browsers` is the detected [browserslist](https://github.com/ai/browserslist).
61-
Learn more about it in the `browsers` section.
62-
* `sourcemaps` is a boolean that indicates if you should add sourcemaps to your
63-
generated output (applies to JS and CSS only)
64-
* `compact` is a boolean that indicates if the user wants a compact output
65-
format or not.
69+
above this would be `[{ a: 1, b: 2 }]` which explains the output).
70+
The convention is to expect an array of tasks. Each of them is an object with a
71+
`source` and a `target` plus any other things you need to configure. The other
72+
two options are described below: [`assetManager`](#assetManager) and
73+
[`options`](#Options).
6674

67-
This function should return a function that runs your pipeline. This function
75+
The function should return a function that runs your pipeline. This function
6876
takes an optional argument:
6977

70-
* If it is undefined, then run the pipeline
78+
* If it is `undefined`, then run the pipeline.
7179
* Otherwise, it is an array of paths. Only run the pipeline, if changes to these
7280
files can change your output.
7381

82+
## Skeleton
83+
84+
This is the skeleton of the `index.js` of your plugin:
85+
86+
```javascript
87+
// the key that is exported in faucet.config.js
88+
export const key = "example";
89+
90+
// the bucket - see below
91+
export const bucket = "static";
92+
93+
// your plugin
94+
export function plugin(pluginConfig, assetManager, options) {
95+
// initialize your pipeline
96+
97+
return function(filepaths) {
98+
// run the pipeline
99+
}
100+
}
101+
```
102+
103+
As a user of the plugin, you can then use it like this in your
104+
`faucet.config.js`:
105+
106+
```js
107+
import * as examplePlugin from "...";
108+
109+
export const example = [{
110+
a: 1,
111+
b: 2
112+
}];
113+
114+
export const plugins = [ examplePlugin ];
115+
```
116+
117+
## Buckets
118+
119+
You also need to choose one of our four "buckets". It is a categorization of the
120+
pipelines that determines the order in which they will be run. You have the
121+
choice between the following buckets – the choice depends on the kind of files
122+
that your pipeline generates:
123+
124+
* static: Static files like images or fonts
125+
* scripts: JavaScript/WebAssembly files
126+
* styles: CSS files
127+
* markup: HTML files
128+
74129
## assetManager
75130

76131
The assetManager helps you to write files to disk. It will make sure that your
@@ -115,27 +170,12 @@ You can also receive entries from the manifest like this:
115170
assetManager.manifest.get(name)
116171
```
117172

118-
## browsers
173+
## Options
119174

120-
`browsers` is the detected [browserslist](https://github.com/ai/browserslist).
121-
It is an object with keys for each browserslist. Your plugin can ignore this
122-
when it doesn't produce code that needs to be modified due to supported
123-
browsers.
175+
`options` is an object with the following keys derived from the CLI invocation:
124176

125-
If no browserslist was detected, the object will be empty. If one was detected,
126-
it contains at least the browserslist `"default"` which you should default to.
127-
You can allow users to ignore the browserslist for one of the bundles or choose
128-
another browserslist. The convention for the name of that options is
129-
`browserslist`. An example for an implementation could look like this:
177+
* `sourcemaps` is a boolean that indicates if you should add sourcemaps to your
178+
generated output.
179+
* `compact` is a boolean that indicates if the user wants a compact output
180+
format or not.
130181

131-
```javascript
132-
let selectedBrowsers;
133-
let { browserslist } = bundleConfig;
134-
if(browserslist === false) {
135-
selectedBrowsers = null;
136-
} else if(browserslist) {
137-
selectedBrowsers = browsers[browserslist];
138-
} else {
139-
selectedBrowsers = browsers.defaults;
140-
}
141-
```

0 commit comments

Comments
 (0)