react: typescript import images alias

1、webpack.config.js

resolve: {

  extensions: ["ts", "tsx", "js", "jsx"],

  alias: {

    images: path.resolve(__dirname, 'src/images/'

  )}

}

2、tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths: {
        "images/*": ["src/images/*"]
        }
    }
}

3、declare images.d.ts

declare module '*.png'
declare module '*.gif'
declare module '*.jpeg'
declare module '*.jpg'

 4、webpack.config.js

{
        test: /\.(gif|jpg|png|woff|svg|eot|ttf|otf|woff2)\??.*$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[path][name].[ext]'
            }
          }
        ]
      }

 5、import * as Img from "./path/to/image.png";

posted @ 2019-07-22 12:57  Nyan  阅读(671)  评论(0编辑  收藏  举报