Skip to content

Commit 0f20089

Browse files
koddssonkeithamus
andcommitted
Fix esbuild CSS plugin
Co-authored-by: Keith Cirkel <[email protected]>
1 parent 4a71f23 commit 0f20089

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

.eleventy.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ const buildJS = (config = {}) => {
1515
bundle: true,
1616
write: true,
1717
outdir: "_site/script",
18+
plugins: [
19+
{
20+
name: "css",
21+
setup: (plugin) => {
22+
console.log('==================>')
23+
plugin.onResolve({filter: /^.*\.css$/}, ({path, importer, resolveDir, kind}) => {
24+
return {path, namespace: 'css', pluginData: {importer, resolveDir, kind}}
25+
})
26+
plugin.onLoad({filter: /^.*\.css$/, namespace: 'css'}, async (ctx) => {
27+
const {default: stringToTemplateLiteral} = await import('string-to-template-literal')
28+
let contents = await fs.readFile(path.resolve(ctx.pluginData.resolveDir, ctx.path), 'utf8')
29+
30+
contents = `const c = new CSSStyleSheet(); c.replaceSync(${stringToTemplateLiteral(contents)}); export default c;`
31+
32+
return {contents, resolveDir: ctx.pluginData.resolveDir}
33+
})
34+
}
35+
}
36+
],
1837
...config,
1938
})
2039
}
@@ -25,40 +44,16 @@ module.exports = (eleventyConfig) => {
2544

2645
const entryPoints = glob.sync("script/*.[tj]s")
2746
eleventyConfig.addWatchTarget("script/*.[tj]s")
28-
47+
2948
buildJS({ entryPoints })
3049

3150
eleventyConfig.on("beforeWatch", (changedFiles) => {
3251
// Run me before --watch or --serve re-runs
33-
if (entryPoints.some((watchPath) => changedFiles.includes(watchPath))) {
52+
if (changedFiles.some((watchPath) => watchPath.endsWith('.css') || entryPoints.includes(watchPath))) {
3453
buildJS({ entryPoints })
3554
}
3655
})
3756

38-
// eleventyConfig.addPlugin(js, {
39-
// entryPoints: glob.sync("script/*.[tj]s"),
40-
// outDir: "_site/script",
41-
// esbuild: {
42-
// plugins: [
43-
// {
44-
// name: "css",
45-
// setup: (plugin) => {
46-
// console.log('==================>')
47-
// plugin.onResolve({filter: /^.*\.css$/}, (ctx) => Object.assign(ctx, {namespace: 'css'}))
48-
// plugin.onLoad({filter: /^.*\.css$/, namespace: 'css'}, async (ctx) => {
49-
// let contents = await fs.readFileSync(path.resolve(ctx.resolveDir, ctx.filePath), 'utf8')
50-
51-
// contents = `const c = new CSSStyleSheet(); c.replaceSync("${contents}"); export default c;`
52-
53-
// return {contents, resolveDir: ctx.resolveDir}
54-
// })
55-
// }
56-
// }
57-
// ],
58-
// minify: false
59-
// }
60-
// })
61-
6257
eleventyConfig.addFilter("iso8601", rss.dateToRfc3339)
6358
eleventyConfig.addFilter("date_to_rfc3339", rss.dateToRfc3339)
6459
eleventyConfig.addFilter("date_to_rfc822", rss.dateToRfc822)

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"retext-sentence-spacing": "^5.2.0",
125125
"retext-simplify": "^7.2.0",
126126
"retext-spell": "^5.3.0",
127+
"string-to-template-literal": "^2.0.0",
127128
"stylelint": "^14.16.0",
128129
"stylelint-config-standard": "^29.0.0",
129130
"stylelint-media-use-custom-media": "^2.0.1",

0 commit comments

Comments
 (0)