@@ -4,34 +4,83 @@ const headers = require("markdown-it-github-headings")
44const js = require ( "@jamshop/eleventy-plugin-esbuild" )
55const glob = require ( "glob" )
66const path = require ( "node:path" )
7- const highlight = require ( "@11ty/eleventy-plugin-syntaxhighlight " )
7+ const fs = require ( "node:fs/promises " )
88const rss = require ( "@11ty/eleventy-plugin-rss" )
99const dedent = require ( "dedent" )
10+ const { build } = require ( "esbuild" )
11+
12+ const buildJS = ( config = { } ) => {
13+ return build ( {
14+ minify : process . NODE_ENV === "development" ? false : true ,
15+ bundle : true ,
16+ splitting : true ,
17+ write : true ,
18+ format : "esm" ,
19+ metafile : true ,
20+ outdir : "_site/script" ,
21+ plugins : [
22+ {
23+ name : "css" ,
24+ setup : ( plugin ) => {
25+ plugin . onResolve ( { filter : / ^ .* \. c s s $ / } , ( { path, importer, resolveDir, kind } ) => {
26+ return {
27+ path,
28+ namespace : "css" ,
29+ pluginData : { importer, resolveDir, kind } ,
30+ }
31+ } )
32+ plugin . onLoad ( { filter : / ^ .* \. c s s $ / , namespace : "css" } , async ( ctx ) => {
33+ const { default : stringToTemplateLiteral } = await import ( "string-to-template-literal" )
34+ let contents = await fs . readFile ( path . resolve ( ctx . pluginData . resolveDir , ctx . path ) , "utf8" )
35+
36+ contents = `const c = new CSSStyleSheet(); c.replaceSync(${ stringToTemplateLiteral (
37+ contents
38+ ) } ); export default c;`
39+
40+ return { contents, resolveDir : ctx . pluginData . resolveDir }
41+ } )
42+ } ,
43+ } ,
44+ ] ,
45+ ...config ,
46+ } )
47+ }
1048
1149module . exports = ( eleventyConfig ) => {
1250 eleventyConfig . addPlugin ( rss )
1351 eleventyConfig . addPlugin ( css )
14- eleventyConfig . addPlugin ( js , {
15- entryPoints : Object . fromEntries ( glob . sync ( "script/*.[tj]s" ) . map ( ( e ) => [ path . basename ( e , path . extname ( e ) ) , e ] ) ) ,
16- output : "_site/script" ,
52+
53+ const entryPoints = glob . sync ( "script/*.[tj]s" )
54+ eleventyConfig . addWatchTarget ( "script/*.[tj]s" )
55+
56+ buildJS ( { entryPoints } )
57+
58+ eleventyConfig . on ( "beforeWatch" , ( changedFiles ) => {
59+ // Run me before --watch or --serve re-runs
60+ if ( changedFiles . some ( ( watchPath ) => watchPath . endsWith ( ".css" ) || entryPoints . includes ( watchPath ) ) ) {
61+ buildJS ( { entryPoints } )
62+ }
1763 } )
18- eleventyConfig . addPlugin ( highlight )
1964
2065 eleventyConfig . addFilter ( "iso8601" , rss . dateToRfc3339 )
2166 eleventyConfig . addFilter ( "date_to_rfc3339" , rss . dateToRfc3339 )
2267 eleventyConfig . addFilter ( "date_to_rfc822" , rss . dateToRfc822 )
2368 eleventyConfig . addFilter ( "html_to_absolute_urls" , rss . convertHtmlToAbsoluteUrls )
2469 eleventyConfig . addFilter ( "domain" , ( str ) => new URL ( str ) . hostname )
2570
26- eleventyConfig . setLibrary (
27- "md" ,
28- markdown ( {
29- html : true ,
30- linkify : true ,
31- } )
32- . use ( headers , { prefixHeadingIds : false } )
33- . disable ( "code" )
34- )
71+ const md = markdown ( {
72+ html : true ,
73+ linkify : true ,
74+ highlight : ( str , lang ) => {
75+ return `<pre class="code-interactive"><code-interactive lang="${ lang } ">${ md . utils . escapeHtml (
76+ str
77+ ) } </code-interactive></pre>`
78+ } ,
79+ } )
80+ . use ( headers , { prefixHeadingIds : false } )
81+ . disable ( "code" )
82+
83+ eleventyConfig . setLibrary ( "md" , md )
3584
3685 eleventyConfig . addPassthroughCopy ( "images" )
3786 eleventyConfig . addPassthroughCopy ( "browserconfig.xml" )
0 commit comments