vite-plugin-wasm-pack icon indicating copy to clipboard operation
vite-plugin-wasm-pack copied to clipboard

use object api

Open milahu opened this issue 3 years ago • 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;

milahu avatar Nov 12 '21 06:11 milahu

hi, @milahu

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

nshen avatar Nov 12 '21 07:11 nshen

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

milahu avatar Nov 12 '21 17:11 milahu

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

milahu avatar Nov 12 '21 18:11 milahu

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?

alensiljak avatar Jul 26 '23 20:07 alensiljak

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.

alensiljak avatar Jul 26 '23 21:07 alensiljak