Menci / vite-plugin-wasm

Add WebAssembly ESM integration (aka. Webpack's `asyncWebAssembly`) to Vite and support `wasm-pack` generated modules.
MIT License
281 stars 16 forks source link

Rollup error: Unexpected character '' (Note that you need plugins to import files that are not JavaScript) #10

Closed bitlands closed 1 year ago

bitlands commented 2 years ago

I get the following error when I'm building my Nuxt 3 project with yarn build (nuxt build) using vite-plugin-wasm.

Unexpected character '' (Note that you need plugins to import files that are not JavaScript)                                 09:02:57

  at error (node_modules/rollup/dist/es/shared/rollup.js:1858:30)
  at Module.error (node_modules/rollup/dist/es/shared/rollup.js:12412:16)
  at Module.tryParse (node_modules/rollup/dist/es/shared/rollup.js:12789:25)
  at Module.setSource (node_modules/rollup/dist/es/shared/rollup.js:12694:24)
  at ModuleLoader.addModuleSource (node_modules/rollup/dist/es/shared/rollup.js:22136:20)

My vite.config.ts looks like this:

import wasm from 'vite-plugin-wasm'

/** @type {import('vite').UserConfig} */
export default {
    build: {
    target: 'esnext'
  },
  plugins: [
        wasm(),
    ],
  server: {
    watch: {
      usePolling: true
    }
  }
}

This is the wasm lib I'm using. When I comment the import line the build runs without any problems.

import CardanoSerializationLibrary from '@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib'

class Loader {
  _wasm: typeof CardanoSerializationLibrary | undefined

  async load() {
    if (this._wasm) return
    this._wasm = await import('@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib')
  }

  get Cardano() {
    return this._wasm
  }
}

export default new Loader()

Unfortunately, I'm stuck with this issue. Any help is appreciated.

Menci commented 2 years ago

Could you upload a simple project that could reproduce this issue?

chrizcros commented 2 years ago

Yes: https://github.com/bitlands/wasm-import

When you run yarn run build I get the same error

chrizcros commented 2 years ago

@Menci Could you already take a look?

Menci commented 2 years ago

I just found in preview it works. I think there're something different with Nuxt-Vite's configuration.

Menci commented 2 years ago

It seems that vendor packages in node_modules are skipped processing by Nuxt-Vite. I don't know which configuration did this. Maybe you should open an issue at Nuxt.

chrizcros commented 2 years ago

I've opened an issue here: https://github.com/nuxt/nuxt.js/issues/14620

chrizcros commented 2 years ago

It's resolved now.