Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack & Typescript image import

I'm working on a React application and using Webpack & Typescript. I would like to use an image in one of the <img/> tags. However, I did not find the proper way to have access to the image files.

webpack.config.js:

 ...  module: {         rules: [             ...             {                 test: /\.(png|jpe?g|svg)$/,                 loader: 'file-loader',                 options: {                     name: 'assets/[name].[ext]',                 }             }         ] 

app.tsx:

... render() {     return <img src='/assets/logo-large.png' alt="logo"/> } 

When running the app, the assets/logo-large.png resource is not found.

like image 812
Cosmin SD Avatar asked Apr 26 '17 15:04

Cosmin SD


People also ask

What is webpack used for?

This is why webpack exists. It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.

What is webpack in React?

Webpack is a popular module bundling system built on top of Node. js. It can handle not only combination and minification of JavaScript and CSS files, but also other assets such as image files (spriting) through the use of plugins.

Is webpack still used?

Loved by many, hated by some, known to all. And still the most popular bundler in 2021. With more than 15 million weekly downloads (at the time of writing this post), there's no doubt that Webpack is still the bundler par excellence in 2021.

What is webpack and do I need it?

According to the official site, Webpack is a static module bundler for modern JavaScript applications. It builds a dependency graph that is responsible for bundling all your javascript modules into one regardless where they are located when one javascript file depends on each other.


2 Answers

Alternatively, in your custom_typings folder (if you have that), you can add a new import-png.d.ts file:

declare module "*.png" {   const value: any;   export default value; } 

So you can import an image using:

import myImg from 'img/myImg.png'; 

Alternatively, as reported by @mario-petrovic, you sometimes need to use a different export option as below (export = syntax). See here for the differences between the two approaches:

declare module "*.png" {   const value: any;   export = value; } 

In which case you probably need to import the image as:

import * as myImg from 'img/myImg.png'; 
like image 98
Erik Vullings Avatar answered Sep 25 '22 03:09

Erik Vullings


Setup Webpack file-loader, add declaration.d.ts, and voila!

after spending some time to figure out the solution, this is what I did...

Step 1

ensure that you have installed file-loader as a dev dependency by

npm install -D file-loader, if you use yarn yarn add -D file-loader

Step 2

add the loader corresponding to the file extension in the Webpack rules webpack.config.js, like this

module: {     rules: [       ...,       {         test: /\.(png|jpe?g|gif|jp2|webp)$/,         loader: 'file-loader',         options: {           name: '[name].[ext]',         },       },     ],   },  

Step 3

create an index.d.ts file next to your tsconfig.json file, actually you can name it whatever you want but you have to follow step 4.

Since Webpack now is going to handle several image extensions so you can add other image formats supported by file-loader

declare module '*.png'; declare module '*.jpg'; 

Step 4

go to your tsconfig.json file and add index.d.ts to the include array like this:

{   "compilerOptions": {     ...,     "jsx": "react",     "esModuleInterop": true,     "target": "ES2020",     "moduleResolution": "node"   },   "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],   "include": ["src", "index.d.ts"] /// <-- Like this!! } 

be aware that if you haven't defined an include array, typescript by default is going to add all the files on the root folder, if you just define one file and not the file that contains all your code it is not going to compile. I think is a good practice to have all your code within the src folder.

Voila!!

like image 30
Carlos Avatar answered Sep 24 '22 03:09

Carlos