-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
70 lines (60 loc) · 2.12 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
const postcss = require("postcss");
const postcssImport = require("postcss-import");
const postcssCustomMedia = require("postcss-custom-media");
const htmlmin = require("html-minifier");
const esbuild = require("esbuild");
const { minify } = require("csso");
module.exports = function (eleventyConfig) {
const SRC_DIR = "src";
const OUT_DIR = "dist";
const INDEX_CSS_PATH = `${SRC_DIR}/_includes/styles/index.css`;
const IS_PROD = process.env.ELEVENTY_ENV === "production";
// Pass all files in the `public` directory to the `OUT_DIR`
eleventyConfig.addPassthroughCopy({
"public/**/*": "../dist",
});
// Make sure Elleventy watches TS files and triggers a build when they change.
// Bundling is handled by `esbuild` after the Elleventy build ends.
eleventyConfig.addWatchTarget(`${SRC_DIR}/scripts/`);
// Transform CSS with PostCSS plugins: {{ css | postcss }}
eleventyConfig.addNunjucksAsyncFilter("postcss", (css, callback) => {
postcss([postcssImport, postcssCustomMedia])
.process(css, { from: INDEX_CSS_PATH })
.then((result) => callback(null, result.css));
});
// Minify CSS in production: {{ css | cssmin }}
eleventyConfig.addFilter("cssmin", (code) =>
IS_PROD ? minify(code).css : code
);
// Bundle TS with `esbuild` after the Elleventy build ends
eleventyConfig.on("eleventy.after", () =>
esbuild.build({
entryPoints: [`${SRC_DIR}/scripts/index.ts`],
bundle: true,
target: "es2015", // ES6
outdir: OUT_DIR,
minify: IS_PROD,
sourcemap: !IS_PROD,
})
);
if (IS_PROD) {
// Minify HTML during the Elleventy build
eleventyConfig.addTransform("htmlmin", function (content) {
if (this.outputPath && this.outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
sortAttributes: true,
sortClassName: true,
});
return minified;
}
return content;
});
}
return {
dir: { input: SRC_DIR, output: OUT_DIR },
};
};