Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Serve static images in development with Vite using new URL

I currently have a React App with Vite 2.7.1. I'm having trouble displaying static local images in Vite while in development.This works in production.

I'm using:

  new URL(path, import.meta.url);

And I've tried:

  new URL(path, import.meta.url).href;

Then I'd consume it like:

  const wavesBg = new URL(
  '../../../../resources/images/background-waves.svg', import.meta.url
);
 <img src={wavesBg } />

I don't know if this is a file server problem, but it does map the address correctly! enter image description here enter image description here

Here's my vite.config.ts file:

export default defineConfig({
  plugins: [
    viteCommonjs(),
    // https://www.npmjs.com/package/@vitejs/plugin-react
    react(),
    tsconfigPaths(),
    eslintPlugin(),
   
  ],
  resolve: {
    alias: {
      stream: 'stream-browserify',
      '~': path.resolve(__dirname, 'src'),
    },
  },
  server: {
    open: true,
    fs: {
      allow: [
        // https://vitejs.dev/config/#server-fs-allow
        searchForWorkspaceRoot(process.cwd()),
       // This is where I'm serving my images.
        '/libs/components/src/resources/images',
      ],
    },
  },
  optimizeDeps: {
 /// Omitted
  },
  // esbuild: { jsxInject: `import React from 'react'` },
});

Note. Vite is currently configured in a monorepo fashion served using the NX CLI.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda