UNPKG

3.7 kBMarkdownView Raw
1[![npm version](https://badge.fury.io/js/metalsmith-changed.svg)](https://badge.fury.io/js/metalsmith-changed) [![Build Status](https://travis-ci.org/arve0/metalsmith-changed.svg?branch=master)](https://travis-ci.org/arve0/metalsmith-changed)
2
3# metalsmith-changed
4Only process files that have changed.
5
6metalsmith-changed will write a ctimes json-file to your `build`-folder in order to keep track of changed files.
7
8 **Must** be used with `metalsmith.clean(false)`, `.clean(true)` (the default) disables metalsmith-changed and all files are passed on to the next plugin.
9
10 metalsmith-changed can also be disabled with `force: true` and individual files can be ignored (always build) with `forcePattern`.
11
12
13## example
14```js
15var Metalsmith = require('metalsmith');
16var changed = require('metalsmith-changed');
17
18Metalsmith()
19 .clean(false)
20 .use(changed())
21 ... // more plugins
22 .build(function (err) {
23 if (err) throw err;
24 });
25```
26
27Which is useful when watching files and livereloading:
28```js
29const Metalsmith = require('metalsmith');
30const changed = require('metalsmith-changed');
31const livereload = require('metalsmith-livereload');
32const nodeStatic = require('node-static');
33const watch = require('glob-watcher');
34const open = require('open');
35
36const DIR = __dirname + '/test/fixtures/';
37
38/**
39 * Build with metalsmith.
40 */
41const build = (clean = false) => (done) => {
42 console.log(`Building. clean: ${clean}.`);
43 Metalsmith(DIR)
44 .clean(clean)
45 .use(changed())
46// .use(expensivePlugin()) // ie markdown -> html
47 .use(livereload({ debug: true }))
48 .build((err, files) => {
49 let filenames = Object.keys(files).join(', ');
50 console.log('Built: ' + filenames);
51 done(err);
52 });
53};
54
55/**
56 * Serve files.
57 */
58var serve = new nodeStatic.Server(DIR + 'build');
59require('http').createServer((req, res) => {
60 req.addListener('end', () => serve.serve(req, res));
61 req.resume();
62}).listen(8080);
63
64/**
65 * Watch files.
66 */
67watch(DIR + 'src/**/*', { ignoreInitial: false }, build(false));
68// watch(DIR + 'templates/**/*', build(true)); // force build of all files
69
70/**
71 * Open browser.
72 */
73open('http://localhost:8080');
74```
75
76As ctimes are persisted to disk, this works nice with cli tools like [watch run](https://www.npmjs.com/package/watch-run) too.
77
78## forcePattern
79If the option `forcePattern` is defined, files matching the pattern(s) will not
80be removed from building even if the file has not changed. `forcePattern` should
81be a string or an array of strings.
82
83[micromatch](https://github.com/jonschlinkert/micromatch) is used for
84matching the files.
85
86Example:
87```js
88Metalsmith()
89 .clean(false)
90 .use(changed({
91 forcePattern: [
92 '**/index.md', // always build index files
93 ... // more patterns
94 ]
95 }))
96 ... // more plugins
97 .build(function(err){
98 if (err) throw err;
99 });
100```
101
102
103## default options
104```js
105changed({
106 force: false, // build all files
107 forcePattern: false, // always build files matching these patterns
108 forceAllPattern: false, // force build of all files if files matching this pattern is changed
109 ctimes: 'metalsmith-changed-ctimes.json' // where to store ctimes, relative to the build folder
110})
111```
112
113
114## metalsmith-changed-ctimes.json
115`metalsmith-changed-ctimes.json` is written to your `build` folder upon every build. `metalsmith-changed` takes ctimes from `files[n].stats.ctime`, so if a plugin creates files with `.stats.ctime`, `metalsmith-changed` can be used with it.
116
117Files without `stats.ctime` are always built.
118
119
120## develop
121```sh
122npm build # babel
123npm test
124DEBUG=metalsmith-changed npm test # test with debug output
125```
126
127## release
128```sh
129npm version patch|minor|major
130npm publish
131```