1 | ## var builder = build(nodes, [options])
|
2 |
|
3 | Creates a new builder with `nodes`. `nodes` can either be a `tree` returned from `component-resolver`, or a tree flattened by `component-flatten`.
|
4 |
|
5 | `options`:
|
6 |
|
7 | - `concurrency` <16> - this is how many files the builder will process at a time. In general, a limit at all is only necessary for `EMFILE` errors, but since this builder uses `graceful-fs`, setting `Infinity` should be fine for most build.
|
8 | - `development` - Include files in local components' development fields.
|
9 | - `out` <'components'> - folder where all the components are saved.
|
10 | - `root` <process.cwd()> - root folder
|
11 |
|
12 | ### builder.end([callback])
|
13 |
|
14 | Returns the entire build as a single string (if applicable).
|
15 | Nothing is executed until you execute `.end()`.
|
16 | Aliased as `.build()`.
|
17 |
|
18 | ## Plugins
|
19 |
|
20 | ### Using Plugins
|
21 |
|
22 | Plugins are of the form:
|
23 |
|
24 | ```js
|
25 | .use(field, plugin(options), plugin(options))
|
26 |
|
27 | // example
|
28 | .use('scripts', build.plugins.js())
|
29 | ```
|
30 |
|
31 | Thus, plugins are registered on a per-field, allowing the builder to know which `fields` to unglob.
|
32 |
|
33 | Included plugins are stored in `require('component-builder').plugins`. Please see the documentation on each builder for the included, relevant plugins.
|
34 |
|
35 | ### Creating Plugins
|
36 |
|
37 | Plugins can be defined in one of the following three forms:
|
38 |
|
39 | ```js
|
40 | // synchronous
|
41 | function plugin(options) {
|
42 | return function plugin(file) {
|
43 |
|
44 | }
|
45 | }
|
46 |
|
47 | // asynchronous
|
48 | function plugin(options) {
|
49 | return function plugin(file, done) {
|
50 |
|
51 | }
|
52 | }
|
53 | ```
|
54 |
|
55 | When creating a plugin, you __should__ wrap the actual plugin with another function even if there are no options. This creates a consistent API among all plugins.
|
56 |
|
57 | `file` is created using [manifest](https://github.com/component/manifest.js) and has the following properties:
|
58 |
|
59 | - `path` - the `path` of the file as defined in the component, ex. `index.js`.
|
60 | - `filename` - the absolute `path` of where this file is located, ex. `/Users/jong/app/index.js`.
|
61 | - `extension` - the `extension` of this file, example `js`. This tells plugins how to treat each file.
|
62 | - `node` - the `component.json`
|
63 | - `branch` - the resolved branch based on the resolver.
|
64 | - `manifest` - a resolved "builder" object - look at the source code for more details.
|
65 |
|
66 | There is a convenience method called `file.read`. This allows you to read the current file as a `utf-8` string, which is saved as `file.string`. This would obviously only work with asynchronous plugins. For the `scripts` and `styles` builder, if `file.string` is never populated, the file will not be included in the build.
|
67 |
|
68 | ```js
|
69 | // only includes `.js` files in the build
|
70 | function plugin(file) {
|
71 | if (file.extension === 'js') return;
|
72 | // `file.string = true` is a shortcut to include the string
|
73 | file.string = true;
|
74 | }
|
75 |
|
76 | // read and autoprefix css
|
77 | var autoprefixer = require('autoprefixer');
|
78 |
|
79 | function plugin(file, done) {
|
80 | if (file.extension !== 'css') return done();
|
81 | file.read(function (err, string) {
|
82 | if (err) return done(err);
|
83 | file.string = autoprefixer.process(string).css;
|
84 | done();
|
85 | })
|
86 | }
|
87 | ```
|