Giter VIP home page Giter VIP logo

Comments (3)

hi-ogawa avatar hi-ogawa commented on September 27, 2024 1

I've seen a similar issue recently and your case is somewhat similar where wagmi is somehow inlined (i.e. processed vite/vitest transform).

I thought there might be some packaging issue of wagmi, but they are all green on publint, so there might be a problem with Vitest's externalization heuristics.

For the time being, I think it should be safe to explicitly set server.deps.external, so that wagmi always ends up with import("wagmi"), meaning there is only a single copy managed by NodeJs.

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    environment: "jsdom",
    server: {
      deps: {
        external: ["wagmi"]
      }
    }
  },
});

I'll need to look into further to see where the issue lies.

from vitest.

pt7892 avatar pt7892 commented on September 27, 2024

Thank you for help, I can confirm that this workaround/fix works!

from vitest.

hi-ogawa avatar hi-ogawa commented on September 27, 2024

Vitest's externalization heuristic is based on isValidNodeImport and this might be the one having an issue.

if (/\.(\w+-)?esm?(-\w+)?\.js$|\/(esm?)\//.test(id))
return false
id = id.replace('file:///', '')
const package_ = await findNearestPackageData(dirname(id))
if (package_.type === 'module')
return true

As mentioned in the comment, isValidNodeImport seems to be copied from mlly, but the original one actually has a flipped condition where it checks pkg.type === "module" before .../esm/...js:

https://github.com/unjs/mlly/blob/c5bcca0cda175921344fd6de1bc0c499e73e5dac/src/syntax.ts#L82-L90

  const package_ = await readPackageJSON(resolvedPath).catch(() => {});
  // @ts-ignore
  if (package_?.type === "module") {
    return true;
  }

  if (/\.(\w+-)?esm?(-\w+)?\.js$|\/(esm?)\//.test(resolvedPath)) {
    return false;
  }

wagmi is resolved to ./dist/esm/exports/index.js, so the regex check hits first and isValidNodeImport is returning false on Vitest.

I wonder if Vitest introduced this flipped version on purpose or it may be a simple oversight. The PR #4368 is quite big, so I haven't gone through all the context.

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.