Skip to content

Commit

Permalink
Merge pull request #190 from zeabur/yuanlin/zea-2436-support-remixjs-…
Browse files Browse the repository at this point in the history
…serverless-deployment

feat: Remix serverless support
  • Loading branch information
Yuanlin Lin authored Jan 18, 2024
2 parents b3a02c4 + fe3852b commit 768fa2e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ func getServerless(ctx *nodePlanContext) bool {
types.NodeProjectFrameworkNuxtJs: true,
types.NodeProjectFrameworkWaku: true,
types.NodeProjectFrameworkAngular: true,
types.NodeProjectFrameworkRemix: true,
}

if serverless, ok := defaultServerless[framework]; ok {
Expand Down
103 changes: 103 additions & 0 deletions internal/nodejs/remix/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Package remix is used to transform build output of Remix app to the serverless build output format of Zeabur
package remix

import (
"encoding/json"
"fmt"
"log"
"os"
"path"

uuid2 "github.com/google/uuid"
cp "github.com/otiai10/copy"
"github.com/zeabur/zbpack/pkg/types"
)

// TransformServerless will transform the build output of Remix app to the serverless build output format of Zeabur
func TransformServerless(workdir string) error {

// create a tmpDir to store the build output
uuid := uuid2.New().String()
tmpDir := path.Join(os.TempDir(), uuid)
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
log.Printf("remove tmp dir: %s\n", err)
}
}()

// /tmpDir/uuid/dist
remixBuildDir := path.Join(tmpDir, "build")

// /workDir/.zeabur/output
zeaburOutputDir := path.Join(workdir, ".zeabur/output")

fmt.Println("=> Copying build output from image")
err := cp.Copy(path.Join(os.TempDir(), "zbpack/buildkit"), path.Join(tmpDir))
if err != nil {
return err
}

fmt.Println("=> Copying static asset files")

err = os.MkdirAll(path.Join(zeaburOutputDir, "static"), 0755)
if err != nil {
return fmt.Errorf("create static dir: %w", err)
}

err = cp.Copy(path.Join(tmpDir, "public"), path.Join(zeaburOutputDir, "static"))
if err != nil {
return fmt.Errorf("copy static dir: %w", err)
}

fmt.Println("=> Copying remix build output")

err = os.MkdirAll(path.Join(zeaburOutputDir, "functions"), 0755)
if err != nil {
return fmt.Errorf("create functions dir: %w", err)
}

_ = os.MkdirAll(path.Join(zeaburOutputDir, "functions/index.func"), 0755)
err = cp.Copy(remixBuildDir, path.Join(zeaburOutputDir, "functions/index.func/build"))
if err != nil {
return fmt.Errorf("copy waku's RSC function dir: %w", err)
}

fmt.Println("=> Copying node_modules")

err = cp.Copy(path.Join(remixBuildDir, "../node_modules"), path.Join(zeaburOutputDir, "functions/index.func/node_modules"))
if err != nil {
return fmt.Errorf("copy node_modules/waku dir: %w", err)
}

fmt.Println("=> Writing config.json ...")

config := types.ZeaburOutputConfig{Routes: []types.ZeaburOutputConfigRoute{{Src: ".*", Dest: "/"}}}

configBytes, err := json.Marshal(config)
if err != nil {
return err
}

err = os.WriteFile(path.Join(workdir, ".zeabur/output/config.json"), configBytes, 0644)
if err != nil {
return err
}

fmt.Println("=> Copying package.json ...")

err = cp.Copy(path.Join(tmpDir, "package.json"), path.Join(zeaburOutputDir, "functions/index.func/package.json"))
if err != nil {
return fmt.Errorf("copy package.json: %w", err)
}

indexjs := `import entry from './build/server-build-nodejs-eyJydW50aW1lIjoibm9kZWpzIn0.mjs';
export default entry;`

err = os.WriteFile(path.Join(zeaburOutputDir, "functions/index.func/index.mjs"), []byte(indexjs), 0644)
if err != nil {
return fmt.Errorf("write index.mjs: %w", err)
}

return nil
}
11 changes: 11 additions & 0 deletions pkg/zeaburpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/afero"
"github.com/zeabur/zbpack/internal/nodejs/nextjs"
"github.com/zeabur/zbpack/internal/nodejs/nuxtjs"
"github.com/zeabur/zbpack/internal/nodejs/remix"
"github.com/zeabur/zbpack/internal/nodejs/waku"
"github.com/zeabur/zbpack/internal/static"
"github.com/zeabur/zbpack/pkg/plan"
Expand Down Expand Up @@ -339,6 +340,16 @@ func Build(opt *BuildOptions) error {
}
}

if t == types.PlanTypeNodejs && m["framework"] == string(types.NodeProjectFrameworkRemix) && m["serverless"] == "true" {
println("Transforming build output to serverless format ...")
err = remix.TransformServerless(*opt.Path)
if err != nil {
log.Println("Failed to transform serverless: " + err.Error())
handleBuildFailed(err)
return err
}
}

if t == types.PlanTypeNodejs && m["framework"] == string(types.NodeProjectFrameworkNuxtJs) && m["serverless"] == "true" {
println("Transforming build output to serverless format ...")
err = nuxtjs.TransformServerless(*opt.Path)
Expand Down

0 comments on commit 768fa2e

Please sign in to comment.