Skip to content

Commit

Permalink
feat: use 'main' from plugin.yml to specify typescript entrypoint (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
connerdouglass authored Jun 28, 2024
1 parent ccd3e69 commit 9e540c0
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"os"
"path/filepath"
"strings"
"time"

"github.com/customrealms/cli/internal/minecraft"
Expand All @@ -24,37 +25,50 @@ type BuildAction struct {
}

func (a *BuildAction) Run(ctx context.Context) error {

// Create the temp directory for the code output from Webpack.
// The code will be put into "bundle.js" in that directory
webpackOutputDir := filepath.Join(
os.TempDir(),
fmt.Sprintf("cr-build-%d-%d", time.Now().Unix(), rand.Uint32()),
)
if err := os.MkdirAll(webpackOutputDir, 0777); err != nil {
return err
return fmt.Errorf("creating webpack output dir: %w", err)
}
defer os.RemoveAll(webpackOutputDir)

// Write the webpack configuration file temporarily
webpackConfigFile := filepath.Join(webpackOutputDir, "webpack.config.js")
if err := os.WriteFile(webpackConfigFile, []byte(webpackConfig), 0777); err != nil {
return err
return fmt.Errorf("write webpack config file: %w", err)
}

// Parse the plugin.yml file
pluginYML, err := a.Project.PluginYML()
if err != nil {
return fmt.Errorf("parse plugin.yml: %w", err)
}

fmt.Println("============================================================")
fmt.Println("Bundling JavaScript code using Webpack")
fmt.Println("============================================================")

// Determine the entrypoint for the TypeScript project
var entrypoint string
if pluginYML != nil && strings.HasSuffix(pluginYML.Main, ".ts") {
entrypoint = pluginYML.Main
} else {
entrypoint = "./src/main.ts"
}

// Build the local directory
err := a.Project.Exec(ctx, "npx", "webpack-cli",
err = a.Project.Exec(ctx, "npx", "webpack-cli",
"--mode=production",
"-o", webpackOutputDir,
"-c", webpackConfigFile,
"--entry", "./src/main.ts",
"--entry", entrypoint,
)
if err != nil {
return err
return fmt.Errorf("run webpack: %w", err)
}

fmt.Println()
Expand Down

0 comments on commit 9e540c0

Please sign in to comment.