forked from yamadashy/tech-blog-rss-feed
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
111 lines (93 loc) · 2.96 KB
/
.eleventy.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
const htmlmin = require('html-minifier-terser');
const Image = require('@11ty/eleventy-img');
const path = require('path');
const ts = require('typescript');
const constants = require('./src/common/constants');
const eleventyCacheOption = require('./src/common/eleventy-cache-option');
const CleanCSS = require("clean-css");
Image.concurrency = 50;
const minifyHtmlTransform = (content, outputPath) => {
if(outputPath && outputPath.endsWith('.html')) {
return htmlmin.minify(content, {
// オプション参考: https://github.com/terser/html-minifier-terser#options-quick-reference
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
maxLineLength: 1000
});
}
return content;
}
const imageShortcode = async (src, alt, pathPrefix = '') => {
let metadata = null;
try {
metadata = await Image(src, {
widths: [150, 450],
formats: ["webp", "jpeg"],
outputDir: 'public/images/feed-thumbnails',
urlPath: `${pathPrefix}images/feed-thumbnails/`,
cacheOptions: eleventyCacheOption,
sharpWebpOptions: {
quality: 50,
},
sharpJpegOptions: {
quality: 70,
}
});
} catch (e) {
// エラーが起きたら代替画像にする
console.log('[image-short-code] error', src);
return `<img src='${pathPrefix}images/alternate-feed-image.png' alt='${alt}' loading='lazy' width='256' height='256'>`
}
return Image.generateHTML(metadata, {
alt,
sizes: '100vw',
loading: 'lazy',
decoding: 'async',
});
}
const relativeUrlFilter = (url) => {
const relativeUrl = path.relative(url, '/');
return relativeUrl === '' ? './' : `${relativeUrl}/`;
}
const minifyCssFilter = (css) => {
return new CleanCSS({}).minify(css).styles;
}
const supportTypeScriptTemplate = (eleventyConfig) => {
eleventyConfig.addTemplateFormats('ts');
eleventyConfig.addExtension('ts', {
outputFileExtension: 'js',
compile: async (inputContent) => {
return async () => {
const result = ts.transpileModule(inputContent, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
return result.outputText;
}
}
});
}
module.exports = function (eleventyConfig) {
// static assets
eleventyConfig.addPassthroughCopy('src/site/images');
eleventyConfig.addPassthroughCopy('src/site/feeds');
// images
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode);
// minify html
eleventyConfig.addTransform('minify html', minifyHtmlTransform);
// relative path
eleventyConfig.addFilter("relativeUrl", relativeUrlFilter);
// minify css
eleventyConfig.addFilter("minifyCss", minifyCssFilter);
// TypeScript
supportTypeScriptTemplate(eleventyConfig);
// TODO: _data も TypeScript 対応したい
// @see https://github.com/11ty/eleventy/discussions/1835
return {
htmlTemplateEngine: 'njk',
dir: {
input: 'src/site',
output: 'public'
}
}
}