-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.mjs
185 lines (172 loc) · 4.05 KB
/
build.mjs
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
#!/usr/bin/env node
"use strict";
import { join } from "path";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { Renderer } from "marked";
import metalsmith from "metalsmith";
import babel from "metalsmith-babel";
import collections from "metalsmith-collections";
import fingerprint from "metalsmith-fingerprint-ignore";
import htmlmin from "metalsmith-html-minifier";
import msif from "metalsmith-if";
import discoverHelpers from "metalsmith-discover-helpers";
import discoverPartials from "metalsmith-discover-partials";
import layouts from "metalsmith-layouts";
import less from "metalsmith-less";
import markdown from "metalsmith-markdown";
import moveup from "metalsmith-move-up";
import permalinks from "metalsmith-permalinks";
import prism from "metalsmith-prism";
import serve from "metalsmith-serve";
import watch from "metalsmith-watch";
import importJson from "./scripts/utils/importJson.mjs";
import remove from "./scripts/plugins/remove.mjs";
import fingerprintMeta from "./scripts/plugins/fingerprint-meta.mjs";
import addStyle from "./scripts/plugins/add-styles.mjs";
import addScript from "./scripts/plugins/add-scripts.mjs";
import replaceVersion from "./scripts/plugins/replace-version.mjs";
import updateMarkdownToHtml from "./scripts/plugins/update-md-to-html.mjs";
const isDev = process.env.NODE_ENV === "development";
const isProd = process.env.NODE_ENV === "production";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* The build pipeline
*/
async function build() {
const site = await importJson('./src/content/site.json', import.meta.url)
const packageJson = await importJson('./package.json', import.meta.url)
metalsmith(join(__dirname))
.metadata({
site,
package: packageJson,
})
.use(replaceVersion())
.use(
babel({
presets: ["@babel/preset-env"],
sourceMaps: isDev,
compact: isProd,
comments: isDev,
minified: isProd,
})
)
.use(
less({
render: {
compress: true,
},
useDynamicSourceMap: true,
})
)
.use(
msif(
isProd,
fingerprint({
pattern: ["**/css/*.css", "**/js/*.js", "**/*.svg", "**/*.jpg"],
})
)
)
.use(addStyle())
.use(addScript())
.use(
collections({
posts: {
pattern: "content/posts/**/*",
sortBy: "date",
reverse: true,
refer: false,
},
})
)
.use(
moveup({
pattern: "content/**/*",
})
)
.use(
moveup({
pattern: "posts/**/*",
})
)
.use(
moveup({
pattern: "static/**/*",
})
)
.use(
markdown({
breaks: true,
langPrefix: "language-",
renderer: new Renderer(),
})
)
.use(updateMarkdownToHtml())
.use(prism())
.use(
permalinks({
relative: false,
})
)
.use(
discoverHelpers({
directory: "scripts/helpers",
pattern: /\.cjs$/,
})
)
.use(
discoverPartials({
directory: "src/layouts/partials",
pattern: /\.hbs$/,
})
)
.use(
layouts({
directory: "src/layouts",
pattern: "**/*.html",
})
)
.use(msif(isProd, fingerprintMeta()))
.use(
htmlmin({
removeAttributeQuotes: false,
})
)
.use(
remove({
pattern: ["**/layouts/**", "site.json", ".eslintrc"],
})
)
.use(
msif(
isDev,
watch({
paths: {
"${source}/**/*": true,
"${source}/layouts/**/*": "**/*.md",
},
livereload: isDev,
})
)
)
.use(
msif(
isDev,
serve({
port: 7000,
http_error_files: {
// eslint-disable-line camelcase
404: "/404.html",
},
})
)
)
.destination(join(__dirname, "build"))
.build((err) => {
if (err) {
throw err;
}
});
}
build();