cleanup, support loaders

This commit is contained in:
nvms 2024-11-14 02:57:40 -05:00
parent 0a67f3bc40
commit d35e137872
5 changed files with 47 additions and 47 deletions

View File

@ -44,7 +44,10 @@ func (b *Builder) Build(buildOptions *api.BuildOptions) error {
if err := os.WriteFile(file.Path, file.Contents, os.ModePerm); err != nil {
return fmt.Errorf("esr :: failed to write file: %v", err)
}
fmt.Printf("esr :: wrote: %s\n", filepath.Join(b.Config.Outdir, filepath.Base(file.Path)))
// fmt.Printf("esr :: wrote: %s\n", filepath.Join(b.Config.Outdir, filepath.Base(file.Path)))
fmt.Printf("esr :: wrote: %s\n", filepath.Join(filepath.Dir(file.Path), filepath.Base(file.Path)))
// fmt.Printf("esr :: wrote: %s\n", file.Path)
if filepath.Ext(file.Path) == ".js" {
b.BuiltJS = append(b.BuiltJS, file.Path)

View File

@ -70,6 +70,14 @@ func CreateDefaultPackageJson() string {
}`
}
// InitProject initializes a new project in the current working directory.
// It ensures the directory is empty before proceeding. The function creates
// essential directories and files required for the project, including:
// - A 'public' directory with an 'index.html' file.
// - A 'src' directory with an 'index.ts' file.
// - Configuration files such as '.esr.yml', 'package.json', 'tsconfig.json',
// and '.prettierrc' with default settings.
// If any step fails, the function calls Die with an appropriate error message.
func InitProject() {
cwd, err := os.Getwd()
if err != nil {

View File

@ -79,6 +79,3 @@ func (l *LiveReload) Start() {
func (l *LiveReload) Reload() {
l.messages <- "reload"
}
// JsSnippet generates a JS snippet for including in the template.
// const JsSnippet = `<script>const source = new EventSource('/livereload'); source.onmessage = (e) => { if (e.data === 'reload') location.reload(); };</script>`

View File

@ -99,6 +99,7 @@ func ModeOptions(config *config.Config, mode ModeType, entryPoint, tempFilePath
fmt.Printf("Unknown JSX: %s, using default\n", config.JSX)
}
// Default loaders
var loaders = map[string]api.Loader{
".js": api.LoaderJS,
".mjs": api.LoaderJS,
@ -122,50 +123,39 @@ func ModeOptions(config *config.Config, mode ModeType, entryPoint, tempFilePath
".woff2": api.LoaderDataURL,
}
buildOptions.Loader = loaders
// Overwrite sane default loaders with ones from config
for ext, loader := range config.Loader {
switch loader {
case "file":
loaders[ext] = api.LoaderFile
case "dataurl":
loaders[ext] = api.LoaderDataURL
case "binary":
loaders[ext] = api.LoaderBinary
case "text":
loaders[ext] = api.LoaderText
case "js":
loaders[ext] = api.LoaderJS
case "jsx":
loaders[ext] = api.LoaderJSX
case "tsx":
loaders[ext] = api.LoaderTSX
case "ts":
loaders[ext] = api.LoaderTS
case "json":
loaders[ext] = api.LoaderJSON
case "css":
loaders[ext] = api.LoaderCSS
case "default":
loaders[ext] = api.LoaderDefault
// Add additional cases if necessary for other loaders
default:
fmt.Printf("Unknown loader: %s for extension %s, using none\n", loader, ext)
loaders[ext] = api.LoaderNone
}
}
// Apply loader
// buildOptions.Loader = make(map[string]api.Loader)
// for k, v := range config.Loader {
// switch v {
// case ".woff2":
// buildOptions.Loader[k] = api.LoaderDataURL
// case "file":
// buildOptions.Loader[k] = api.LoaderFile
// case "dataurl":
// buildOptions.Loader[k] = api.LoaderDataURL
// case "binary":
// buildOptions.Loader[k] = api.LoaderBinary
// case "base64":
// buildOptions.Loader[k] = api.LoaderBase64
// case "copy":
// buildOptions.Loader[k] = api.LoaderCopy
// case "text":
// buildOptions.Loader[k] = api.LoaderText
// case "js":
// buildOptions.Loader[k] = api.LoaderJS
// case "jsx":
// buildOptions.Loader[k] = api.LoaderJSX
// case "tsx":
// buildOptions.Loader[k] = api.LoaderTSX
// case "ts":
// buildOptions.Loader[k] = api.LoaderTS
// case "json":
// buildOptions.Loader[k] = api.LoaderJSON
// case "css":
// buildOptions.Loader[k] = api.LoaderCSS
// case "globalcss":
// buildOptions.Loader[k] = api.LoaderGlobalCSS
// case "localcss":
// buildOptions.Loader[k] = api.LoaderLocalCSS
// case "empty":
// buildOptions.Loader[k] = api.LoaderEmpty
// case "default":
// buildOptions.Loader[k] = api.LoaderDefault
// default:
// buildOptions.Loader[k] = api.LoaderNone
// }
// }
buildOptions.Loader = loaders
return &buildOptions
}

View File

@ -46,6 +46,8 @@ func (r *Runner) Run(entryPoint string) {
return
}
fmt.Printf("esr :: running: \"%s %s\"\n", r.Config.Run.Runtime, r.TempFilePath)
cmd := exec.Command(r.Config.Run.Runtime, r.TempFilePath)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout