yao/sui/core/compile.go
2023-10-01 17:20:30 +08:00

48 lines
1.2 KiB
Go

package core
import (
"regexp"
"github.com/evanw/esbuild/pkg/api"
"github.com/yaoapp/gou/runtime/transform"
)
// Compile the page
func (page *Page) Compile() {}
// CompileJS compile the javascript
func (page *Page) CompileJS(source []byte, minify bool) ([]byte, error) {
jsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
if minify {
minified, err := transform.MinifyJS(jsCode)
return []byte(minified), err
}
return []byte(jsCode), nil
}
// CompileTS compile the typescript
func (page *Page) CompileTS(source []byte, minify bool) ([]byte, error) {
tsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
if minify {
jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{
Target: api.ESNext,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
})
return []byte(jsCode), err
}
jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{Target: api.ESNext})
return []byte(jsCode), err
}
// CompileCSS compile the css
func (page *Page) CompileCSS(source []byte, minify bool) ([]byte, error) {
if minify {
cssCode, err := transform.MinifyCSS(string(source))
return []byte(cssCode), err
}
return source, nil
}