Skip to content

Commit 88fdcba

Browse files
committed
Update render files.
1 parent 5ca1bd9 commit 88fdcba

File tree

2 files changed

+237
-6
lines changed

2 files changed

+237
-6
lines changed

assets/images.11ty.js

+119-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,119 @@
1-
version https://git-lfs.github.com/spec/v1
2-
oid sha256:4c840f634e3c59f4ab713f321f1f126f7a9e88877ef0c2eff715760742308a27
3-
size 2936
1+
const path = require("path")
2+
const util = require("util")
3+
const glob = require("glob")
4+
const sharp = require("sharp")
5+
const mkdirp = require("mkdirp")
6+
7+
const outputPath = `_site/assets/images/`
8+
9+
const resizeConf = {
10+
sizes: [
11+
{
12+
width: 320,
13+
rename: { suffix: "-320w" },
14+
},
15+
{
16+
width: 640,
17+
rename: { suffix: "-640w" },
18+
},
19+
{
20+
width: 1280,
21+
rename: { suffix: "-1280w" },
22+
},
23+
{
24+
width: 1920,
25+
rename: { suffix: "-1920w" },
26+
},
27+
{
28+
width: 3840,
29+
rename: { suffix: "-3840w" },
30+
},
31+
],
32+
jpegOptions: {
33+
quality: 80,
34+
progressive: true,
35+
withMetadata: true,
36+
force: false,
37+
// errorOnUnusedImage: false,
38+
// errorOnEnlargement: false
39+
},
40+
webpOptions: {
41+
quality: 80,
42+
withMetadata: true,
43+
force: true,
44+
},
45+
pngOptions: {
46+
compressionLevel: 8,
47+
force: false,
48+
},
49+
}
50+
51+
module.exports = class {
52+
async data() {
53+
const filePath = path.join(__dirname, `/images/`)
54+
return {
55+
permalink: `/assets/images/images.json`,
56+
eleventyExcludeFromCollections: true,
57+
filePath,
58+
}
59+
}
60+
61+
async loadImages(imgFolder) {
62+
const cwd = path.resolve(imgFolder.file)
63+
const getImages = util.promisify(glob)
64+
const processedImages = []
65+
66+
const imgs = await getImages("**/*(*.jpg|*.png|*.gif)", { cwd: cwd })
67+
const imgsRendered = await getImages("**/*(*.jpg|*.png|*.gif)", { cwd: outputPath })
68+
69+
imgs.forEach(function (img) {
70+
const ext = path.extname(img)
71+
const base = path.basename(img, ext)
72+
const dir = path.dirname(img)
73+
74+
mkdirp.sync(path.join(outputPath, dir))
75+
76+
if (!imgsRendered.includes(img)) {
77+
// console.log(`PASS THROUGH MASTER FOR ${img}`)
78+
let passThroughImg = sharp(imgFolder.file + img)
79+
passThroughImg.toFile(path.join(outputPath, dir, base + ext))
80+
} else {
81+
// console.log(`FOUND MASTER FOR ${img}`)
82+
}
83+
84+
85+
const image = sharp(imgFolder.file + img)
86+
image
87+
.jpeg(resizeConf.jpegOptions)
88+
.png(resizeConf.pngOptions)
89+
.webp(resizeConf.webpOptions)
90+
resizeConf.sizes.forEach(function (size) {
91+
const newPath = path.join(dir, base + size.rename.suffix + ext)
92+
const newPathOutput = path.join(outputPath, newPath)
93+
if (imgsRendered.includes(newPath)) {
94+
// console.log(`FOUND RESIZED IMAGE FOR ${newPath}`);
95+
return;
96+
}
97+
// console.log(`RENDERING NEW RESIZED IMAGE FOR ${newPath}`);
98+
const resized = image.resize({
99+
width: size.width,
100+
withoutEnlargement: true,
101+
kernel: "lanczos2",
102+
})
103+
resized.toFile(newPathOutput);
104+
})
105+
106+
processedImages.push(img)
107+
})
108+
109+
return JSON.stringify(processedImages, null, "\t")
110+
}
111+
112+
async render({ filePath }) {
113+
try {
114+
return await this.loadImages({ file: filePath })
115+
} catch (err) {
116+
throw new Error(err)
117+
}
118+
}
119+
}

