编译wasm ungzip压缩字符串
wasm-pack build --target bundler --release
输出的目录在 pkg目录下,默认是一个npm的包。
npm install ungzip-wasm
vue引入代码
- 引入 wasm-loader
npm install wasm-loader -D
- vue.config.js 加入配置
module.exports = {
configureWebpack: {
module: {
rules: [
{
test: /\.wasm$/,
type: 'webassembly/experimental',
},
{
test: /\.(png|jpg|gif|svg|ico)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/',
},
},
],
},
],
},
},
};
- 在需要用到的地方引入。由于wasm是二进制文件,因此需要异步引入
async function init() {
// 异步导入 WebAssembly 模块
const wasmModule = await import('ungzip-wasm');
// 使用 WebAssembly 模块
try {
const result = wasmModule.decompress_string('your_base64_string');
} catch (e) {
// wasm内部对错误并没有进行处理,在这里处理下
}
console.log('Decompressed string:', result);
}
init().catch(console.error);