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

212
Visualizações
React & Webpack - How to make local images folder public and redirect `src` from `img` to it?

The context

I'm working on a React project where the styles and the images are being provided by an external theme, as the React app will be embeded into a portlet inside this webpage. As so, the images inside the React app are relative and point to a directory which I don't have access to during development.

<img src="/relative/external/theme/images/themeImg.png" />

As I don't have access to it, when I develop locally I am not able to see any of those images. However, for development purposes I do have those image files locally in my src/js/images/ local directory.

We are using Webpack 4.

The current config

Here I leave part of the actual configuration (I cannot publish everything):

 {
    name: 'main',
    mode: 'development',
    devtool: 'source-map',
    output: {
      filename: 'main.js',
      path: path.resolve(__dirname, 'dist'),
    },
    entry: {
      main: './src/main/js/main.js',
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          resolve: { extensions: ['.js', '.jsx'] },
          exclude: /node_modules/,
          loaders: ['babel-loader'],
        },
        {
          test: /\.svg$/,
          use: [
            {
              loader: 'svg-url-loader',
            },
          ],
        },
        { test: /\.css$/, loader: 'style-loader!css-loader' },
        {
          test: /\.scss$/,
          use: [
            {
              loader: 'style-loader',
            },
            {
              loader: 'css-loader',
            },
            {
              loader: 'sass-loader',
            },
          ],
        },
      ],
    },
    devServer: {
      contentBase: path.join(__dirname, 'dist'),
      port: 9000,
      historyApiFallback: true,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods':
          'GET, POST, PUT, DELETE, PATCH, OPTIONS',
        'Access-Control-Allow-Headers':
          'X-Requested-With, content-type, Authorization',
      },
    }
  }

The question

Is there a way to which I can tell Webpack to make my src/js/images/ directory public, so it's accessible by the HTML src attributes, and redirect all relative/external/theme/images calls to that public images/ directory in development (without messing with publicPath so the whole bundle is still served from the same place)?

I cannot import images and use them in the src since I cannot import them from a directory I don't have access to, that's why the whole folder should be public and available to load the images from it.

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