forked from sitepoint-editors/metalsmith-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
181 lines (164 loc) · 4.66 KB
/
build.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
/*
Metalsmith build file
Build site with `node ./build.js` or `npm start`
Build production site with `npm run production`
*/
'use strict';
var
// defaults
consoleLog = false, // set true for metalsmith file and meta content logging
devBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() !== 'production'),
pkg = require('./package.json'),
// main directories
dir = {
base: __dirname + '/',
lib: __dirname + '/lib/',
source: './src/',
dest: './build/'
},
// modules
metalsmith = require('metalsmith'),
markdown = require('metalsmith-markdown'),
publish = require('metalsmith-publish'),
wordcount = require("metalsmith-word-count"),
collections = require('metalsmith-collections'),
permalinks = require('metalsmith-permalinks'),
inplace = require('metalsmith-in-place'),
layouts = require('metalsmith-layouts'),
sitemap = require('metalsmith-mapsite'),
rssfeed = require('metalsmith-feed'),
sass = require('metalsmith-sass'),
assets = require('metalsmith-assets'),
moveRemove = require('metalsmith-move-remove'),
htmlmin = devBuild ? null : require('metalsmith-html-minifier'),
browsersync = devBuild ? require('metalsmith-browser-sync') : null,
helpers = require('metalsmith-register-helpers'),
// custom plugins
setdate = require(dir.lib + 'metalsmith-setdate'),
moremeta = require(dir.lib + 'metalsmith-moremeta'),
debug = consoleLog ? require(dir.lib + 'metalsmith-debug') : null,
collectionsclean = require(dir.lib + 'metalsmith-collections-clean'),
siteMeta = {
devBuild: devBuild,
version: pkg.version,
name: 'Artisan Maker',
desc: 'A demonstration static site built using Metalsmith',
author: 'Jon Barlow',
contact: 'https://twitter.com/orviwan',
domain: devBuild ? 'http://127.0.0.1' : 'https://orviwan.github.io/artisan-maker', // set domain
rootpath: devBuild ? null : '' // set absolute path (null for relative)
},
templateConfig = {
engine: 'handlebars',
directory: dir.source + 'template/',
partials: dir.source + 'partials/',
default: 'page.html',
pattern: ['**/*.html', '**/*.md']
};
console.log((devBuild ? 'Development' : 'Production'), 'build, version', pkg.version);
var ms = metalsmith(dir.base)
.clean(true) // clean folder before a production build (!devBuild)
.source(dir.source + 'html/') // source folder (src/html/)
.destination(dir.dest) // build folder (build/)
.metadata(siteMeta) // add meta data to every page
.use(publish()) // draft, private, future-dated
.use(setdate()) // set date on every page if not set in front-matter
.use(collectionsclean())
.use(collections({ // determine page collection/taxonomy
page: {
pattern: '**/index.*',
sortBy: 'priority',
reverse: true,
refer: false
},
legal: {
pattern: '**/legal/*',
sortBy: 'priority',
reverse: false,
refer: false
},
blog: {
pattern: 'blog/**/*',
sortBy: 'date',
reverse: true,
refer: true,
limit: 50,
metadata: {
layout: 'blog.html'
}
},
products: {
pattern: 'products/**/*',
sortBy: 'priority',
reverse: false,
refer: true,
metadata: {
layout: 'product.html'
}
}
}))
.use(markdown()) // convert markdown
.use(permalinks({ // generate permalinks
pattern: ':mainCollection/:title'
}))
.use(wordcount({
raw: true
})) // word count
.use(moremeta()) // determine root paths and navigation
.use(helpers({
"directory": "lib/_helpers"
}))
.use(inplace(templateConfig)) // in-page templating
.use(layouts(templateConfig)) // layout templating
// SITEMAP XML
.use(sitemap({
hostname: siteMeta.domain + (siteMeta.rootpath || ''),
omitIndex: true
}))
// BLOG RSS
.use(rssfeed({
collection: 'blog',
site_url: siteMeta.domain + (siteMeta.rootpath || ''),
title: siteMeta.name,
description: siteMeta.desc
}))
// IMAGES
.use(assets({
source: dir.source + 'html/_assets/images/',
destination: '../build/images/'
}));
// SASS
if (devBuild) {
ms.use(sass({
outputStyle: 'expanded',
outputDir: './css/',
sourceMap: true,
sourceMapContents: true
}));
} else {
ms.use(sass({
outputStyle: 'compressed',
outputDir: './css/'
}));
}
ms.use(moveRemove({
remove: ['_assets']
}))
// MINIFY HTML
//if (htmlmin) ms.use(htmlmin());
// DEBUG LOG
if (debug) ms.use(debug()); // output page debugging information
// BROWSERSYNC
if (browsersync) {
ms.use(browsersync(
{ // start test server
server: dir.dest,
files: [dir.source + '**/*']
})
);
}
// BUILD
ms.build(function(err) {
if (err) throw err;
});