Skip to content

Commit 8932af1

Browse files
committed
Add support for metalsmith builder hooks
Close vuejs#427
1 parent 182feab commit 8932af1

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ vue init ~/fs/path/to-custom-template my-project
7777
- `prompts`: used to collect user options data;
7878

7979
- `filters`: used to conditional filter files to render.
80+
81+
- `metalsmith`: used to add custom metalsmith plugins in the chain.
8082

8183
- `completeMessage`: the message to be displayed to the user when the template has been generated. You can include custom instruction here.
8284

@@ -179,6 +181,36 @@ The `skipInterpolation` field in the metadata file should be a [minimatch glob p
179181
}
180182
```
181183

184+
#### Metalsmith
185+
186+
`vue-cli` uses [metalsmith](https://github.com/segmentio/metalsmith) to generate the project.
187+
188+
You may customize the metalsmith builder created by vue-cli to register custom plugins.
189+
190+
```javascript
191+
{
192+
"metalsmith": function(metalsmith, opts, helpers) {
193+
function customMetalsmithPlugin(files, metalsmith, done) {
194+
// Implement something really custom here.
195+
done(null, files);
196+
}
197+
198+
metalsmith.use(customMetalsmithPlugin);
199+
}
200+
}
201+
```
202+
203+
If you need your to hook metalsmith before questions are asked, you may use an object with `before`/`after` keys
204+
205+
```javascript
206+
{
207+
"metalsmith": {
208+
before: function(metalsmith, opts, helpers) {},
209+
after: function(metalsmith, opts, helpers) {}
210+
}
211+
}
212+
```
213+
182214
#### Additional data available in meta.{js,json}
183215

184216
- `destDirName` - destination directory name

lib/generate.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,24 @@ module.exports = function generate (name, src, dest, done) {
4343
opts.helpers && Object.keys(opts.helpers).map(function (key) {
4444
Handlebars.registerHelper(key, opts.helpers[key])
4545
})
46-
metalsmith
47-
.use(askQuestions(opts.prompts))
46+
47+
var helpers = {chalk, logger}
48+
49+
if (opts.metalsmith && typeof opts.metalsmith.before === 'function') {
50+
opts.metalsmith.before(metalsmith, opts, helpers)
51+
}
52+
53+
metalsmith.use(askQuestions(opts.prompts))
4854
.use(filterFiles(opts.filters))
4955
.use(renderTemplateFiles(opts.skipInterpolation))
50-
.clean(false)
56+
57+
if (typeof opts.metalsmith === 'function') {
58+
opts.metalsmith(metalsmith, opts, helpers)
59+
} else if (opts.metalsmith && typeof opts.metalsmith.after === 'function') {
60+
opts.metalsmith.after(metalsmith, opts, helpers)
61+
}
62+
63+
metalsmith.clean(false)
5164
.source('.') // start from template root instead of `./src` which is Metalsmith's default for `source`
5265
.destination(dest)
5366
.build(function (err, files) {

0 commit comments

Comments
 (0)