assets/styles.11ty.js

+118-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,118 @@
1-
version https://git-lfs.github.com/spec/v1
2-
oid sha256:704cddb384430aff341d8ace1d21055a9d2c4aa62d2110cd14b7491ffda5577b
3-
size 3033
1+
// Shamelessly modified from Eleventastic:
2+
// https://github.com/maxboeck/eleventastic
3+
4+
const path = require("path")
5+
const sass = require("sass")
6+
const CleanCSS = require("clean-css")
7+
const cssesc = require("cssesc")
8+
9+
const isProd = process.env.ELEVENTY_ENV === "production"
10+
11+
// main entry point name
12+
const fileName = "style.scss"
13+
14+
module.exports = class {
15+
async data() {
16+
const filePath = path.join(__dirname, `/scss/${fileName}`)
17+
return {
18+
permalink: '/assets/style.css',
19+
eleventyExcludeFromCollections: true,
20+
layout: 'blank',
21+
filePath,
22+
}
23+
}
24+
25+
// Compile Sass to CSS,
26+
// Embed Source Map in Development
27+
async compile(config) {
28+
return new Promise((resolve, reject) => {
29+
if (!isProd) {
30+
config.sourceMap = true
31+
config.sourceMapEmbed = true
32+
config.outputStyle = "expanded"
33+
}
34+
35+
const result = sass.renderSync(config)
36+
return resolve(result.css.toString())
37+
})
38+
}
39+
40+
// Minify & Optimize with CleanCSS in Production
41+
async minify(css) {
42+
return new Promise((resolve, reject) => {
43+
if (!isProd) {
44+
resolve(css)
45+
}
46+
const minified = new CleanCSS().minify(css)
47+
if (!minified.styles) {
48+
return reject(minified.error)
49+
}
50+
resolve(minified.styles)
51+
})
52+
}
53+
54+
// display an error overlay when CSS build fails.
55+
// this brilliant idea is taken from Mike Riethmuller / Supermaya
56+
// @see https://github.com/MadeByMike/supermaya/blob/master/site/utils/compile-scss.js
57+
renderError(error) {
58+
return `
59+
/* Error compiling stylesheet */
60+
*,
61+
*::before,
62+
*::after {
63+
box-sizing: border-box;
64+
}
65+
html,
66+
body {
67+
margin: 0;
68+
padding: 0;
69+
min-height: 100vh;
70+
font-family: monospace;
71+
font-size: 1.25rem;
72+
line-height:1.5;
73+
}
74+
body::before {
75+
content: '';
76+
background: #000;
77+
top: 0;
78+
bottom: 0;
79+
width: 100%;
80+
height: 100%;
81+
opacity: 0.7;
82+
position: fixed;
83+
}
84+
body::after {
85+
content: '${cssesc(error)}';
86+
white-space: pre;
87+
display: block;
88+
top: 0;
89+
padding: 30px;
90+
margin: 50px;
91+
width: calc(100% - 100px);
92+
color:#721c24;
93+
background: #f8d7da;
94+
border: solid 2px red;
95+
position: fixed;
96+
}`
97+
}
98+
99+
// render the CSS file
100+
async render({filePath}) {
101+
try {
102+
const css = await this.compile({ file: filePath })
103+
const result = await this.minify(css)
104+
return result
105+
} catch (err) {
106+
// if things go wrong
107+
if (isProd) {
108+
// throw and abort in production
109+
throw new Error(err)
110+
} else {
111+
// otherwise display the error overly
112+
console.error(err)
113+
const msg = err.formatted || err.message
114+
return this.renderError(msg)
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)