2022-06-07 21:24:22 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-15 23:48:00 +00:00
|
|
|
"io/ioutil"
|
2022-06-07 21:24:22 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"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.
|
|
|
|
if _, filename, _, ok := runtime.Caller(0); ok {
|
|
|
|
if err := os.Chdir(path.Dir(filename)); err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot change cwd: %w", err)
|
|
|
|
}
|
|
|
|
}
|
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-07-26 22:45:52 +00:00
|
|
|
EntryPoints: []string{"src/index.ts", "src/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
|
|
|
},
|
2022-08-04 21:18:47 +00:00
|
|
|
JSXMode: esbuild.JSXModeAutomatic,
|
2022-06-07 21:24:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
outputFile, err := ioutil.TempFile("", "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()
|
|
|
|
defer os.Remove(outputPath)
|
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)
|
|
|
|
}
|
|
|
|
log.Printf("Built wasm in %v\n", time.Since(start))
|
|
|
|
return os.ReadFile(outputPath)
|
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{
|
|
|
|
Filter: "./src/index.css$",
|
|
|
|
}, 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()
|
|
|
|
log.Printf("Ran tailwind in %v\n", time.Since(start))
|
|
|
|
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
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|