Skip to content

Commit

Permalink
Merge pull request #300 from x1unix/fix/wasm-exec-path
Browse files Browse the repository at this point in the history
feat: move wasm files to separate dir
  • Loading branch information
x1unix authored Jan 9, 2024
2 parents 85f59d5 + 068a9c0 commit ae7c357
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
REACT_APP_GO_VERSION: "${{ env.GO_VERSION }}"
REACT_APP_PREV_GO_VERSION: "${{ env.PREV_GO_VERSION }}"
REACT_APP_WASM_API_VER: "${{ env.WASM_API_VER }}"
REACT_APP_WASM_BASE_URL: "/wasm"
- name: Build and push image
uses: docker/build-push-action@v5
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ include docker.mk

# Exports
export REACT_APP_VERSION=$(APP_VERSION)
export REACT_APP_WASM_API_VER=$(WASM_API_VER)
export REACT_APP_GITHUB_URL=$(REPO_URL)
export REACT_APP_WASM_API_VER=$(WASM_API_VER)
export REACT_APP_WASM_BASE_URL=/wasm

.PHONY:run
run:
Expand Down
4 changes: 2 additions & 2 deletions build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WASM_API_VER ?= $(shell cat ./cmd/wasm/api-version.txt)
define build_wasm_worker
@echo ":: Building WebAssembly worker '$(1)' ..."
GOOS=js GOARCH=wasm $(GO) build -buildvcs=false -ldflags "-s -w" -trimpath \
$(2) -o $(PUBLIC_DIR)/$(1)@$(WASM_API_VER).wasm ./cmd/wasm/$(1)
$(2) -o $(PUBLIC_DIR)/wasm/$(1)@$(WASM_API_VER).wasm ./cmd/wasm/$(1)
endef

define check_tool
Expand Down Expand Up @@ -59,7 +59,7 @@ build-ui:

.PHONY: wasm_exec.js
wasm_exec.js:
@cp "$(GOROOT)/misc/wasm/wasm_exec.js" $(PUBLIC_DIR)
@cp "$(GOROOT)/misc/wasm/wasm_exec.js" $(PUBLIC_DIR)/wasm/wasm_exec@$(WASM_API_VER).js

.PHONY:build-webworker
analyzer.wasm:
Expand Down
7 changes: 4 additions & 3 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN yarn install --silent && \
REACT_APP_GO_VERSION=$GO_VERSION \
REACT_APP_PREV_GO_VERSION=$PREV_GO_VERSION \
REACT_APP_WASM_API_VER=$WASM_API_VER \
REACT_APP_WASM_BASE_URL=/wasm \
yarn build

FROM golang:${GO_VERSION}-alpine as build
Expand All @@ -45,7 +46,7 @@ RUN echo "Building server with version $APP_VERSION" && \
-ldflags "-s -w" \
-trimpath \
-o ./analyzer@$WASM_API_VER.wasm ./cmd/wasm/analyzer && \
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .
cp $(go env GOROOT)/misc/wasm/wasm_exec.js ./wasm_exec@$WASM_API_VER.js

