Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!