-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeMetalsmithConfig.js
48 lines (45 loc) · 1.47 KB
/
makeMetalsmithConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const metalsmith = require('metalsmith');
const inPlace = require('metalsmith-in-place');
const beautify = require('metalsmith-beautify');
const layout = require('metalsmith-layouts');
const readFileSync = require('jsonfile').readFileSync;
const fs = require('fs');
const manifestToAssets = require('./manifestToAssets');
module.exports = function makeMetalsmithConfig(dir, userConfig) {
const configuration = Object.assign({
source: `${dir}/src/views`,
destination: `${dir}/dist`,
assetDir: `${dir}/dist/assets`,
}, userConfig || {});
if (!configuration.assets) {
const manifestPath = `${dir}/dist/assets/manifest.json`;
try {
const manifest = readFileSync(manifestPath);
configuration.assets = manifestToAssets(manifest, `${dir}/dist/assets`, (file) => {
return () => fs.readFileSync(`${dir}/dist/assets/${file}`).toString()
});
} catch (e) {}
}
return metalsmith(dir,)
.source(configuration.source)
.destination(`${dir}/dist`)
.use(inPlace({
engineOptions: {
path: configuration.source
}
}))
.clean(false)
.use((files) => {
if (configuration.assets) {
for (const file in files) {
files[file].assets = configuration.assets
}
}
})
.use(layout({
engine: 'nunjucks',
default: `${configuration.source}/lib/layouts/default.njk`
}))
.use(beautify())
.ignore(`${configuration.source}/lib/**`);
}