Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Webpack image can't find module

I am trying to export my images by doing so:

const path = require('path');
module.exports = {
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader',
        ],
      },
    ],
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

Here is my folder structure:

enter image description here

And here is my index.ts file:

import {css} from 'lit-element';
import ErrorIcon from './assets/icons/error.svg';
export const iconStyles = css`
  .icon-error {
    background-image : src("${ErrorIcon.src}");
  }
  .icon-warning {
     background-image : src("/assets/icons/warning.svg");
  }`;

When I run build I get the following error:

TS2307: Cannot find module './assets/icons/error.svg' or its corresponding type declarations.

What have I done wrong?

Upvotes: 1

Views: 1393

Answers (2)

Marc Rasmussen
Marc Rasmussen

Reputation: 20565

i figured out the issue apprently in typescript you have to add a file that looks like this:

declare module "*.svg" {
  const content: any;
  export default content;
}

Source

Upvotes: 1

tom
tom

Reputation: 10601

Give it a try:

background-image : url("${ErrorIcon.src}");

Upvotes: 0

Related Questions