Skip to content

Commit

Permalink
fix(zbpack): implement static provider generate static files
Browse files Browse the repository at this point in the history
Signed-off-by: hackerchai <[email protected]>
  • Loading branch information
hackerchai committed Oct 29, 2023
1 parent 24e5915 commit 1d0c7bf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
bin
.DS_Store
tests/*/.zeabur
15 changes: 11 additions & 4 deletions internal/static/TransformServerless.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
)

// TransformServerless copies the static files from output to .zeabur/output/static and creates a config.json file for SPA
func TransformServerless(image, workdir string, meta types.PlanMeta) error {
err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
func TransformServerless(image, workdir string, meta types.PlanMeta, planType types.PlanType) error {
if planType == types.PlanTypeStatic {
err := utils.CopyFromImage(image, "/usr/share/nginx/html/static"+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
}
} else {
err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
}
}

config := types.ZeaburOutputConfig{Containerized: false, Routes: make([]types.ZeaburOutputConfigRoute, 0)}
Expand Down
18 changes: 17 additions & 1 deletion pkg/zeaburpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func Build(opt *BuildOptions) error {
}

_ = os.RemoveAll(".zeabur")
if wd != *opt.Path {
_ = os.RemoveAll(*opt.Path + "/.zeabur")
}

if t == types.PlanTypeNodejs && m["framework"] == string(types.NodeProjectFrameworkNextJs) && m["serverless"] == "true" {
println("Transforming build output to serverless format ...")
Expand All @@ -204,7 +207,17 @@ func Build(opt *BuildOptions) error {

if t == types.PlanTypeNodejs && m["outputDir"] != "" {
println("Transforming build output to serverless format ...")
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m)
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, t)
if err != nil {
println("Failed to transform serverless: " + err.Error())
handleBuildFailed(err)
return err
}
}

if t == types.PlanTypeStatic {
println("Transforming build output to serverless format ...")
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, t)
if err != nil {
println("Failed to transform serverless: " + err.Error())
handleBuildFailed(err)
Expand All @@ -217,6 +230,9 @@ func Build(opt *BuildOptions) error {
handleLog("\033[90m" + "To run the image, use the following command:" + "\033[0m")
if t == types.PlanTypeNodejs && m["outputDir"] != "" {
handleLog("npx serve .zeabur/output/static")
} else if t == types.PlanTypeStatic {
handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage)
handleLog("or you can find the static files in .zeabur/output/static")
} else {
handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/static-raw-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>

0 comments on commit 1d0c7bf

Please sign in to comment.