forked from ai/easings.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
340 lines (281 loc) · 7.82 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
const yamlParse = require("js-yaml");
const Mustache = require("mustache");
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const unlink = promisify(fs.unlink);
const copyFile = promisify(fs.copyFile);
const Parcel = require("parcel-bundler");
const PostHTML = require("posthtml");
const PostHTMLNano = require("htmlnano");
const MQPacker = require("css-mqpacker");
const postcssCustomProperties = require("postcss-custom-properties");
const PostCSS = require("postcss");
const Terser = require("terser");
const format = require("./helpers/format");
const i18nDir = path.join(__dirname, "i18n");
const langList = fs
.readdirSync(i18nDir)
.filter(filename => !/^_/.test(filename) && /\.ya?ml$/i.test(filename))
.map(filename => fs.readFileSync(path.join(i18nDir, filename)))
.map(file => yamlParse.load(file))
.filter(dic => dic.version && dic.version > 1 && dic.lang_name);
const linksElements = [
"js-info-name",
"js-info-func",
"js-info-simple",
"js-info-complex",
"js-cubic-bezier"
];
const htmlMinifyOptions = {
mergeScripts: false,
minifySvg: false
};
const shortCssClassName = generateCssClassName();
const bundler = new Parcel("./src/index.pug", {
sourceMaps: false,
scopeHoist: true,
publicUrl: "./"
});
const errorBundler = new Parcel("./src/404.pug", {
sourceMaps: false,
scopeHoist: true,
publicUrl: "./"
});
async function build() {
await errorBundler.bundle();
let serviceWorkerCode = await readFile("./src/sw.js", "utf8");
const bundleHome = await bundler.bundle();
const bundleAssets = findAssets(bundleHome);
const cssFile = bundleAssets.find(
item => item.type === "css" && item.entryName === "index.css"
);
const keyframesFile = bundleAssets.find(
item => item.type === "css" && item.entryName === "keyframes.css"
);
const jsFile = bundleAssets.find(
item => item.type === "js" && item.entryName === "index.ts"
);
let cssData = await readFile(cssFile.name, "utf8");
let keyframesData = await readFile(keyframesFile.name, "utf8");
let jsData = await readFile(jsFile.name, "utf8");
await Promise.all([
unlink(cssFile.name),
unlink(keyframesFile.name),
unlink(jsFile.name)
]);
const classesList = {};
linksElements.forEach(item => {
classesList[item] = shortCssClassName.next().value;
});
function cssPlugin(root) {
root.walkRules(rule => {
rule.selector = rule.selector.replace(/\.[\w_-]+/g, str => {
const kls = str.substr(1);
if (!classesList[kls]) {
classesList[kls] = shortCssClassName.next().value;
}
return "." + classesList[kls];
});
});
}
const stylesKeyframe = await PostCSS([
postcssCustomProperties({
preserve: false
})
]).process(keyframesData, { from: keyframesFile.name });
await writeFile(keyframesFile.name, stylesKeyframe);
await copyFile("./src/favicon.ico", "./dist/favicon.ico");
const styles = await PostCSS([
postcssCustomProperties({
preserve: false
}),
cssPlugin,
MQPacker
]).process(cssData, { from: cssFile.name });
Object.keys(classesList).forEach(origin => {
const startSelector = `["'.]`;
const endSelector = `["'\\s\\[):,+~>]`;
jsData = jsData.replace(
new RegExp(`(${startSelector})${origin}(${endSelector})`, "g"),
`$1${classesList[origin]}$2`
);
});
jsData = jsData
.replace(
/parcelRequire=function.*\(function \(require\)\s?{/i,
"(function(window,document){"
)
.replace(/}\);$/, "})(window,document);");
const minifyJS = Terser.minify(jsData, {
toplevel: true
});
await writeFile(jsFile.name, minifyJS.code);
bundleAssets.forEach(asset => {
serviceWorkerCode = serviceWorkerCode.replace(
new RegExp(`['"]${asset.entryName}['"]`, "g"),
`"/${path.basename(asset.name)}"`
);
});
serviceWorkerCode = Terser.minify(serviceWorkerCode).code;
function htmlPlugin(lang = "en") {
return tree => {
tree.match({ attrs: { class: true } }, i => ({
tag: i.tag,
content: i.content,
attrs: {
...i.attrs,
class: i.attrs.class
.split(" ")
.map(origin => {
if (!(origin in classesList)) {
console.error(`Class "${origin}" don't use`);
return "";
}
return classesList[origin];
})
.join(" ")
}
}));
tree.match({ tag: "link", attrs: { rel: "stylesheet" } }, file => {
if (file.attrs.href.includes("src.")) {
return {
tag: "style",
content: styles.css
};
}
return file;
});
tree.match({ tag: "link", attrs: { rel: "manifest" } }, file => {
return {
tag: "link",
attrs: {
...file.attrs,
href: `manifest.${lang}.json`
}
};
});
tree.match({ tag: "meta", attrs: { property: "og:image" } }, file => ({
tag: "meta",
attrs: {
...file.attrs,
content: `https://easings.net/${file.attrs.content}`
}
}));
};
}
const manifest = bundleAssets.find(asset => asset.type === "webmanifest");
const manifestFile = await readFile(manifest.name, "utf8");
bundleAssets
.filter(i => i.type === "html")
.forEach(async item => {
const file = await readFile(item.name);
const html = PostHTML().process(file, { sync: true }).html;
if (/\/index\.html$/i.test(item.name)) {
const distDirName = path.dirname(item.name);
langList.forEach(async lang => {
const viewData = format(
lang,
lang.lang_code,
langList.map(dic => ({
code: dic.lang_code,
name: dic.lang_name
}))
);
const htmlFragment = Mustache.render(html, viewData);
const manifestLang = Mustache.render(manifestFile, viewData);
await writeFile(
path.join(distDirName, `sw.${lang.lang_code}.js`),
serviceWorkerCode.replace("/:lang", `/${lang.lang_code}`)
);
await writeFile(
path.join(distDirName, `manifest.${lang.lang_code}.json`),
manifestLang
);
const htmlMinFragment = await PostHTML([
PostHTMLNano(htmlMinifyOptions)
])
.use(htmlPlugin(lang.lang_code))
.process(htmlFragment);
await writeFile(
path.join(distDirName, `${lang.lang_code}.html`),
htmlMinFragment.html.replace(/\n/g, "").replace(/>\s</g, "><")
);
});
const engLang = langList.find(lang => lang.lang_code === "en");
const htmlFragment = Mustache.render(
html,
format(
engLang,
"",
langList.map(dic => ({
code: dic.lang_code,
name: dic.lang_name
}))
)
);
const htmlMinFragment = await PostHTML([
PostHTMLNano(htmlMinifyOptions)
])
.use(htmlPlugin())
.process(htmlFragment);
await writeFile(
item.name,
htmlMinFragment.html.replace(/\n/g, "").replace(/>\s</g, "><")
);
} else {
await writeFile(
item.name,
PostHTML()
.use(htmlPlugin())
.process(file, { sync: true }).html
);
}
});
}
build().catch(error => {
process.stderr.write(error.stack + "\n");
process.exit(1);
});
function findAssets(bundle) {
return Array.from(bundle.childBundles).reduce(
(all, item) => all.concat(findAssets(item)),
[
{
name: bundle.name,
entryName: bundle.entryAsset.basename,
type: bundle.type
}
]
);
}
function* generateCssClassName() {
const options = {
alphabet: "abcefghijklmnopqrstuvwxyz0123456789-_",
length: 1,
index: 0
};
const getClassName = () => {
let result = "";
for (let i = options.length - 1; i >= 0; i--) {
const x = Math.pow(options.alphabet.length, i);
const n = Math.floor(options.index / x);
result += options.alphabet[n % options.alphabet.length];
}
options.index++;
if (options.index > Math.pow(options.alphabet.length, options.length) - 1) {
options.length++;
options.index = 0;
}
return result;
};
while (true) {
let result = getClassName();
while (/^[0-9-].*$/.test(result)) {
result = getClassName();
}
yield result;
}
}