|
1 | 1 | import { load as yamlLoad } from 'js-yaml'; |
2 | 2 | import pluginRss from '@11ty/eleventy-plugin-rss'; |
3 | 3 | import { bundle as lightningcssBundle, browserslistToTargets, Features } from 'lightningcss'; |
| 4 | +import * as esbuild from 'esbuild'; |
4 | 5 | import Image from '@11ty/eleventy-img'; |
5 | 6 | import { glob } from 'glob'; |
6 | | -import { cpSync } from 'node:fs'; |
| 7 | +import { cpSync, rmSync, mkdirSync, writeFileSync } from 'node:fs'; |
7 | 8 | import os from 'node:os'; |
| 9 | +import path from 'node:path'; |
| 10 | +import * as pagefind from 'pagefind'; |
8 | 11 | import MarkdownIt from 'markdown-it'; |
9 | 12 |
|
10 | 13 | import packageJson from './package.json' with { type: 'json' }; |
@@ -141,6 +144,32 @@ export default (config) => { |
141 | 144 | return code; |
142 | 145 | }); |
143 | 146 |
|
| 147 | + // JavaScript |
| 148 | + |
| 149 | + config.addTemplateFormats('js'); |
| 150 | + |
| 151 | + config.addExtension('js', { |
| 152 | + outputFileExtension: 'js', |
| 153 | + compile: async (content, path) => { |
| 154 | + if (path !== './src/scripts/index.js') { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + return async () => { |
| 159 | + let { outputFiles } = await esbuild.build({ |
| 160 | + target: 'es2020', |
| 161 | + entryPoints: [path], |
| 162 | + minify: true, |
| 163 | + bundle: true, |
| 164 | + external: ['/pagefind/pagefind.js'], |
| 165 | + write: false, |
| 166 | + }); |
| 167 | + |
| 168 | + return outputFiles[0].text; |
| 169 | + }; |
| 170 | + }, |
| 171 | + }); |
| 172 | + |
144 | 173 | // Covers |
145 | 174 |
|
146 | 175 | const imagesCache = '.cache/@11ty/_images'; |
@@ -187,8 +216,31 @@ export default (config) => { |
187 | 216 | ); |
188 | 217 | }); |
189 | 218 |
|
| 219 | + // Search |
| 220 | + |
190 | 221 | config.on('eleventy.after', async () => { |
191 | 222 | cpSync(imagesCache, 'dist', { recursive: true }); |
| 223 | + |
| 224 | + const { index } = await pagefind.createIndex(); |
| 225 | + await index.addDirectory({ path: 'dist' }); |
| 226 | + rmSync('dist/pagefind', { recursive: true, force: true }); |
| 227 | + |
| 228 | + const { files } = await index.getFiles(); |
| 229 | + const ignoredPaths = [ |
| 230 | + 'pagefind-ui', |
| 231 | + 'pagefind-modular-ui', |
| 232 | + 'translations/', |
| 233 | + ]; |
| 234 | + |
| 235 | + const filteredFiles = files.filter((file) => { |
| 236 | + return !ignoredPaths.some((pattern) => file.path.includes(pattern)); |
| 237 | + }); |
| 238 | + |
| 239 | + filteredFiles.forEach((file) => { |
| 240 | + const outputPath = path.join('dist/pagefind', file.path); |
| 241 | + mkdirSync(path.dirname(outputPath), { recursive: true }); |
| 242 | + writeFileSync(outputPath, file.content); |
| 243 | + }); |
192 | 244 | }); |
193 | 245 |
|
194 | 246 | // Dates |
|
0 commit comments