【发布时间】:2023-03-19 18:23:02
【问题描述】:
我正在学习 CSS 课程,已经厌倦了手动输入类似的 HTML 结构,所以我连接了 pug 模板和 webpack-dev-server 来提供它。
注意:我的入口点是 .scss 文件而不是 .js
我有两个问题:
a) 只有来自 css 的资源会被加载到 build 文件夹,但 .pug 模板中的 src 属性中引用的图像和 svg 会被忽略。
b) 浏览器重新加载在webpack-dev-server 中不起作用
我的webpack.config.js:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: './sass/main.scss',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'css/style.css',
},
mode: 'development',
module: {
rules: [
{
test: /\.pug$/,
use: ['pug-loader'],
},
{
test: /\.(css|sass|scss)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
},
{
loader: 'sass-loader',
},
]
})
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
// {
// loader: 'file-loader',
// options: {
// name: 'img/[name].[ext]',
// }
// },
{
loader: 'url-loader',
options: {
limit: 10000,
publicPath: "../",
name: 'img/[name].[ext]',
}
}
],
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: './test.pug',
filename: 'index.html',
favicon: 'img/favicon.png',
cache: false,
}),
new ExtractTextPlugin('css/style.css'),
],
devServer: {
contentBase: path.join(__dirname, 'build'),
compress: true,
open: 'firefox',
port: 8080,
hot: true,
},
};
请指教。
【问题讨论】:
标签: webpack sass html-webpack-plugin pug-loader