gpt4 book ai didi

webpack - 无法使用 Webpack 文件加载器加载图像

转载 作者:行者123 更新时间:2023-12-01 00:22:02 25 4
gpt4 key购买 nike

根据file-loader usage documentation我应该能够加载如下图像:

test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]

后来通过:
import img from './file.png';

这是我的 webpack.config.js :
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
{
test: /\.glsl$/,
loader: 'webpack-glsl-loader'
},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
options: { failOnHint: true }
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
entry: {
app: './src/index.ts'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Output Manager'
})
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
}
};

这就是我创建 dev-server 的方式:
const app = express();

const compiler = webpack(webpackConfig);

app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
}));

app.listen(3000, () => {
console.log('Listening on port 3000!\n');
});

进一步我的 src结构是这样的:
src
|-images
| |- image.gif
|
|-app.ts

我正在尝试将图像加载到 app.ts如下:
import img from './images/image.gif';

// ...

private loadImage(): void {
const image = new Image();
image.src = img;
image.onload = () => {

};
}

不幸的是我得到:

GET http://localhost:8080/undefined 404 (Not Found)



在 Chrome 的开发工具导航器中,我可以在 webpack://./src/images/image.gif 下看到图像文件.内容如下所示:
module.exports = __webpack_public_path__ + "9b626fa0956bfa785b543ef53e895d5e.gif";


//////////////////
// WEBPACK FOOTER
// ./src/images/image.gif
// module id = 109
// module chunks = 0

另外,如果我将鼠标悬停在 img调试时变量我可以看到名称字符串 "/9b626fa0956bfa785b543ef53e895d5e.gif" .记录变量返回 undefined虽然和 image.onload永远不会触发。

如果我调用 http://localhost:8080/9b626fa0956bfa785b543ef53e895d5e.gif在浏览器中我可以看到图像。

知道为什么我无法加载图像,尽管我可以在浏览器中看到它并且路径字符串似乎在那里?

编辑#1:

当我使用 TypeScript 时,我必须声明一个模块才能导入 .gif文件:
declare module "*.gif" {
const content: any;
export default content;
}

然后我发现了这个 Github issue问题的解决方法是导入文件,如 import * as styles from "styles.scss" .所以我尝试像这样导入:
import * as img from './images/img.gif';

不幸的是,这导致:

error TS2322: Type 'typeof "*.gif"' is not assignable to type 'string'.

最佳答案

我在这里看到两个问题。首先,您应该尝试加载图像文件,例如 import * as img from './images/img.gif'; .您还必须在某处使用图像才能实际加载它。单独导入不会触发加载文件。但是一个简单的console.log(img);应该触发它。

第二个问题是你得到这个异常的原因:

error TS2322: Type 'typeof "*.gif"' is not assignable to type 'string'.



您应该尝试包装导入文件的实际用法,例如:
if (typeof img === 'string') {
console.log(img);
}

这样就不会触发异常。

我希望这有帮助。

关于webpack - 无法使用 Webpack 文件加载器加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com