2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-06-07 21:24:22 +00:00
|
|
|
|
2023-08-24 22:02:42 +00:00
|
|
|
//go:build !plan9
|
|
|
|
|
2022-06-07 21:24:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-08-31 01:05:49 +00:00
|
|
|
"net"
|
2022-06-07 21:24:22 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2023-08-17 05:09:53 +00:00
|
|
|
"slices"
|
2022-06-07 21:24:22 +00:00
|
|
|
"strconv"
|
2022-07-26 00:51:06 +00:00
|
|
|
"time"
|
2022-06-07 21:24:22 +00:00
|
|
|
|
|
|
|
esbuild "github.com/evanw/esbuild/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
devMode = true
|
|
|
|
prodMode = false
|
|
|
|
)
|
|
|
|
|
|
|
|
// commonSetup performs setup that is common to both dev and build modes.
|
|
|
|
func commonSetup(dev bool) (*esbuild.BuildOptions, error) {
|
|
|
|
// Change cwd to to where this file lives -- that's where all inputs for
|
|
|
|
// esbuild and other build steps live.
|
2023-02-21 23:55:06 +00:00
|
|
|
root, err := findRepoRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 18:34:38 +00:00
|
|
|
if *yarnPath == "" {
|
|
|
|
*yarnPath = path.Join(root, "tool", "yarn")
|
|
|
|
}
|
2023-02-21 23:55:06 +00:00
|
|
|
tsConnectDir := filepath.Join(root, "cmd", "tsconnect")
|
|
|
|
if err := os.Chdir(tsConnectDir); err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot change cwd: %w", err)
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
2022-08-15 23:48:00 +00:00
|
|
|
if err := installJSDeps(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot install JS deps: %w", err)
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &esbuild.BuildOptions{
|
2022-08-24 01:05:23 +00:00
|
|
|
EntryPoints: []string{"src/app/index.ts", "src/app/index.css"},
|
2022-07-20 01:04:09 +00:00
|
|
|
Outdir: *distDir,
|
2022-06-07 21:24:22 +00:00
|
|
|
Bundle: true,
|
|
|
|
Sourcemap: esbuild.SourceMapLinked,
|
|
|
|
LogLevel: esbuild.LogLevelInfo,
|
|
|
|
Define: map[string]string{"DEBUG": strconv.FormatBool(dev)},
|
|
|
|
Target: esbuild.ES2017,
|
2022-08-15 23:48:00 +00:00
|
|
|
Plugins: []esbuild.Plugin{
|
|
|
|
{
|
|
|
|
Name: "tailscale-tailwind",
|
|
|
|
Setup: func(build esbuild.PluginBuild) {
|
|
|
|
setupEsbuildTailwind(build, dev)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "tailscale-go-wasm-exec-js",
|
|
|
|
Setup: setupEsbuildWasmExecJS,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "tailscale-wasm",
|
|
|
|
Setup: func(build esbuild.PluginBuild) {
|
|
|
|
setupEsbuildWasm(build, dev)
|
|
|
|
},
|
2022-07-26 00:51:06 +00:00
|
|
|
},
|
2022-08-15 23:48:00 +00:00
|
|
|
},
|
2023-09-28 23:46:04 +00:00
|
|
|
JSX: esbuild.JSXAutomatic,
|
2022-06-07 21:24:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 23:55:06 +00:00
|
|
|
func findRepoRoot() (string, error) {
|
2023-03-30 23:11:30 +00:00
|
|
|
if *rootDir != "" {
|
|
|
|
return *rootDir, nil
|
|
|
|
}
|
2023-02-21 23:55:06 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if _, err := os.Stat(path.Join(cwd, "go.mod")); err == nil {
|
|
|
|
return cwd, nil
|
|
|
|
}
|
|
|
|
if cwd == "/" {
|
|
|
|
return "", fmt.Errorf("Cannot find repo root")
|
|
|
|
}
|
|
|
|
cwd = path.Dir(cwd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 01:05:49 +00:00
|
|
|
func commonPkgSetup(dev bool) (*esbuild.BuildOptions, error) {
|
|
|
|
buildOptions, err := commonSetup(dev)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buildOptions.EntryPoints = []string{"src/pkg/pkg.ts", "src/pkg/pkg.css"}
|
|
|
|
buildOptions.Outdir = *pkgDir
|
|
|
|
buildOptions.Format = esbuild.FormatESModule
|
|
|
|
buildOptions.AssetNames = "[name]"
|
|
|
|
return buildOptions, nil
|
|
|
|
}
|
|
|
|
|
2022-08-24 01:05:23 +00:00
|
|
|
// cleanDir removes files from dirPath, except the ones specified by
|
|
|
|
// preserveFiles.
|
|
|
|
func cleanDir(dirPath string, preserveFiles ...string) error {
|
|
|
|
log.Printf("Cleaning %s...\n", dirPath)
|
|
|
|
files, err := os.ReadDir(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return os.MkdirAll(dirPath, 0755)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !slices.Contains(preserveFiles, file.Name()) {
|
|
|
|
if err := os.Remove(filepath.Join(dirPath, file.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 01:05:49 +00:00
|
|
|
func runEsbuildServe(buildOptions esbuild.BuildOptions) {
|
|
|
|
host, portStr, err := net.SplitHostPort(*addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot parse addr: %v", err)
|
|
|
|
}
|
|
|
|
port, err := strconv.ParseUint(portStr, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot parse port: %v", err)
|
|
|
|
}
|
2023-09-28 23:46:04 +00:00
|
|
|
buildContext, ctxErr := esbuild.Context(buildOptions)
|
|
|
|
if ctxErr != nil {
|
|
|
|
log.Fatalf("Cannot create esbuild context: %v", err)
|
|
|
|
}
|
|
|
|
result, err := buildContext.Serve(esbuild.ServeOptions{
|
2022-08-31 01:05:49 +00:00
|
|
|
Port: uint16(port),
|
|
|
|
Host: host,
|
|
|
|
Servedir: "./",
|
2023-09-28 23:46:04 +00:00
|
|
|
})
|
2022-08-31 01:05:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot start esbuild server: %v", err)
|
|
|
|
}
|
|
|
|
log.Printf("Listening on http://%s:%d\n", result.Host, result.Port)
|
2024-11-03 15:12:34 +00:00
|
|
|
select {}
|
2022-08-31 01:05:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 01:05:23 +00:00
|
|
|
func runEsbuild(buildOptions esbuild.BuildOptions) esbuild.BuildResult {
|
|
|
|
log.Printf("Running esbuild...\n")
|
|
|
|
result := esbuild.Build(buildOptions)
|
|
|
|
if len(result.Errors) > 0 {
|
|
|
|
log.Printf("ESBuild Error:\n")
|
|
|
|
for _, e := range result.Errors {
|
|
|
|
log.Printf("%v", e)
|
|
|
|
}
|
|
|
|
log.Fatal("Build failed")
|
|
|
|
}
|
|
|
|
if len(result.Warnings) > 0 {
|
|
|
|
log.Printf("ESBuild Warnings:\n")
|
|
|
|
for _, w := range result.Warnings {
|
|
|
|
log.Printf("%v", w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:48:00 +00:00
|
|
|
// setupEsbuildWasmExecJS generates an esbuild plugin that serves the current
|
|
|
|
// wasm_exec.js runtime helper library from the Go toolchain.
|
|
|
|
func setupEsbuildWasmExecJS(build esbuild.PluginBuild) {
|
|
|
|
wasmExecSrcPath := filepath.Join(runtime.GOROOT(), "misc", "wasm", "wasm_exec.js")
|
|
|
|
build.OnResolve(esbuild.OnResolveOptions{
|
|
|
|
Filter: "./wasm_exec$",
|
|
|
|
}, func(args esbuild.OnResolveArgs) (esbuild.OnResolveResult, error) {
|
|
|
|
return esbuild.OnResolveResult{Path: wasmExecSrcPath}, nil
|
|
|
|
})
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 23:48:00 +00:00
|
|
|
// setupEsbuildWasm generates an esbuild plugin that builds the Tailscale wasm
|
|
|
|
// binary and serves it as a file that the JS can load.
|
|
|
|
func setupEsbuildWasm(build esbuild.PluginBuild, dev bool) {
|
|
|
|
// Add a resolve hook to convince esbuild that the path exists.
|
|
|
|
build.OnResolve(esbuild.OnResolveOptions{
|
|
|
|
Filter: "./main.wasm$",
|
|
|
|
}, func(args esbuild.OnResolveArgs) (esbuild.OnResolveResult, error) {
|
|
|
|
return esbuild.OnResolveResult{
|
|
|
|
Path: "./src/main.wasm",
|
|
|
|
Namespace: "generated",
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
build.OnLoad(esbuild.OnLoadOptions{
|
|
|
|
Filter: "./src/main.wasm$",
|
|
|
|
}, func(args esbuild.OnLoadArgs) (esbuild.OnLoadResult, error) {
|
|
|
|
contents, err := buildWasm(dev)
|
|
|
|
if err != nil {
|
|
|
|
return esbuild.OnLoadResult{}, fmt.Errorf("Cannot build main.wasm: %w", err)
|
|
|
|
}
|
|
|
|
contentsStr := string(contents)
|
|
|
|
return esbuild.OnLoadResult{
|
|
|
|
Contents: &contentsStr,
|
|
|
|
Loader: esbuild.LoaderFile,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildWasm(dev bool) ([]byte, error) {
|
|
|
|
start := time.Now()
|
2022-09-15 12:06:59 +00:00
|
|
|
outputFile, err := os.CreateTemp("", "main.*.wasm")
|
2022-06-07 21:24:22 +00:00
|
|
|
if err != nil {
|
2022-08-15 23:48:00 +00:00
|
|
|
return nil, fmt.Errorf("Cannot create main.wasm output file: %w", err)
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
2022-08-15 23:48:00 +00:00
|
|
|
outputPath := outputFile.Name()
|
2022-09-26 22:37:27 +00:00
|
|
|
|
2022-08-15 23:48:00 +00:00
|
|
|
defer os.Remove(outputPath)
|
2022-09-26 22:37:27 +00:00
|
|
|
// Running defer (*os.File).Close() in defer order before os.Remove
|
|
|
|
// because on some systems like Windows, it is possible for os.Remove
|
|
|
|
// to fail for unclosed files.
|
|
|
|
defer outputFile.Close()
|
2022-06-07 21:24:22 +00:00
|
|
|
|
|
|
|
args := []string{"build", "-tags", "tailscale_go,osusergo,netgo,nethttpomithttp2,omitidna,omitpemdecrypt"}
|
|
|
|
if !dev {
|
2022-08-04 17:37:19 +00:00
|
|
|
if *devControl != "" {
|
2022-08-15 23:48:00 +00:00
|
|
|
return nil, fmt.Errorf("Development control URL can only be used in dev mode.")
|
2022-08-04 17:37:19 +00:00
|
|
|
}
|
2022-06-07 21:24:22 +00:00
|
|
|
// Omit long paths and debug symbols in release builds, to reduce the
|
|
|
|
// generated WASM binary size.
|
|
|
|
args = append(args, "-trimpath", "-ldflags", "-s -w")
|
2022-08-04 17:37:19 +00:00
|
|
|
} else if *devControl != "" {
|
|
|
|
args = append(args, "-ldflags", fmt.Sprintf("-X 'main.ControlURL=%v'", *devControl))
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
2022-08-15 23:48:00 +00:00
|
|
|
|
|
|
|
args = append(args, "-o", outputPath, "./wasm")
|
2022-06-07 21:24:22 +00:00
|
|
|
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
|
|
|
|
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2022-08-15 23:48:00 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot build main.wasm: %w", err)
|
|
|
|
}
|
2022-10-24 23:05:01 +00:00
|
|
|
log.Printf("Built wasm in %v\n", time.Since(start).Round(time.Millisecond))
|
|
|
|
|
|
|
|
if !dev {
|
|
|
|
err := runWasmOpt(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot run wasm-opt: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:48:00 +00:00
|
|
|
return os.ReadFile(outputPath)
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:05:01 +00:00
|
|
|
func runWasmOpt(path string) error {
|
|
|
|
start := time.Now()
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot stat %v: %w", path, err)
|
|
|
|
}
|
|
|
|
startSize := stat.Size()
|
2023-01-31 02:41:53 +00:00
|
|
|
cmd := exec.Command("../../tool/wasm-opt", "--enable-bulk-memory", "-Oz", path, "-o", path)
|
2022-10-24 23:05:01 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot run wasm-opt: %w", err)
|
|
|
|
}
|
|
|
|
stat, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot stat %v: %w", path, err)
|
|
|
|
}
|
|
|
|
endSize := stat.Size()
|
|
|
|
log.Printf("Ran wasm-opt in %v, size dropped by %dK\n", time.Since(start).Round(time.Millisecond), (startSize-endSize)/1024)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 21:24:22 +00:00
|
|
|
// installJSDeps installs the JavaScript dependencies specified by package.json
|
|
|
|
func installJSDeps() error {
|
|
|
|
log.Printf("Installing JS deps...\n")
|
2022-07-27 22:46:13 +00:00
|
|
|
return runYarn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runYarn(args ...string) error {
|
|
|
|
cmd := exec.Command(*yarnPath, args...)
|
2022-07-22 00:58:50 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
2022-06-07 21:24:22 +00:00
|
|
|
}
|
2022-07-20 01:04:09 +00:00
|
|
|
|
|
|
|
// EsbuildMetadata is the subset of metadata struct (described by
|
|
|
|
// https://esbuild.github.io/api/#metafile) that we care about for mapping
|
|
|
|
// from entry points to hashed file names.
|
|
|
|
type EsbuildMetadata struct {
|
|
|
|
Outputs map[string]struct {
|
2022-08-02 22:02:26 +00:00
|
|
|
Inputs map[string]struct {
|
|
|
|
BytesInOutput int64 `json:"bytesInOutput"`
|
|
|
|
} `json:"inputs,omitempty"`
|
2022-07-20 01:04:09 +00:00
|
|
|
EntryPoint string `json:"entryPoint,omitempty"`
|
|
|
|
} `json:"outputs,omitempty"`
|
|
|
|
}
|
2022-07-26 00:51:06 +00:00
|
|
|
|
|
|
|
func setupEsbuildTailwind(build esbuild.PluginBuild, dev bool) {
|
|
|
|
build.OnLoad(esbuild.OnLoadOptions{
|
2022-08-24 01:05:23 +00:00
|
|
|
Filter: "./src/.*\\.css$",
|
2022-07-26 00:51:06 +00:00
|
|
|
}, func(args esbuild.OnLoadArgs) (esbuild.OnLoadResult, error) {
|
|
|
|
start := time.Now()
|
|
|
|
yarnArgs := []string{"--silent", "tailwind", "-i", args.Path}
|
|
|
|
if !dev {
|
|
|
|
yarnArgs = append(yarnArgs, "--minify")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(*yarnPath, yarnArgs...)
|
|
|
|
tailwindOutput, err := cmd.Output()
|
2022-10-24 23:05:01 +00:00
|
|
|
log.Printf("Ran tailwind in %v\n", time.Since(start).Round(time.Millisecond))
|
2022-07-26 00:51:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
log.Printf("Tailwind stderr: %s", exitErr.Stderr)
|
|
|
|
}
|
|
|
|
return esbuild.OnLoadResult{}, fmt.Errorf("Cannot run tailwind: %w", err)
|
|
|
|
}
|
|
|
|
tailwindOutputStr := string(tailwindOutput)
|
|
|
|
return esbuild.OnLoadResult{
|
|
|
|
Contents: &tailwindOutputStr,
|
|
|
|
Loader: esbuild.LoaderCSS,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|