Skip to content

Commit 6a7aaef

Browse files
authoredJan 13, 2023
feat: Provide an easily reusable Web SDK (#2615)
1 parent e1a5fce commit 6a7aaef

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed
 

‎scripts/build-web.js

+14-30
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,18 @@ const importmap = {
2525

2626
fs.writeFileSync(path.join(dirname, "..", "dist", "importmap.json"), `${JSON.stringify(importmap, null, 2)}
2727
`);
28-
fs.writeFileSync(path.join(dirname, "..", "dist", "importmap.js"), `var ASC_IMPORTMAP = ${JSON.stringify(importmap, null, 2)};
29-
`);
30-
fs.writeFileSync(path.join(dirname, "..", "dist", "web.html"), `<script async src="https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.js"></script>
31-
<script type="importmap">
32-
${JSON.stringify(importmap, null, 2)}
33-
</script>
34-
<script type="module">
35-
import asc from "assemblyscript/asc";
36-
37-
const files = {
38-
"index.ts": "export function add(a: i32, b: i32): i32 { return a + b; }"
39-
};
40-
41-
const { error, stdout } = await asc.main([
42-
"index.ts", "--textFile", "--optimize"
43-
], {
44-
readFile(name, baseDir) {
45-
if (Object.prototype.hasOwnProperty.call(files, name)) return files[name];
46-
return null;
47-
},
48-
writeFile(name, data, baseDir) {
49-
},
50-
listFiles(dirname, baseDir) {
51-
return [];
52-
}
53-
});
54-
55-
if (error) throw error;
56-
console.log(stdout.toString());
57-
</script>
28+
fs.writeFileSync(path.join(dirname, "..", "dist", "web.js"), `var ASSEMBLYSCRIPT_VERSION = ${JSON.stringify(mainVersion)};
29+
var ASSEMBLYSCRIPT_IMPORTMAP = ${JSON.stringify(importmap, null, 2)};
30+
if (!document.currentScript.src.includes("noinstall")) {
31+
let elem = document.createElement("script");
32+
elem.type = "importmap";
33+
elem.text = JSON.stringify(ASSEMBLYSCRIPT_IMPORTMAP);
34+
document.head.appendChild(elem);
35+
}
36+
if (!document.currentScript.src.includes("noshim")) {
37+
let elem = document.createElement("script");
38+
elem.async = true;
39+
elem.src = "https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.wasm.min.js";
40+
document.head.appendChild(elem);
41+
}
5842
`);

0 commit comments

Comments
 (0)
Please sign in to comment.