Eze*_*wei 0 webpack webpack-2 webpack-file-loader
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader?name=img/img-[hash:6].[ext]',
},
Run Code Online (Sandbox Code Playgroud)
我将此配置添加到中以通过将上述规则添加到中webpack.config.js来使用file-loader
module: {
rules: [
Run Code Online (Sandbox Code Playgroud)
部分,但没有任何图像文件移动到dist/文件夹,即使我将图像文件放入src/images/. 我是否遗漏了一些需要添加到 webpack 配置中的内容?
我正在遵循https://julienrenaux.fr/2015/03/30/introduction-to-webpack-with-practical-examples/中的示例
顺便说一句,我正在使用 webpack 2...现在有不同的方法来配置文件加载器吗?
这是整个 webpack 配置文件
const { resolve } = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const config = {
devtool: 'cheap-module-eval-source-map',
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./main.js',
'./assets/scss/main.scss',
],
output: {
filename: 'bundle.js',
path: resolve(__dirname, 'dist'),
publicPath: '/',
},
context: resolve(__dirname, 'app'),
devServer: {
hot: true,
contentBase: resolve(__dirname, 'dist'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.js$/,
loaders: [
'babel-loader',
],
exclude: /node_modules/,
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
'css-loader',
{
loader: 'sass-loader',
query: {
sourceMap: false,
},
},
],
}),
},
{ test: /\.(png|jpg)$/, use: 'url-loader?limit=15000' },
{ test: /\.eot(\?v=\d+.\d+.\d+)?$/, use: 'file-loader' },
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
{ test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/, use: 'url-loader?limit=10000&mimetype=application/octet-stream' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=image/svg+xml' },
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader?name=img/img-[hash:6].[ext]',
},
],
},
plugins: [
new ExtractTextPlugin({ filename: 'style.css', disable: false, allChunks: true }),
new CopyWebpackPlugin([{ from: 'vendors', to: 'vendors' }]),
new OpenBrowserPlugin({ url: 'http://localhost:8080' }),
new webpack.HotModuleReplacementPlugin(),
],
};
module.exports = config;
Run Code Online (Sandbox Code Playgroud)
使用以下代码片段要求内部src/images/(或图像所在的任何位置)下的所有文件main.js:
// load assets
function requireAll(r) { r.keys().forEach(r); }
requireAll(require.context('src/images/', true));
Run Code Online (Sandbox Code Playgroud)
这将使 Webpack 处理位于该文件夹内的所有文件。
| 归档时间: |
|
| 查看次数: |
5095 次 |
| 最近记录: |