MCPcopy Index your code
hub / github.com/esm-dev/esm.sh / ServeCSSModule

Method ServeCSSModule

web/handler.go:517–556  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request, query url.Values)

Source from the content-addressed store, hash-verified

515}
516
517func (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
558func (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"))

Callers 1

ServeHTTPMethod · 0.95

Calls 8

BuildMethod · 0.80
ErrorMethod · 0.80
WriteMethod · 0.80
HasMethod · 0.80
WriteHeaderMethod · 0.80
HeaderMethod · 0.80
SetMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected