|
| 1 | +const fs = require('fs'); |
| 2 | +const inclusiveLangPlugin = require('@11ty/eleventy-plugin-inclusive-language'); |
| 3 | +const cacheBuster = require('@mightyplow/eleventy-plugin-cache-buster'); |
| 4 | +const htmlmin = require('html-minifier'); |
| 5 | +const { minify } = require('terser'); |
| 6 | +const siteSettings = require('./src/globals/site.json'); |
| 7 | +const { |
| 8 | + version: eleventyVersion, |
| 9 | +} = require('./node_modules/@11ty/eleventy/package.json'); |
| 10 | + |
| 11 | +module.exports = function (eleventyConfig) { |
| 12 | + eleventyConfig.addPassthroughCopy('./src/css/tailwind.include.css'); |
| 13 | + eleventyConfig.addPassthroughCopy({ public: './' }); |
| 14 | + |
| 15 | + eleventyConfig.addShortcode('year', function () { |
| 16 | + return new Date().getFullYear().toString(); |
| 17 | + }); |
| 18 | + |
| 19 | + eleventyConfig.addShortcode('eleventyVersion', function () { |
| 20 | + return `Eleventy ${eleventyVersion}`; |
| 21 | + }); |
| 22 | + |
| 23 | + eleventyConfig.addFilter('formatDateTime', function (date) { |
| 24 | + return date.toLocaleDateString('en-US', { |
| 25 | + year: 'numeric', |
| 26 | + month: 'long', |
| 27 | + day: 'numeric', |
| 28 | + weekday: 'long', |
| 29 | + hour: 'numeric', |
| 30 | + minute: 'numeric', |
| 31 | + timeZoneName: 'short', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + eleventyConfig.addPlugin(inclusiveLangPlugin); |
| 36 | + |
| 37 | + if (process.env.ELEVENTY_ENV === 'production') { |
| 38 | + eleventyConfig.addPlugin(cacheBuster({ outputDirectory: 'dist' })); |
| 39 | + |
| 40 | + eleventyConfig.addTransform('htmlmin', function (content, outputPath) { |
| 41 | + if (outputPath && outputPath.endsWith('.html')) { |
| 42 | + return htmlmin.minify(content, { |
| 43 | + useShortDoctype: true, |
| 44 | + removeComments: true, |
| 45 | + collapseWhitespace: true, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + return content; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + eleventyConfig.addNunjucksAsyncFilter('jsmin', async function ( |
| 54 | + code, |
| 55 | + callback |
| 56 | + ) { |
| 57 | + if (process.env.ELEVENTY_ENV === 'production') { |
| 58 | + try { |
| 59 | + const minified = await minify(code); |
| 60 | + callback(null, minified.code); |
| 61 | + } catch (err) { |
| 62 | + console.error('Terser error: ', err); |
| 63 | + // Fail gracefully. |
| 64 | + callback(null, code); |
| 65 | + } |
| 66 | + } else { |
| 67 | + callback(null, code); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + eleventyConfig.setBrowserSyncConfig({ |
| 72 | + callbacks: { |
| 73 | + ready: function (err, bs) { |
| 74 | + bs.addMiddleware('*', (req, res) => { |
| 75 | + const content_404 = fs.readFileSync('dist/404.html'); |
| 76 | + // Provides the 404 content without redirect. |
| 77 | + res.write(content_404); |
| 78 | + // Add 404 http status code in request header. |
| 79 | + res.writeHead(404, { 'Content-Type': 'text/html' }); |
| 80 | + res.end(); |
| 81 | + }); |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + return { |
| 87 | + pathPrefix: siteSettings.baseUrl, |
| 88 | + dir: { |
| 89 | + input: 'src', |
| 90 | + output: 'dist', |
| 91 | + includes: 'includes', |
| 92 | + layouts: 'includes/layouts', |
| 93 | + data: 'globals', |
| 94 | + }, |
| 95 | + }; |
| 96 | +}; |
0 commit comments