Skip to content

Documentation issue in Getting Started page #1064

Open
@mwmahlberg

Description

@mwmahlberg

The Makefile does not make a lot of sense. In its current form there are phony targets used without any need and that does not scale well when the project gets larger. Hence, I'd strongly suggest something like this (tested against the example code):

# We extract the name for the binary from the go.mod file
# and use it to build the binary and in the run target.
BINARY:=$(shell awk '/^module/{n=split($$2,parts,"/");print parts[n]}' go.mod )

# We use the find command to get all the go files in the current directory
# and its subdirectories, excluding the vendor directory (if it exists).
# This is used to build the binary and the wasm file.
# The reason is that subsequent builds will only happen if the source files change.
GOFILES=$(shell find . -name '*.go' -not -path './vendor/*')

MODFILES=go.mod go.sum

WASMDIR=web

# We declare our PHONY targets, that is targets that are not files.
# This is useful for targets that are always run, like run and clean.
.PHONY: run clean debug

# Note that make will run the first target if no target is specified.
# It's dependencies are built first, if they are not up to date.
run: $(WASMDIR)/app.wasm $(BINARY)
	./$(BINARY)

debug:
	@echo "BINARY: $(BINARY)"
	@echo "GOFILES: $(GOFILES)"
	@echo "WASMDIR: $(WASMDIR)"

# Create the web directory if it does not exist where app expects to find the wasm file.
$(WASMDIR):
	mkdir -p $@

# Build the app.wasm file and put it into the appropriate directory.
# Also, the | operator is used to create the web directory if it does not exist, without
# making it a source dependency.
$(WASMDIR)/app.wasm : $(GOFILES) $(MODFILES)|  $(WASMDIR)/
	GOARCH=wasm GOOS=js go build -o $@

# Build the binary file and put it into the current directory.
# If any file in GOFILES is newer than the binary, it will be rebuilt.
$(BINARY): $(GOFILES) $(MODFILES)
	go build -o $@

clean:
	$(RM) $(BINARY)
	$(RM) -r $(WASMDIR)

or, to be more portable and easier to read:

.PHONY: run

.DEFAULT_GOAL := run

web/:
	mkdir -p $@

web/app.wasm: | web/
	GOARCH=wasm GOOS=js go build -o $@

server:
	go build -o $@

run: web/app.wasm server
	./server

clean:
	$(RM) server
	$(RM) -r web

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions