Skip to content

Commit

Permalink
fix: rust build (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Oct 13, 2023
1 parent 189cd42 commit bf96a61
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ await build({
minify: args.production,
sourcemap: args.production ? undefined : 'inline',
define: {
debug: !args.production ? 'true' : '',
debug: !args.production ? 'true' : 'false',
esbuild: 'true',
packageName: `'${packageName}'`
}
Expand Down
22 changes: 11 additions & 11 deletions projects/rust-compile-template/build.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import {spawn} from 'child_process';
import {promises} from 'fs';
import path from 'path';

const RELEASE = process.argv.includes('release');

console.log('building...');


const stream = spawn('wasm-pack', ['build', RELEASE ? '--release': '--dev'], {stdio: 'inherit'});
const stream = spawn('wasm-pack', ['build', RELEASE ? '--release' : '--dev'], {stdio: 'inherit'});

stream.on('exit', copyFiles);

async function copyFiles(code){
if (code){
async function copyFiles(code) {
if (code) {
return;
}

const __dirname = path.resolve();

const toPath = __dirname + '/../../src/static/wasm/component/';
const fromPath = __dirname + '/pkg/';

Expand All @@ -25,21 +26,20 @@ async function copyFiles(code){

let content = await promises.readFile(fromPath + 'rust_assembly_bg.js', 'utf8');

content = "import {promises} from 'fs';\nimport { fileURLToPath } from 'url';\n" +content;
content = "import {promises} from 'fs';\nimport { fileURLToPath } from 'url';\n" + content;

content = content.replace("import * as wasm from './rust_assembly_bg.wasm';",
`const loadPath = typeof esbuild !== 'undefined' ? '/../static/wasm/component/': '/../';
content = content.replace("import * as wasm from './rust_assembly_bg.wasm';",
`const loadPath = typeof esbuild !== 'undefined' ? '/../static/wasm/component/': '/../';
const wasmModule = new WebAssembly.Module(await promises.readFile(fileURLToPath(import.meta.url + loadPath + 'build.wasm')));
const wasmInstance = new WebAssembly.Instance(wasmModule, ${
RELEASE ? '{}': `{"./rust_assembly_bg.js": {
RELEASE ? '{}' : `{"./rust_assembly_bg.js": {
__wbindgen_throw: () => {}
}`
}});
}}`
});
const wasm = wasmInstance.exports;`);

promises.writeFile(toPath + 'index.js', content);



console.log('Done!');
}
1 change: 0 additions & 1 deletion projects/rust-compile-template/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(dead_code)]
#![feature(once_cell)]
mod actions;
pub mod better_string;
pub mod ejs;
Expand Down
1 change: 0 additions & 1 deletion projects/rust-compile-template/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(dead_code)]
#![feature(once_cell)]

mod actions;
mod html_search;
Expand Down
34 changes: 13 additions & 21 deletions src/static/wasm/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import {promises} from 'fs';
import { fileURLToPath } from 'url';
const loadPath = typeof esbuild !== 'undefined' ? '/../static/wasm/component/': '/../';
const wasmModule = new WebAssembly.Module(await promises.readFile(fileURLToPath(import.meta.url + loadPath + 'build.wasm')));
const wasmInstance = new WebAssembly.Instance(wasmModule, {"./rust_assembly_bg.js": {
__wbindgen_throw: () => {}
}});
const wasmInstance = new WebAssembly.Instance(wasmModule, {});
const wasm = wasmInstance.exports;

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();
let WASM_VECTOR_LEN = 0;

let cachedUint8Memory0;
function getUint8Memory0() {
Expand All @@ -21,12 +15,6 @@ function getUint8Memory0() {
return cachedUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;

let cachedTextEncoder = new lTextEncoder('utf-8');
Expand All @@ -46,8 +34,6 @@ const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'

function passStringToWasm0(arg, malloc, realloc) {

if (typeof(arg) !== 'string') throw new Error('expected a string argument');

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length);
Expand Down Expand Up @@ -76,7 +62,7 @@ function passStringToWasm0(arg, malloc, realloc) {
ptr = realloc(ptr, len, len = offset + arg.length * 3);
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
if (ret.read !== arg.length) throw new Error('failed to pass whole string');

offset += ret.written;
}

Expand Down Expand Up @@ -118,6 +104,16 @@ function getInt32Memory0() {
}
return cachedInt32Memory0;
}

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
/**
* @returns {string}
*/
Expand Down Expand Up @@ -309,10 +305,6 @@ export function html_attr_parser(text) {
}
}

export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);

0 comments on commit bf96a61

Please sign in to comment.