Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import from external URLs #4949

Closed
6 tasks done
sagargurtu opened this issue Jan 13, 2024 · 3 comments · Fixed by #4987
Closed
6 tasks done

Cannot import from external URLs #4949

sagargurtu opened this issue Jan 13, 2024 · 3 comments · Fixed by #4987

Comments

@sagargurtu
Copy link
Contributor

sagargurtu commented Jan 13, 2024

Describe the bug

Vitest is not able to export from external URLs.

Reproduction

Working Vite Example: https://stackblitz.com/edit/vitejs-vite-krutmh
Nonworking Vitest Example: https://stackblitz.com/edit/vitest-dev-vitest-fn6bpu?file=src%2Fbasic.js

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.2.0 
    vite: latest => 5.0.11 
    vitest: latest => 1.2.0

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 14, 2024

Thanks for the repro. FYI, Vite's SSR also doesn't support network imports yet:

Interestingly, the issue on Vitest (vite-node) might be actually different since Vite gets an error during ssrLoadModule (while transformRequest seems to be working fine). Vite-node doesn't use this API and also the error stack suggests Vitest is failing during loadAndTransform.


EDIT: Indeed, Vite side issue seems irrelevant. On Vitest, server.deps.external and poolOptions.threads/forks.execArgv can be used to enable nodejs's native network imports:

https://stackblitz.com/edit/vitest-dev-vitest-3ewr53?file=vite.config.ts

export default defineConfig({
  test: {
    poolOptions: {
      threads: {
        execArgv: ['--experimental-network-imports'],
      },
    },
    server: {
      deps: {
        external: [/^https:/],
      },
    },
  },
});

@sagargurtu
Copy link
Contributor Author

Thank you for providing the workaround. Will it become the official solution, or will Vitest incorporate this logic internally?

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 15, 2024

I think users should be still responsible for enabling --experimental-network-imports (unless you're using Vitest browser mode, in which case the flag is probably not necessary nor relevant), but the other part external: [/^https:/] could be handled by Vitest (vite-node) automatically just like data:... is always treated as external here:

// data: should be processed by native import,
// since it is a feature of ESM
if (id.startsWith('data:'))
return id

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants