New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-wasm-pack

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-wasm-pack - npm Package Compare versions

Comparing version 0.1.6 to 0.1.7

22

dist/index.d.ts
import { Plugin } from 'vite';
declare function vitePluginWasmPack(crates: string[] | string): Plugin;
/**
* return a Vite plugin for handling wasm-pack crate
*
* only use local crate
*
* import wasmPack from 'vite-plugin-wasm-pack';
*
* plugins: [wasmPack(['./my-local-crate'])]
*
* only use npm crate, leave the first param to an empty array
*
* plugins: [wasmPack([],['test-npm-crate'])]
*
* use both local and npm crate
*
* plugins: [wasmPack(['./my-local-crate'],['test-npm-crate'])]
*
* @param crates local crates paths, if you only use crates from npm, leave an empty array here.
* @param moduleCrates crates names from npm
*/
declare function vitePluginWasmPack(crates: string[] | string, moduleCrates?: string[] | string): Plugin;
export default vitePluginWasmPack;

115

dist/index.js

@@ -6,20 +6,59 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const narrowing_1 = require("narrowing");
function vitePluginWasmPack(crates) {
const path_1 = __importDefault(require("path"));
/**
* return a Vite plugin for handling wasm-pack crate
*
* only use local crate
*
* import wasmPack from 'vite-plugin-wasm-pack';
*
* plugins: [wasmPack(['./my-local-crate'])]
*
* only use npm crate, leave the first param to an empty array
*
* plugins: [wasmPack([],['test-npm-crate'])]
*
* use both local and npm crate
*
* plugins: [wasmPack(['./my-local-crate'],['test-npm-crate'])]
*
* @param crates local crates paths, if you only use crates from npm, leave an empty array here.
* @param moduleCrates crates names from npm
*/
function vitePluginWasmPack(crates, moduleCrates) {
const prefix = '@vite-plugin-wasm-pack@';
const pkg = 'pkg';
const pkg = 'pkg'; // default folder of wasm-pack module
let config_base;
let config_assetsDir;
const cratePaths = (0, narrowing_1.isString)(crates) ? [crates] : crates;
function wasmFilename(cratePath) {
return path_1.default.basename(cratePath).replace('-', '_') + '_bg.wasm';
}
const modulePaths = !moduleCrates
? []
: (0, narrowing_1.isString)(moduleCrates)
? [moduleCrates]
: moduleCrates;
// from ../../my-crate -> my_crate_bg.wasm
const wasmFilename = (cratePath) => {
return path_1.default.basename(cratePath).replace(/\-/g, '_') + '_bg.wasm';
};
// wasmfileName : CrateType
const wasmMap = new Map();
// 'my_crate_bg.wasm': {path:'../../my_crate/pkg/my_crate_bg.wasm', isNodeModule: false}
cratePaths.forEach((cratePath) => {
const wasmFile = wasmFilename(cratePath);
wasmMap.set(wasmFile, path_1.default.join(cratePath, pkg, wasmFile));
wasmMap.set(wasmFile, {
path: path_1.default.join(cratePath, pkg, wasmFile),
isNodeModule: false
});
});
// 'my_crate_bg.wasm': { path: 'node_modules/my_crate/my_crate_bg.wasm', isNodeModule: true }
modulePaths.forEach((cratePath) => {
const wasmFile = wasmFilename(cratePath);
wasmMap.set(wasmFile, {
path: path_1.default.join('node_modules', cratePath, wasmFile),
isNodeModule: true
});
});
return {

@@ -42,3 +81,3 @@ name: 'vite-plugin-wasm-pack',

id = id.replace(prefix, '');
const modulejs = path_1.default.join('./node_modules', id, id.replace('-', '_') + '.js');
const modulejs = path_1.default.join('./node_modules', id, id.replace(/\-/g, '_') + '.js');
const code = await fs_extra_1.default.promises.readFile(modulejs, {

@@ -50,30 +89,47 @@ encoding: 'utf-8'

},
async buildStart(inputOptions) {
for await (const cratePath of cratePaths) {
const pkgPath = path_1.default.join(cratePath, pkg);
async buildStart(_inputOptions) {
const prepareBuild = async (cratePath, isNodeModule) => {
const pkgPath = isNodeModule
? path_1.default.join('node_modules', cratePath)
: path_1.default.join(cratePath, pkg);
const crateName = path_1.default.basename(cratePath);
if (!fs_extra_1.default.existsSync(pkgPath)) {
console.error(chalk_1.default.bold.red('Error: ') +
`Can't find ${chalk_1.default.bold(pkgPath)}, run ${chalk_1.default.bold.red(`wasm-pack build ${cratePath} --target web`)} first`);
this.error(`Can't find ${pkgPath}, run 'wasm-pack build ${cratePath} --target web' first`);
if (isNodeModule) {
console.error(chalk_1.default.bold.red('Error: ') +
`Can't find ${chalk_1.default.bold(pkgPath)}, run ${chalk_1.default.bold.red(`npm install ${cratePath}`)} first`);
}
else {
console.error(chalk_1.default.bold.red('Error: ') +
`Can't find ${chalk_1.default.bold(pkgPath)}, run ${chalk_1.default.bold.red(`wasm-pack build ${cratePath} --target web`)} first`);
}
}
try {
await fs_extra_1.default.copy(pkgPath, path_1.default.join('node_modules', crateName));
if (!isNodeModule) {
// copy pkg generated by wasm-pack to node_modules
try {
await fs_extra_1.default.copy(pkgPath, path_1.default.join('node_modules', crateName));
}
catch (error) {
this.error(`copy crates failed`);
}
}
catch (error) {
this.error(`copy crates failed`);
return;
}
const jsName = crateName.replace('-', '_') + '.js';
// replace default load path with '/assets/xxx.wasm'
const jsName = crateName.replace(/\-/g, '_') + '.js';
const jsPath = path_1.default.join('./node_modules', crateName, jsName);
const regex = /input = new URL\('(.+)'.+;/g;
let code = fs_extra_1.default.readFileSync(path_1.default.resolve(jsPath), { encoding: 'utf-8' });
code = code.replace(regex, (match, group1) => {
code = code.replace(regex, (_match, group1) => {
return `input = "${path_1.default.posix.join(config_base, config_assetsDir, group1)}"`;
});
fs_extra_1.default.writeFileSync(jsPath, code);
};
for await (const cratePath of cratePaths) {
await prepareBuild(cratePath, false);
}
for await (const cratePath of modulePaths) {
await prepareBuild(cratePath, true);
}
},
configureServer({ middlewares }) {
return () => {
// send 'root/pkg/xxx.wasm' file to user
middlewares.use((req, res, next) => {

@@ -83,5 +139,6 @@ if ((0, narrowing_1.isString)(req.url)) {

res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
if (basename.endsWith('.wasm') && wasmMap.get(basename) != null) {
const entry = wasmMap.get(basename);
if (basename.endsWith('.wasm') && entry) {
res.writeHead(200, { 'Content-Type': 'application/wasm' });
fs_extra_1.default.createReadStream(wasmMap.get(basename)).pipe(res);
fs_extra_1.default.createReadStream(entry.path).pipe(res);
}

@@ -96,8 +153,8 @@ else {

buildEnd() {
cratePaths.forEach((c) => {
const wasmFile = wasmFilename(c);
// copy xxx.wasm files to /assets/xxx.wasm
wasmMap.forEach((crate, fileName) => {
this.emitFile({
type: 'asset',
fileName: `assets/${wasmFile}`,
source: fs_extra_1.default.readFileSync(wasmMap.get(wasmFile))
fileName: `assets/${fileName}`,
source: fs_extra_1.default.readFileSync(crate.path)
});

@@ -104,0 +161,0 @@ });

{
"name": "vite-plugin-wasm-pack",
"version": "0.1.6",
"version": "0.1.7",
"description": "Vite plugin for rust using wasm-pack 🦀",

@@ -15,9 +15,9 @@ "main": "dist/index.js",

"keywords": [
"vite",
"vite-plugin",
"wasm-pack",
"wasm-bindgen",
"rust",
"vite-plugin",
"webassembly",
"wasm",
"rust",
"vite",
"cargo"

@@ -24,0 +24,0 @@ ],

@@ -49,3 +49,3 @@ # 🦀 vite-plugin-wasm-pack

```bash
vite v2.3.8 dev server running at:
vite v2.6.5 dev server running at:

@@ -76,6 +76,3 @@ > Local: http://localhost:3000/

export default defineConfig({
build: {
minify: false
},
// pass your crate path to the plugin
// pass your local crate path to the plugin
plugins: [wasmPack('./my-crate')]

@@ -118,2 +115,12 @@ });

## Use wasm-pack package installed via npm
If you want use a package from npm that built with wasm-pack, like this one [test-npm-crate](https://www.npmjs.com/package/test-npm-crate)
you have to pass the package name to the second param of our plugin.
`wasmPack(['./my-local-crate'],['test-npm-crate'])`
full example is in [./npm-crate-example](./npm-crate-example) folder.
## Examples

@@ -120,0 +127,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc