|
| 1 | +const fs = require('fs'); |
| 2 | +const inclusiveLangPlugin = require('@11ty/eleventy-plugin-inclusive-language'); |
| 3 | +const htmlmin = require('html-minifier'); |
| 4 | +const { minify } = require('terser'); |
1 | 5 | const siteSettings = require('./src/globals/site.json'); |
2 | 6 |
|
3 | | -module.exports = function (config) { |
4 | | - config.addPassthroughCopy('./src/css/tailwind.include.css'); |
5 | | - config.addPassthroughCopy({ public: './' }); |
| 7 | +module.exports = function (eleventyConfig) { |
| 8 | + eleventyConfig.addPassthroughCopy('./src/css/tailwind.include.css'); |
| 9 | + eleventyConfig.addPassthroughCopy({ public: './' }); |
| 10 | + |
| 11 | + eleventyConfig.addPlugin(inclusiveLangPlugin); |
| 12 | + |
| 13 | + eleventyConfig.addNunjucksAsyncFilter('jsmin', async function ( |
| 14 | + code, |
| 15 | + callback |
| 16 | + ) { |
| 17 | + if (process.env.ELEVENTY_ENV === 'production') { |
| 18 | + try { |
| 19 | + const minified = await minify(code); |
| 20 | + callback(null, minified.code); |
| 21 | + } catch (err) { |
| 22 | + console.error('Terser error: ', err); |
| 23 | + // Fail gracefully. |
| 24 | + callback(null, code); |
| 25 | + } |
| 26 | + } else { |
| 27 | + callback(null, code); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + eleventyConfig.setBrowserSyncConfig({ |
| 32 | + callbacks: { |
| 33 | + ready: function (err, bs) { |
| 34 | + bs.addMiddleware('*', (req, res) => { |
| 35 | + const content_404 = fs.readFileSync('dist/404.html'); |
| 36 | + // Provides the 404 content without redirect. |
| 37 | + res.write(content_404); |
| 38 | + // Add 404 http status code in request header. |
| 39 | + res.writeHead(404, { 'Content-Type': 'text/html' }); |
| 40 | + res.end(); |
| 41 | + }); |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + eleventyConfig.addTransform('htmlmin', function (content, outputPath) { |
| 47 | + if ( |
| 48 | + process.env.ELEVENTY_ENV === 'production' && |
| 49 | + outputPath && |
| 50 | + outputPath.endsWith('.html') |
| 51 | + ) { |
| 52 | + return htmlmin.minify(content, { |
| 53 | + useShortDoctype: true, |
| 54 | + removeComments: true, |
| 55 | + collapseWhitespace: true, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return content; |
| 60 | + }); |
6 | 61 |
|
7 | 62 | return { |
8 | 63 | pathPrefix: siteSettings.baseUrl, |
|
0 commit comments