FROM golang:${GO_VERSION}-alpine as production
ARG GO_VERSION
Expand All @@ -60,8 +61,8 @@ ENV APP_GTAG_ID=''
COPY data ./data
COPY --from=ui-build /tmp/web/build ./public
COPY --from=build /tmp/playground/server .
COPY --from=build /tmp/playground/*.wasm ./public
COPY --from=build /tmp/playground/wasm_exec.js ./public
COPY --from=build /tmp/playground/*.wasm ./public/wasm/
COPY --from=build /tmp/playground/*.js ./public/wasm/
EXPOSE 8000
ENTRYPOINT /opt/playground/server \
-f='/opt/playground/data/packages.json' \
Expand Down
6 changes: 3 additions & 3 deletions build/release.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RUN echo "Building server with version $APP_VERSION" && \
-ldflags "-s -w" \
-trimpath \
-o ./analyzer@$WASM_API_VER.wasm ./cmd/wasm/analyzer && \
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .
cp $(go env GOROOT)/misc/wasm/wasm_exec.js ./wasm_exec@$WASM_API_VER.js

FROM golang:${GO_VERSION}-alpine as production
ARG GO_VERSION
Expand All @@ -47,8 +47,8 @@ ENV APP_GTAG_ID=''
COPY data ./data
COPY web/build ./public
COPY --from=build /tmp/playground/server .
COPY --from=build /tmp/playground/*.wasm ./public
COPY --from=build /tmp/playground/wasm_exec.js ./public
COPY --from=build /tmp/playground/*.wasm ./public/wasm/
COPY --from=build /tmp/playground/*.js ./public/wasm/
EXPOSE 8000
ENTRYPOINT /opt/playground/server \
-f='/opt/playground/data/packages.json' \
Expand Down
2 changes: 0 additions & 2 deletions web/public/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
</div>
</div>
</div>
<script src="/wasm_exec.js"></script>
<script src="%REACT_APP_WASM_BASE_URL%/wasm_exec@%REACT_APP_WASM_API_VER%.js"></script>
<% if (process.env.NODE_ENV === 'production') { %>
{{ if .GoogleTagID }}
<script async src="https://www.googletagmanager.com/gtag/js?id={{.GoogleTagID}}"></script>
Expand Down
2 changes: 2 additions & 0 deletions web/public/wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
*.wasm
2 changes: 1 addition & 1 deletion web/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ registerRoute(
);

// Cache WebAssembly and Go assets
const goWasmAssetsRegExp = new RegExp('^/(wasm_exec.js|go-repl.wasm|worker.wasm)$');
const goWasmAssetsRegExp = new RegExp('^/wasm/(.*)(.js|.wasm)$', 'i');
const DAY_IN_SECONDS = 24 * 60 * 60;
registerRoute(
({ url }) => url.origin === self.location.origin && goWasmAssetsRegExp.test(url.pathname),
Expand Down
2 changes: 1 addition & 1 deletion web/src/services/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { default } from "./singleton";
export * from "./models";
export * from "./client";
export * from "./interface";
export * from "./utils";
export * from "./resources";
export * from "./provider";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
REACT_APP_WASM_API_VER: wasmApiVersion = 'v1',
REACT_APP_WASM_BASE_URL: wasmBaseUrl = '',
REACT_APP_WASM_BASE_URL: wasmBaseUrl = '/wasm',
} = process.env;

/**
Expand All @@ -9,3 +9,8 @@ const {
* @param name
*/
export const getWasmUrl = name => `${wasmBaseUrl}/${name}@${wasmApiVersion}.wasm`;

/**
* URL for Go Wasm executor
*/
export const wasmExecUrl = `${wasmBaseUrl}/wasm_exec@${wasmApiVersion}.js`;
2 changes: 1 addition & 1 deletion web/src/services/gorepl/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BrowserFSBinding } from "~/lib/gowasm/bindings/browserfs";
import { PackageDBBinding } from "~/lib/gowasm/bindings/packagedb";
import { Worker, WorkerBinding } from "~/lib/gowasm/bindings/worker";

import { getWasmUrl } from "~/services/api/utils";
import { getWasmUrl } from "~/services/api/resources";
import { wrapResponseWithProgress } from "~/utils/http";
import { FileSystemWrapper } from "~/services/go/fs";
import ProcessStub from "~/services/go/process";
Expand Down
4 changes: 2 additions & 2 deletions web/src/workers/analyzer.worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { instantiateStreaming } from "~/lib/go/common";
import { getWasmUrl } from "~/services/api/utils";
import { getWasmUrl, wasmExecUrl } from "~/services/api/resources";

declare const self: DedicatedWorkerGlobalScope;
export default {} as typeof Worker & { new (): Worker };
Expand All @@ -9,7 +9,7 @@ const FN_EXIT = 'exit';
const TYPE_ANALYZE = 'ANALYZE';
const TYPE_EXIT = 'EXIT';

self.importScripts('/wasm_exec.js');
self.importScripts(wasmExecUrl);

function wrapModule(module) {
const wrapped = {
Expand Down
3 changes: 2 additions & 1 deletion web/src/workers/repl.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
GoReplWorker,
startGoWorker, WorkerConfig,
} from "~/services/gorepl/worker";
import { wasmExecUrl } from "~/services/api/resources";

declare const self: DedicatedWorkerGlobalScope;
export default {} as typeof Worker & { new (): Worker };

self.importScripts('/wasm_exec.js');
self.importScripts(wasmExecUrl);

let worker: GoReplWorker|null = null;
const rpcClient = new Client(globalThis, {
Expand Down
6 changes: 3 additions & 3 deletions web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3198,9 +3198,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001286, caniuse-lite@^1.0.30001297:
version "1.0.30001479"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001479.tgz"
integrity sha512-6nuRFim5dx8Eu2tO+KJ9PiBdPHs7WB5Hdf+klDcyefyEuOAcfhihIv7pS+JFknJLUiNQbm1AJYKm0c9QOlQS/Q==
version "1.0.30001576"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001576.tgz"
integrity sha512-ff5BdakGe2P3SQsMsiqmt1Lc8221NR1VzHj5jXN5vBny9A6fpze94HiVV/n7XRosOlsShJcvMv5mdnpjOGCEgg==

case-sensitive-paths-webpack-plugin@^2.4.0:
version "2.4.0"
Expand Down

0 comments on commit ae7c357

Please sign in to comment.