nshen/vite-plugin-wasm-pack

use object api

Opened this issue · 5 comments

actual

// vite.config.js

import wasmPack from 'vite-plugin-wasm-pack';

  // only use local crate
  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']),
  ],

expected

// vite.config.js

import wasmPack from 'vite-plugin-wasm-pack';

  // only use local crate
  plugins: [
    wasmPack({ local: ['./my-local-crate'] }),
  ],

  // only use npm crate
  plugins: [
    wasmPack({ npm: ['test-npm-crate'] }),
  ],

  // use both local and npm crate
  plugins: [
    wasmPack({ local: ['./my-local-crate'], npm: ['test-npm-crate'] }),
  ],
// index.d.ts

//import type { Plugin } from 'vite';
type Plugin = any;

export default function vitePluginWasmPack(options: {
  local?: string[],
  npm?: string[],
}): Plugin;
nshen commented

hi, @milahu

looks great, would you like to create a pull request?

nope sorry : /

im only using vite-plugin-wasm-pack
as a base for my vite-plugin-emscripten / vite-plugin-treesitter

the api could be even simpler, for example ...
this would make it easier to generalize the code for multiple wasm compilers

// vite.config.js

import wasmPack from 'vite-plugin-wasm-pack';

  // use both local and npm crate
  plugins: [
    wasmPack(
      // list of packages
      [
        './my-local-crate', // local paths start with ./
        'test-npm-crate',
        '@org/pkg',
        { path: './a/b/c', type: 'emscripten', src: /\.(c|cc|cpp)$/ }, // local options for one packages
        { path: 'another-npm-package', type: 'makefile' },
      ],
      // global options for all packages
      {
        globalOption1: '...',
        globalOption2: '...',
      }
    ),
  ],

the resolveId(id) seems to be a noop, since all my ids start with /node_modules/
so that (path.basename(localPathList[i]) === id) is always false

im only using vite-plugin-wasm-pack
as a base for my vite-plugin-emscripten / vite-plugin-treesitter

https://github.com/milahu/vite-plugin-tree-sitter/blob/master/index.js

I'm trying to use this plugin to see if it resolves using wasm-pack-ed packages with Quasar.
Unfortunately, Quasar only supports passing the parameters in object notation: https://quasar.dev/quasar-cli-vite/handling-vite#adding-vite-plugins

Solving this issue would also allow using the plugin with the Quasar framework.

The suggestion seems to me like a breaking change, though. Would that be ok?

I tried it here: https://github.com/alensiljak/vite-plugin-wasm-pack/tree/object-params
but something is wrong with the expected parameters. I'm passing the package name, as listed in package.json. There, it points to the local folder.
Will have to continue this at another time, unfortunately.