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";