我正在尝试使用webpack导入图像。像这样:
import img from '../assets/bmo.png';
但我不断收到错误消息
未找到模块:'../ assets / bmo.png'
我尝试了各种不同的图像路径:
import img from '../assets/bmo.png';
import img from '../../assets/bmo.png';
import img from './assets/bmo.png';
import img from './src/assets/bmo.png';
其他文件的导入可以起作用:
import { GameOver } from "./scenes/game-over";
我的文件夹结构
index.html
-src
-scenes
game-over.ts
-assets
bmo.png
-dist
bundle.js
为什么我不能使用import
导入图像?
编辑
添加了webpack配置
var path = require('path');
var pathToPhaser = path.join(__dirname, '/node_modules/phaser/');
var phaser = path.join(pathToPhaser, 'dist/phaser.js');
module.exports = {
entry: './src/game.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{ test: /\.ts$/, loader: 'ts-loader', exclude: '/node_modules/' },
{ test: /phaser\.js$/, loader: 'expose-loader?Phaser' },
{ test: /\.(png|jpg|gif)$/, use: [{loader: 'file-loader', options: {
outputPath: 'images',
} }]}
]
},
devServer: {
contentBase: path.resolve(__dirname, './'),
publicPath: '/dist/',
host: '127.0.0.1',
port: 8080,
open: true
},
resolve: {
extensions: ['.ts', '.js'],
alias: {
phaser: phaser
}
}
};