@@ -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 : / ^ .* \. c s s $ / } , ( { path, importer, resolveDir, kind} ) => {
24+ return { path, namespace : 'css' , pluginData : { importer, resolveDir, kind} }
25+ } )
26+ plugin . onLoad ( { filter : / ^ .* \. c s s $ / , 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 )
0 commit comments