Skip to content

Commit 793f04f

Browse files
jimpercopybara-github
authored andcommitted
Transition project from CommonJS to ES Modules (ESM) and upgrade the Eleventy dependency to v3.1.6
PiperOrigin-RevId: 955281585
1 parent ea60530 commit 793f04f

21 files changed

Lines changed: 758 additions & 1587 deletions

.eleventy.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import {UserConfig} from '@11ty/eleventy';
1+
import type UserConfig from '@11ty/eleventy/UserConfig';
22
import fs from 'fs';
33
import path from 'path';
4+
import {fileURLToPath} from 'url';
45

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));
1013

1114
// Defines the output formats 11ty will perform post-processing on.
1215
const POST_PROCESS_OUTPUT_FORMATS = ['js', 'legacyjs'];
1316

14-
module.exports = (eleventyConfig: UserConfig) => {
17+
export default (eleventyConfig: UserConfig) => {
18+
eleventyConfig.configureErrorReporting({ allowMissingExtensions: true });
19+
1520
// Ignore TS templates, they're compiled to JS as part of the build process.
1621
eleventyConfig.ignores!.add('samples/*.11ty*.ts');
1722

@@ -44,32 +49,36 @@ module.exports = (eleventyConfig: UserConfig) => {
4449
eleventyConfig.on('eleventy.after', async () => {
4550
const distPath = path.join(__dirname, 'dist');
4651

47-
fs.readdirSync(distPath, {withFileTypes: true})
52+
const samples = fs.readdirSync(distPath, {withFileTypes: true})
4853
.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+
}
7382
});
7483

7584
return {

0 commit comments

Comments
 (0)