2

My webpack config:

{ test: /\.(png|jpg)$/, loader: 'url?limit=8192' }

In my project, I must import images like this:

import homeBg1 from './image/homeBg1.jpg';
import homeBg2 from './image/homeBg2.jpg';
import homeBg3 from './image/homeBg3.jpg';

The operating is cumbersome, who can give me some advice?Thx!

1 Answer 1

7

This code will import all *.jpg files from image folder:

var requireContext = require.context("./image", true, /^\.\/.*\.jpg$/);
requireContext.keys().map(requireContext);

More about require.context: https://webpack.js.org/guides/dependency-management/#requirecontext

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.