| 515 | } |
| 516 | |
| 517 | func (s *Handler) ServeCSSModule(w http.ResponseWriter, r *http.Request, query url.Values) { |
| 518 | ret := esbuild.Build(esbuild.BuildOptions{ |
| 519 | EntryPoints: []string{filepath.Join(s.config.AppDir, r.URL.Path)}, |
| 520 | Write: false, |
| 521 | MinifyWhitespace: true, |
| 522 | MinifySyntax: true, |
| 523 | Target: esbuild.ES2022, |
| 524 | Bundle: true, |
| 525 | }) |
| 526 | if len(ret.Errors) > 0 { |
| 527 | fmt.Println(term.Red(ret.Errors[0].Text)) |
| 528 | http.Error(w, "Internal Server Error", 500) |
| 529 | return |
| 530 | } |
| 531 | css := bytes.TrimSpace(ret.OutputFiles[0].Contents) |
| 532 | xx := xxhash.New() |
| 533 | xx.Write(css) |
| 534 | etag := fmt.Sprintf("w/\"%x%s\"", xx.Sum(nil), s.etagSuffix) |
| 535 | if r.Header.Get("If-None-Match") == etag && !query.Has("t") { |
| 536 | w.WriteHeader(http.StatusNotModified) |
| 537 | return |
| 538 | } |
| 539 | header := w.Header() |
| 540 | header.Set("Content-Type", "application/javascript; charset=utf-8") |
| 541 | if !query.Has("t") { |
| 542 | header.Set("Cache-Control", "public, max-age=0, must-revalidate") |
| 543 | header.Set("Etag", etag) |
| 544 | } |
| 545 | w.Write([]byte("const css=\"")) |
| 546 | w.Write(bytes.ReplaceAll(css, []byte{'"'}, []byte{'\\', '"'})) |
| 547 | w.Write([]byte("\";let style,")) |
| 548 | w.Write([]byte(`applyCSS=css=>{(style??(style=document.head.appendChild(document.createElement("style")))).textContent=css};`)) |
| 549 | if s.config.Dev { |
| 550 | w.Write([]byte(`import createHot from"/@hmr";`)) |
| 551 | w.Write([]byte("const hot=createHot(import.meta.url);hot.accept(_=>applyCSS(_.default));!hot.locked&&applyCSS(css);")) |
| 552 | } else { |
| 553 | w.Write([]byte("applyCSS(css);")) |
| 554 | } |
| 555 | w.Write([]byte("export default css;")) |
| 556 | } |
| 557 | |
| 558 | func (s *Handler) ServeFrameworkCSS(w http.ResponseWriter, r *http.Request, query url.Values, framework string) { |
| 559 | indexHtmlFile, err := os.Open(filepath.Join(s.config.AppDir, "index.html")) |