编译wasm ungzip压缩字符串
npm install ungzip-wasm编译wasm ungzip压缩字符串
``sh`
wasm-pack build --target bundler --release
输出的目录在 pkg目录下,默认是一个npm的包。
`sh
npm install ungzip-wasm
`
vue引入代码
1. 引入 wasm-loader
`sh
npm install wasm-loader -D
`
2. vue.config.js 加入配置
`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/',
},
},
],
},
],
},
},
};
3. 在需要用到的地方引入。由于wasm是二进制文件,因此需要异步引入
`js
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);
``