Menci/vite-plugin-wasm

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

bitlands opened this issue · 7 comments

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

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

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

When you run yarn run build I get the same error

@Menci Could you already take a look?

Menci commented

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

Menci commented

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.

I've opened an issue here: nuxt/nuxt#14620

It's resolved now.