Skip to content

Commit be7998c

Browse files
committed
Move to manual module initialization
1 parent c27cb06 commit be7998c

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ RUN sed -e "s|-isystem /emsdk/upstream/emscripten/cache/sysroot/include[^/]||g"
2424
RUN sed -e "s|-lfontconfig|/emsdk/upstream/emscripten/cache/sysroot/lib/libglib-2.0.a /emsdk/upstream/emscripten/cache/sysroot/lib/libzip.a /emsdk/upstream/emscripten/cache/sysroot/lib/libz.a /emsdk/upstream/emscripten/cache/sysroot/lib/libfontconfig.a|g" -i ../build/CMakeFiles/OpenSCAD.dir/linklibs.rsp
2525

2626
# Add emscripten flags here
27-
RUN sed -e "s|em++|em++ ${EMXX_FLAGS} -s USE_PTHREADS=0 -s NO_DISABLE_EXCEPTION_CATCHING -s FORCE_FILESYSTEM=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_RUNTIME_METHODS=['FS','callMain'] -s EXPORT_ES6=1 -s ENVIRONMENT=web,worker -s MODULARIZE=1 -s EXPORT_NAME=OpenSCAD -s EXIT_RUNTIME=0|g" -i ../build/CMakeFiles/OpenSCAD.dir/link.txt
27+
RUN sed -e "s|em++|em++ ${EMXX_FLAGS} -s USE_PTHREADS=0 -s NO_DISABLE_EXCEPTION_CATCHING -s FORCE_FILESYSTEM=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_RUNTIME_METHODS=['FS','callMain'] -s ENVIRONMENT=web,worker -s EXPORT_NAME=OpenSCAD -s EXIT_RUNTIME=1|g" -i ../build/CMakeFiles/OpenSCAD.dir/link.txt
2828

2929
RUN cd ../build && make -j12 VERBOSE=1

Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ DOCKER_FLAGS= --build-arg CMAKE_BUILD_TYPE=Debug --build-arg EMXX_FLAGS="-gsourc
2020
endif
2121

2222
.PHONY: build
23-
build: build/openscad.js build/openscad.fonts.js
23+
build: build/openscad.wasm.js build/openscad.fonts.js
2424

2525
build/openscad.fonts.js: runtime/node_modules runtime/**/* res
2626
mkdir -p build
@@ -30,16 +30,14 @@ build/openscad.fonts.js: runtime/node_modules runtime/**/* res
3030
runtime/node_modules:
3131
cd runtime; npm install
3232

33-
build/openscad.js: .image.make
33+
build/openscad.wasm.js: .image.make
3434
mkdir -p build
3535
docker run --name tmpcpy openscad
36-
docker cp tmpcpy:/build/openscad.js build/
36+
docker cp tmpcpy:/build/openscad.js build/openscad.wasm.js
3737
docker cp tmpcpy:/build/openscad.wasm build/
3838
docker cp tmpcpy:/build/openscad.wasm.map build/ || true
3939
docker rm tmpcpy
4040

41-
sed -i '1s&^&/// <reference types="./openscad.d.ts" />\n&' build/openscad.js
42-
4341
.image.make: .base-image.make Dockerfile
4442
docker build libs/openscad -f Dockerfile -t $(DOCKER_TAG_OPENSCAD) ${DOCKER_FLAGS}
4543
touch $@

runtime/plugins/type-reference.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { resolve } from "path";
2+
3+
export default function typeReference(file, name) {
4+
return {
5+
name: "type-reference",
6+
transform(code, id) {
7+
if (resolve(id) == resolve(file)) {
8+
return `/// <reference types="./${name}.d.ts" />\n${code}`;
9+
}
10+
},
11+
};
12+
}

runtime/rollup.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typescript from "@rollup/plugin-typescript";
22
import embedFile from "./plugins/embed-file";
3+
import typeReference from "./plugins/type-reference";
34

45
const bundle = (name) => ({
56
input: `src/${name}.ts`,
@@ -9,10 +10,13 @@ const bundle = (name) => ({
910
},
1011
plugins: [
1112
embedFile(),
12-
typescript({ tsconfig: "./tsconfig.json" })],
13+
typescript({ tsconfig: "./tsconfig.json" }),
14+
typeReference(`src/${name}.ts`, name),
15+
]
1316
});
1417

1518
export default [
19+
bundle("openscad"),
1620
bundle("openscad.fonts"),
1721
bundle("openscad.mcad")
1822
];

runtime/src/openscad.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface InitOptions {
55
export interface OpenSCAD {
66
callMain(args: Array<string>): number;
77
FS: FS;
8+
locateFile?: (path: string, prefix: string) => string;
9+
onRuntimeInitialized?: () => void;
810
}
911

1012
export interface FS {
@@ -19,8 +21,34 @@ export interface FS {
1921
unlink(path: string): void;
2022
}
2123

22-
// deno-lint-ignore no-unused-vars
23-
export default function (init: InitOptions): Promise<OpenSCAD> {
24-
// NULL implementation. Will be replaced by the actual OpenSCAD library
25-
return null as unknown as Promise<OpenSCAD>;
24+
declare module globalThis {
25+
let OpenSCAD: Partial<OpenSCAD> | undefined;
2626
}
27+
28+
let wasmModule: string;
29+
30+
async function OpenSCAD(options?: InitOptions): Promise<OpenSCAD> {
31+
if (!wasmModule) {
32+
const url = new URL(`./openscad.wasm.js`, import.meta.url).href;
33+
const request = await fetch(url);
34+
wasmModule = "data:text/javascript;base64," + btoa(await request.text());
35+
}
36+
37+
const module: Partial<OpenSCAD> = {
38+
noInitialRun: true,
39+
locateFile: (path: string) => new URL(`./${path}`, import.meta.url).href,
40+
...options,
41+
};
42+
43+
globalThis.OpenSCAD = module;
44+
await import(wasmModule + `#${Math.random()}`);
45+
delete globalThis.OpenSCAD;
46+
47+
await new Promise((resolve) => {
48+
module.onRuntimeInitialized = () => resolve(null);
49+
});
50+
51+
return module as unknown as OpenSCAD;
52+
}
53+
54+
export default OpenSCAD;

runtime/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"include": ["src"],
33
"exclude": ["dist"],
44
"compilerOptions": {
5+
"module": "esnext",
56
"target": "esnext",
67
"moduleResolution": "node",
78
"esModuleInterop": false,

0 commit comments

Comments
 (0)