最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Importing svgs with TypeScript + Webpack - Stack Overflow

programmeradmin1浏览0评论

I have a pretty simple Webpack + TypeScript setup and am trying to import svgs. I'm using svg-sprite-loader and it works well if I require() my svgs like the following:

require('./assets/alert.svg')

--

However, I'd prefer to use the following:

import alert from './assets/alert.svg'

With that, I get the following error:

TS2307: Cannot find module './assets/alert.svg'.

I have a custom.d.ts file in my project root (after reading this: ). It looks like this:

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

My tsconfig looks like:

{
  "pilerOptions": {
    "module": "monjs",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "lib": ["es6", "dom"]
  },
  "exclude": [
    "**/*.spec.ts"
  ]
}

And finally, my webpack set up includes:

 module: {
   rules: [
       {
           test: /\.ts$/,
           use: `awesome-typescript-loader`
       },
       {
           test: /\.html$/,
           loader: 'html-loader'
       },
       {
           test: /\.svg$/,
           loader: 'svg-sprite-loader'
       }
   ]
 }

I'm trying to understand how that custom.d.ts file es into play. Is there something I need to add in my tsconfig?

Also, for versions, I'm using:

  • Webpack @ 2.5.0
  • TypeScript @ 2.4.0
  • svg-sprite-loader @ 3.6.2
  • awesome-typescript-loader @ 3.1.2

I have a pretty simple Webpack + TypeScript setup and am trying to import svgs. I'm using svg-sprite-loader and it works well if I require() my svgs like the following:

require('./assets/alert.svg')

--

However, I'd prefer to use the following:

import alert from './assets/alert.svg'

With that, I get the following error:

TS2307: Cannot find module './assets/alert.svg'.

I have a custom.d.ts file in my project root (after reading this: https://webpack.js/guides/typescript/#importing-other-assets). It looks like this:

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

My tsconfig looks like:

{
  "pilerOptions": {
    "module": "monjs",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "lib": ["es6", "dom"]
  },
  "exclude": [
    "**/*.spec.ts"
  ]
}

And finally, my webpack set up includes:

 module: {
   rules: [
       {
           test: /\.ts$/,
           use: `awesome-typescript-loader`
       },
       {
           test: /\.html$/,
           loader: 'html-loader'
       },
       {
           test: /\.svg$/,
           loader: 'svg-sprite-loader'
       }
   ]
 }

I'm trying to understand how that custom.d.ts file es into play. Is there something I need to add in my tsconfig?

Also, for versions, I'm using:

  • Webpack @ 2.5.0
  • TypeScript @ 2.4.0
  • svg-sprite-loader @ 3.6.2
  • awesome-typescript-loader @ 3.1.2
Share Improve this question asked Feb 20, 2018 at 20:26 amlyhammamlyhamm 1,1502 gold badges20 silver badges37 bronze badges 8
  • Is there anything else in your custom.d.ts? – Aaron Beall Commented Feb 20, 2018 at 20:44
  • Nope, just that! – amlyhamm Commented Feb 20, 2018 at 20:44
  • Seems like this setup should work. What pile or webpack error do you get after adding custom.d.ts? – Aaron Beall Commented Feb 20, 2018 at 20:44
  • TS2307: Cannot find module './assets/alert.svg' (from the file that has the import statement) – amlyhamm Commented Feb 20, 2018 at 20:46
  • Do you get that if you run tsc directly or only through webpack? – Aaron Beall Commented Feb 20, 2018 at 20:46
 |  Show 3 more ments

2 Answers 2

Reset to default 3

Try adding

"files": [ 
  "custom.d.ts" 
]  

to your tsconfig.json

You can specify a typeRoot in your tsconfig.json pointing to the location of custom.d.ts.

发布评论

评论列表(0)

  1. 暂无评论