|
1 | | -import {UserConfig} from '@11ty/eleventy'; |
| 1 | +import type UserConfig from '@11ty/eleventy/UserConfig'; |
2 | 2 | import fs from 'fs'; |
3 | 3 | import path from 'path'; |
| 4 | +import {fileURLToPath} from 'url'; |
4 | 5 |
|
5 | | -import {TypeScriptExension} from './src/extension/typescript'; |
6 | | -import {formatCode} from './src/transform/format-code'; |
7 | | -import {removeContentMarkers} from './src/transform/remove-content-markers'; |
8 | | -import {removeHeaders} from './src/transform/remove-headers'; |
9 | | -import {parseHeadAndBodyJS} from './src/util/js-utils'; |
| 6 | +import {TypeScriptExension} from './src/extension/typescript.js'; |
| 7 | +import {formatCode} from './src/transform/format-code.js'; |
| 8 | +import {removeContentMarkers} from './src/transform/remove-content-markers.js'; |
| 9 | +import {removeHeaders} from './src/transform/remove-headers.js'; |
| 10 | +import {parseHeadAndBodyJS} from './src/util/js-utils.js'; |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
10 | 13 |
|
11 | 14 | // Defines the output formats 11ty will perform post-processing on. |
12 | 15 | const POST_PROCESS_OUTPUT_FORMATS = ['js', 'legacyjs']; |
13 | 16 |
|
14 | | -module.exports = (eleventyConfig: UserConfig) => { |
| 17 | +export default (eleventyConfig: UserConfig) => { |
| 18 | + eleventyConfig.configureErrorReporting({ allowMissingExtensions: true }); |
| 19 | + |
15 | 20 | // Ignore TS templates, they're compiled to JS as part of the build process. |
16 | 21 | eleventyConfig.ignores!.add('samples/*.11ty*.ts'); |
17 | 22 |
|
@@ -44,32 +49,36 @@ module.exports = (eleventyConfig: UserConfig) => { |
44 | 49 | eleventyConfig.on('eleventy.after', async () => { |
45 | 50 | const distPath = path.join(__dirname, 'dist'); |
46 | 51 |
|
47 | | - fs.readdirSync(distPath, {withFileTypes: true}) |
| 52 | + const samples = fs.readdirSync(distPath, {withFileTypes: true}) |
48 | 53 | .filter((f) => f.isDirectory()) |
49 | | - .map(({name: sample}: fs.Dirent) => { |
50 | | - POST_PROCESS_OUTPUT_FORMATS.forEach(async (format) => { |
51 | | - const jsPath = path.join(distPath, sample, format, 'demo.js'); |
52 | | - const parsedJs = |
53 | | - await parseHeadAndBodyJS(fs.createReadStream(jsPath)); |
54 | | - |
55 | | - const htmlPath = path.join(distPath, sample, format, 'demo.html'); |
56 | | - const htmlContent = |
57 | | - fs.readFileSync(htmlPath) |
58 | | - .toString() |
59 | | - // Replace <script> tag with the parsed head JS. |
60 | | - .replace( |
61 | | - /<script.*?src="\/sample.ts"><\/script>/gm, |
62 | | - `${parsedJs.head}`) |
63 | | - // Append parsed body JS to the <body> tag. |
64 | | - .replace(/<\/body>/gm, `${parsedJs.body}</body>`) |
65 | | - // If there are now 2 consecutive script tags in the body, |
66 | | - // combine them into one. |
67 | | - .replace(/(<body>.*)<\/script>\s+<script>(.*)/s, '$1\n$2'); |
68 | | - |
69 | | - fs.writeFileSync(htmlPath, await formatCode(htmlContent, htmlPath)); |
70 | | - fs.unlinkSync(jsPath); |
71 | | - }); |
72 | | - }); |
| 54 | + .map(({name: sample}: fs.Dirent) => sample); |
| 55 | + |
| 56 | + for (const sample of samples) { |
| 57 | + for (const format of POST_PROCESS_OUTPUT_FORMATS) { |
| 58 | + const jsPath = path.join(distPath, sample, format, 'demo.js'); |
| 59 | + if (!fs.existsSync(jsPath)) continue; |
| 60 | + |
| 61 | + const parsedJs = |
| 62 | + await parseHeadAndBodyJS(fs.createReadStream(jsPath)); |
| 63 | + |
| 64 | + const htmlPath = path.join(distPath, sample, format, 'demo.html'); |
| 65 | + const htmlContent = |
| 66 | + fs.readFileSync(htmlPath) |
| 67 | + .toString() |
| 68 | + // Replace <script> tag with the parsed head JS. |
| 69 | + .replace( |
| 70 | + /<script.*?src="\/sample.ts"><\/script>/gm, |
| 71 | + `${parsedJs.head}`) |
| 72 | + // Append parsed body JS to the <body> tag. |
| 73 | + .replace(/<\/body>/gm, `${parsedJs.body}</body>`) |
| 74 | + // If there are now 2 consecutive script tags in the body, |
| 75 | + // combine them into one. |
| 76 | + .replace(/(<body>.*)<\/script>\s+<script>(.*)/s, '$1\n$2'); |
| 77 | + |
| 78 | + fs.writeFileSync(htmlPath, await formatCode(htmlContent, htmlPath)); |
| 79 | + fs.unlinkSync(jsPath); |
| 80 | + } |
| 81 | + } |
73 | 82 | }); |
74 | 83 |
|
75 | 84 | return { |
|
0 commit comments