-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (173 loc) · 4.92 KB
/
index.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
181
182
183
184
185
// Set a true or false for production/development. Use to run certain plugins
const devBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() === 'development')
const stageBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() === 'staging')
const productionBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() === 'production')
const debugMode = ((process.env.NODE_ENV || '').trim().toLowerCase() === 'debug')
// Dependencies
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
const Metalsmith = require('metalsmith')
const markdown = require('metalsmith-markdown')
const layouts = require('metalsmith-layouts')
const assets = require('metalsmith-assets')
const collections = require('metalsmith-collections')
const permalinks = require('metalsmith-permalinks')
const browserSync = devBuild ? require('metalsmith-browser-sync') : null
const globaldata = require('metalsmith-metadata')
const sass = require('metalsmith-sass')
const inplace = require('metalsmith-in-place')
const debug = require('metalsmith-debug')
const helpers = require('metalsmith-register-helpers')
const sitemap = require('metalsmith-mapsite')
const postcss = require('metalsmith-with-postcss')
const paths = require('metalsmith-paths')
const drafts = require('metalsmith-drafts')
const webpack = require('metalsmith-webpack2')
const models = require('metalsmith-models')
const filedata = require('metalsmith-filedata')
const writemetadata = require('metalsmith-writemetadata')
const raw = require('metalsmith-raw')
const fingerprint = require('metalsmith-fingerprint-ignore')
const pkg = require('./package.json')
const config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'))
// Global Configuration
let assetPath
if (devBuild) {
assetPath = config.assetPath.development
}
if (stageBuild) {
assetPath = config.assetPath.stage
}
if (productionBuild) {
assetPath = config.assetPath.production
}
config.assetPath = assetPath
config.version = pkg.version
config.dependencies = pkg.dependencies
config.repository = pkg.repository.url
config.devBuild = devBuild
config.debugMode = debugMode
// Adds metadata from files
const data = {}
if (fs.existsSync(config.src + 'data/globals/')) {
const dataFiles = fs.readdirSync(path.join(__dirname, config.src + 'data', 'globals'))
dataFiles.forEach(function (filename) {
data[filename.split('.')[0]] = 'data/globals/' + filename
})
}
// Metalsmith Build
const ms = Metalsmith(__dirname)
.source(config.src)
.destination(config.dest)
.metadata(config)
.use(globaldata(data))
.use(models({
directory: config.src + 'data/models'
}))
.use(collections({
posts: {
pattern: 'posts/**/!(index.md)',
sortBy: 'date',
reverse: true
},
collection2: {
pattern: 'collection2/**/!(index.md)',
sortBy: 'date',
reverse: true
}
}))
.use(sass({
outputStyle: devBuild ? 'expanded' : 'compressed',
outputDir: 'styles',
sourceMap: devBuild || false,
sourceMapContents: devBuild || false
}))
.use(postcss({
pattern: ['**/*.css', '!**/_*/*', '!**/_*'],
from: '*.scss',
to: '*.css',
map: devBuild ? {inline: false} : false,
plugins: {
'autoprefixer': {browsers: ['> 0.5%', 'Explorer >= 10']}
}
}))
.use(filedata({
pattern: ['styles/*.css'],
key: 'cssData'
}))
.use(webpack(require('./webpack.config.js')(config)))
.use(fingerprint({
pattern: 'styles/main.css',
keep: true
}))
.use(raw())
.use(helpers({
directory: 'lib/helpers'
}))
.use(inplace({
engine: 'handlebars',
pattern: '**/*.{html,xml,txt,md}',
directory: config.src
}))
.use(markdown({
smartypants: true,
gfm: true,
tables: true,
langPrefix: 'language-'
}))
.use(layouts({
engine: 'handlebars',
directory: 'layouts',
partials: 'partials',
default: 'default.hbs',
pattern: '**/*.html'
}))
.use(drafts())
.use(permalinks({
relative: false,
pattern: ':file',
linksets: [{
match: {collection: 'articles'},
pattern: ':collection/:title'
}, {
match: {collection: 'work'},
pattern: ':collection/:title'}
]
}))
.use(paths({
property: 'paths',
directoryIndex: 'index.html'
}))
.use(assets({
source: './assets', // relative to the working directory
destination: './assets' // relative to the build directory
}))
if (debugMode) {
ms.use(writemetadata({
pattern: ['**/*.md', '**/*.html'],
bufferencoding: 'utf8'
}))
}
if (devBuild) {
ms.use(browserSync({
server: config.dest,
files: [config.src + '**/*.*', 'layouts/*.*', 'partials/**/*.*'],
open: false,
notify: false
}))
}
ms.use(debug({
files: false,
match: '**/*.md'
}))
.use(sitemap({ // generate sitemap.xml
hostname: config.url,
omitIndex: true
}))
.build(function (error) {
console.log((devBuild ? 'Development' : 'Production'), 'build success, version', pkg.version)
if (error) {
throw error
}
})