question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't build in Vite

See original GitHub issue

magick-wasm version

Any

Description

When trying to build with Vite, it gives the following warning, but not always on the same file: 'ws' is imported by ws?commonjs-external, but could not be resolved – treating it as an external dependency It decides to alias ws to require$$2, which doesn’t exist, breaking the build.

I tried installing ws, which stops Vite from complaining. But the build still doesn’t work, saying that Buffer does not exist, suggesting it tries to run in a node environment.

From what I can find, the problematic code resides in @imagemagick/magick-wasm/wasm/magick.js.

Steps to Reproduce

Set up a minimal project (use single -- for npm 6 and under):

npm init vite vite-build-test -- --template vanilla
cd vite-build-test
npm i @imagemagick/magick-wasm

Overwrite main.js to contain only the following:

import { initializeImageMagick } from "@imagemagick/magick-wasm";

Then try npm run build

Optionally serve the freshly created build: npm run preview

Images

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
Peeterushcommented, Sep 12, 2022

Finally got the same thing working in Vite! Under the hood it uses Rollup, so using rollup-plugin-ignore and the following config does the trick:

// vite.config.js
import { builtinModules } from "module";
import { defineConfig } from "vite";
import ignore from "rollup-plugin-ignore";

export default defineConfig({
  build: {
    rollupOptions: {
      plugins: [ignore([...builtinModules, "ws"])],
      // or:
      // plugins: [ignore(["ws", "fs", "child_process", "crypto", "path"])],
    },
  },
});

Edit: the ignore plugin isn’t needed when using the build.commonjsOptions.ignore option instead. This also seems to resolve an edgecase where the crypto module would not be ignored properly. So use this instead:

// vite.config.js
import { builtinModules } from "module";
import { defineConfig } from "vite";

export default defineConfig({
  build: {
    commonjsOptions: {
      ignore: [...builtinModules, "ws"],
      // or:
      // ignore: ["ws", "fs", "child_process", "crypto", "path"],
    },
  },
});
1reaction
dlemstracommented, Oct 6, 2022

I don’t have a plan for this, but I could probably do this tomorrow. Going to be this weekend instead…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't build app in production mode · Issue #1182 · vitejs/vite
Describe the bug. Can't successfuly build app in production mode running "npm run build", while it runs OK in development mode with "npm...
Read more >
Building for Production - Vite
When it is time to deploy your app for production, simply run the vite build command. By default, it uses <root>/index.html as the...
Read more >
Troubleshooting - Vite
If you are running Vite with WSL2, Vite cannot watch file changes in some conditions. ... TypeError: Cannot create property 'foo' on boolean...
Read more >
Build Options - Vite
Note the build will fail if the code contains features that cannot be safely transpiled by esbuild. See esbuild docs for more details....
Read more >
Features | Vite
Vite only performs transpilation on .ts files and does NOT perform type checking. It assumes type checking is taken care of by your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found