mirror of
https://github.com/nvms/esr.git
synced 2025-12-15 14:30:53 +00:00
yep
This commit is contained in:
parent
a666de5a57
commit
81b941558c
23
README.md
23
README.md
@ -18,8 +18,8 @@ This is a simple esbuild-powered dev server. It has livereloading. It's fast. It
|
||||
```yml
|
||||
# Defaults
|
||||
bundle: true
|
||||
platform: browser
|
||||
format: esm
|
||||
platform: browser # browser | node
|
||||
format: esm # esm | cjs | iife
|
||||
outdir: public
|
||||
minify: false
|
||||
minifyWhitespace: false
|
||||
@ -29,7 +29,7 @@ sourcemap: true
|
||||
# Optional. Applies to `--serve`, or `--run --watch`.
|
||||
# Files that match these paths trigger reload or rerun when they are changed.
|
||||
watch:
|
||||
paths: ['src/**/*.{ts,tsx}', 'public/index.html']
|
||||
paths: ['src/**/*.{ts,tsx,css}', 'public/index.html']
|
||||
|
||||
# Each mode inherits from the configuration above, but can
|
||||
# override certain options as needed.
|
||||
@ -46,7 +46,7 @@ build:
|
||||
|
||||
run:
|
||||
# For example: <runtime> <outfile>
|
||||
runtime: bun
|
||||
runtime: bun # node | bun | ??
|
||||
|
||||
jsx: automatic
|
||||
jsxFactory: React.createElement
|
||||
@ -58,22 +58,23 @@ loader:
|
||||
.ts: tsx
|
||||
```
|
||||
|
||||
When serving an index.html, you can use `{{ livereload }}` to inject the livereload EventSource logic, and `{{ js }}` to inject a script tag for the built output file.
|
||||
When serving an index.html, you can use `{{ livereload }}` to inject the livereload EventSource logic, and `{{ js }}` and `{{ css }}` to inject tags for built assets.
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Application</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Application</title>
|
||||
{{ css }}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
{{ livereload }}
|
||||
{{ js }}
|
||||
<div id="app"></div>
|
||||
{{ livereload }}
|
||||
{{ js }}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
@ -12,7 +12,8 @@ import (
|
||||
type Builder struct {
|
||||
Config *config.Config
|
||||
EntryPoint string
|
||||
BuiltFiles []string
|
||||
BuiltJS []string
|
||||
BuiltCSS []string
|
||||
}
|
||||
|
||||
func NewBuilder(c *config.Config, e string) *Builder {
|
||||
@ -28,7 +29,8 @@ func (b *Builder) Build(buildOptions *api.BuildOptions) error {
|
||||
return fmt.Errorf("esr :: build failed: %v", result.Errors)
|
||||
}
|
||||
|
||||
b.BuiltFiles = []string{}
|
||||
b.BuiltJS = []string{}
|
||||
b.BuiltCSS = []string{}
|
||||
|
||||
for _, file := range result.OutputFiles {
|
||||
if filepath.Ext(file.Path) != ".map" {
|
||||
@ -45,7 +47,11 @@ func (b *Builder) Build(buildOptions *api.BuildOptions) error {
|
||||
fmt.Printf("esr :: wrote: %s\n", filepath.Join(b.Config.Outdir, filepath.Base(file.Path)))
|
||||
|
||||
if filepath.Ext(file.Path) == ".js" {
|
||||
b.BuiltFiles = append(b.BuiltFiles, file.Path)
|
||||
b.BuiltJS = append(b.BuiltJS, file.Path)
|
||||
}
|
||||
|
||||
if filepath.Ext(file.Path) == ".css" {
|
||||
b.BuiltCSS = append(b.BuiltCSS, file.Path)
|
||||
}
|
||||
} else {
|
||||
if err := os.WriteFile(file.Path, file.Contents, os.ModePerm); err != nil {
|
||||
|
||||
@ -84,8 +84,21 @@ func (s *Server) BuildIndex() (string, error) {
|
||||
"js": func() template.HTML {
|
||||
var scripts strings.Builder
|
||||
if s.Builder != nil {
|
||||
for _, file := range s.Builder.BuiltFiles {
|
||||
fmt.Fprintf(&scripts, "<script src=\"/%s\"></script>", filepath.Base(file))
|
||||
for _, file := range s.Builder.BuiltJS {
|
||||
if s.Config.Format == "esm" {
|
||||
fmt.Fprintf(&scripts, "<script src=\"/%s\" type=\"module\"></script>", filepath.Base(file))
|
||||
} else {
|
||||
fmt.Fprintf(&scripts, "<script src=\"/%s\"></script>", filepath.Base(file))
|
||||
}
|
||||
}
|
||||
}
|
||||
return template.HTML(scripts.String())
|
||||
},
|
||||
"css": func() template.HTML {
|
||||
var scripts strings.Builder
|
||||
if s.Builder != nil {
|
||||
for _, file := range s.Builder.BuiltCSS {
|
||||
fmt.Fprintf(&scripts, "<link rel=\"stylesheet\" type=\"text/css\" href=\"/%s\"></script>", filepath.Base(file))
|
||||
}
|
||||
}
|
||||
return template.HTML(scripts.String())
|
||||
|
||||
@ -15,4 +15,4 @@ package livereload
|
||||
// console.info('esr: livereload enabled');
|
||||
// </script>
|
||||
// `
|
||||
const JsSnippet = `<script>const source = new EventSource('/livereload'); source.onmessage = (e) => { if (e.data === 'reload') location.reload(); }; console.info('esr :: livereload listening');</script>`
|
||||
const JsSnippet = `<script>const source = new EventSource('/livereload'); source.onmessage = (e) => { if (e.data === 'reload') location.reload(); }; console.info('esr :: listening');</script>`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user