mirror of
https://github.com/nvms/esr.git
synced 2025-12-15 22:40:52 +00:00
172 lines
4.8 KiB
Go
172 lines
4.8 KiB
Go
package esr
|
|
|
|
import (
|
|
"esr/internal/config"
|
|
"esr/internal/esr/plugins"
|
|
"fmt"
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
|
)
|
|
|
|
type ModeType int
|
|
|
|
const (
|
|
ModeBuilder ModeType = iota
|
|
ModeServer
|
|
ModeRunner
|
|
)
|
|
|
|
func ModeOptions(config *config.Config, mode ModeType, entryPoint, tempFilePath string) *api.BuildOptions {
|
|
buildOptions := api.BuildOptions{
|
|
EntryPoints: []string{entryPoint},
|
|
Plugins: []api.Plugin{plugins.EnvPlugin},
|
|
}
|
|
|
|
// Apply common settings based on the mode
|
|
getSourceMap := func(sourcemap bool) api.SourceMap {
|
|
if sourcemap {
|
|
return api.SourceMapLinked
|
|
}
|
|
return api.SourceMapNone
|
|
}
|
|
|
|
switch mode {
|
|
case ModeBuilder:
|
|
buildOptions.Bundle = config.Build.Bundle
|
|
buildOptions.Outdir = config.Build.Outdir
|
|
buildOptions.MinifySyntax = config.Build.Minify
|
|
buildOptions.MinifyWhitespace = config.Build.MinifyWhitespace
|
|
buildOptions.MinifyIdentifiers = config.Build.MinifyIdentifiers
|
|
buildOptions.JSXFactory = config.Build.JSXFactory
|
|
buildOptions.JSXImportSource = config.Build.JSXImportSource
|
|
buildOptions.Sourcemap = getSourceMap(config.Build.Sourcemap)
|
|
buildOptions.External = config.Build.External
|
|
case ModeServer:
|
|
buildOptions.Bundle = config.Serve.Bundle
|
|
buildOptions.Outdir = config.Serve.Outdir
|
|
buildOptions.MinifySyntax = config.Serve.Minify
|
|
buildOptions.MinifyWhitespace = config.Serve.MinifyWhitespace
|
|
buildOptions.MinifyIdentifiers = config.Serve.MinifyIdentifiers
|
|
buildOptions.JSXFactory = config.Serve.JSXFactory
|
|
buildOptions.JSXImportSource = config.Serve.JSXImportSource
|
|
buildOptions.Sourcemap = getSourceMap(config.Serve.Sourcemap)
|
|
buildOptions.External = config.Serve.External
|
|
case ModeRunner:
|
|
buildOptions.Bundle = true
|
|
buildOptions.Format = api.FormatESModule
|
|
buildOptions.Platform = api.PlatformNode
|
|
buildOptions.Outfile = tempFilePath
|
|
buildOptions.Outdir = ""
|
|
buildOptions.Sourcemap = getSourceMap(config.Run.Sourcemap)
|
|
buildOptions.External = config.Run.External
|
|
}
|
|
|
|
// Apply format
|
|
switch config.Format {
|
|
case "esm":
|
|
buildOptions.Format = api.FormatESModule
|
|
case "cjs":
|
|
buildOptions.Format = api.FormatCommonJS
|
|
case "iife":
|
|
buildOptions.Format = api.FormatIIFE
|
|
default:
|
|
buildOptions.Format = api.FormatDefault
|
|
fmt.Printf("Unknown format: %s, using default\n", config.Format)
|
|
}
|
|
|
|
// Apply platform
|
|
switch config.Platform {
|
|
case "browser":
|
|
buildOptions.Platform = api.PlatformBrowser
|
|
case "node":
|
|
buildOptions.Platform = api.PlatformNode
|
|
case "neutral":
|
|
buildOptions.Platform = api.PlatformNeutral
|
|
default:
|
|
buildOptions.Platform = api.PlatformDefault
|
|
fmt.Printf("Unknown platform: %s, using default\n", config.Platform)
|
|
}
|
|
|
|
switch config.JSX {
|
|
case "preserve":
|
|
buildOptions.JSX = api.JSXPreserve
|
|
case "transform":
|
|
buildOptions.JSX = api.JSXTransform
|
|
case "automatic":
|
|
buildOptions.JSX = api.JSXAutomatic
|
|
default:
|
|
buildOptions.JSX = api.JSXPreserve
|
|
fmt.Printf("Unknown JSX: %s, using default\n", config.JSX)
|
|
}
|
|
|
|
var loaders = map[string]api.Loader{
|
|
".js": api.LoaderJS,
|
|
".mjs": api.LoaderJS,
|
|
".cjs": api.LoaderJS,
|
|
".jsx": api.LoaderJSX,
|
|
".ts": api.LoaderTS,
|
|
".tsx": api.LoaderTSX,
|
|
".mts": api.LoaderTS,
|
|
".css": api.LoaderCSS,
|
|
".json": api.LoaderJSON,
|
|
".txt": api.LoaderText,
|
|
".html": api.LoaderText,
|
|
".md": api.LoaderText,
|
|
".svg": api.LoaderDataURL,
|
|
".png": api.LoaderDataURL,
|
|
".webp": api.LoaderDataURL,
|
|
".gif": api.LoaderDataURL,
|
|
".ttf": api.LoaderDataURL,
|
|
".eot": api.LoaderDataURL,
|
|
".woff": api.LoaderDataURL,
|
|
".woff2": api.LoaderDataURL,
|
|
}
|
|
|
|
buildOptions.Loader = loaders
|
|
|
|
// 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
|
|
// }
|
|
// }
|
|
|
|
return &buildOptions
|
|
}
|