我对Webpack 4还是陌生的,并且努力地弄清楚为什么我不能提供CSS文件。
我基本上是使用以下命令启动我的开发服务器
npm start
然后,我可以使用
http://localhost:9000/js/app.bundle.js
但是当我尝试对我的css文件执行相同操作时,它会显示404
http://localhost:9000/css/app.css
我的完整配置是
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
mode: 'development',
entry: {
app: ['./src/js/main.js','./src/scss/style.scss'],
admin: ['./src/js/admin.js', './src/scss/admin.scss']
},
output: {
filename: 'js/[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('autoprefixer')
];
}
}
},
{
loader: 'sass-loader',
}
]
},
{
test: /\.(svg|jpg|png|gif)$/,
use: {
loader: 'file-loader',
options: {
name: 'assets/img/[name].[ext]',
}
}
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: {
loader: 'url-loader',
options: {
name: 'assets/fonts/[name].[ext]',
}
}
},
],
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: "/css/[name].css",
}),
new CopyPlugin([
{from: './src/img', to: 'img'},
{from: './src/icon', to: 'icon'},
{from: './src/fonts', to: 'fonts'},
]),
],
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
port: 9000
},
};
当我运行npm run build
时,我在正确的子目录中看到文件
即
/dist/css/app.css
/dist/css/admin.css
/dist/js/app.bundle.js
/dist/js/admin.bundle.js
如果我将CleanWebpackPlugin()
移出npm run build
,则npm start
将提供文件,但是我不明白为什么当它在内存中运行时它不能提供此服务。
它也可以进行以下更改
new MiniCssExtractPlugin({
filename: "/css/[name].css", Remove this
filename: "[name].css" Add this
}),
但是我没有得到想要的子目录结构。
答案 0 :(得分:0)
看来我所拥有的一切都还可以,我只需要删除/
例如
new MiniCssExtractPlugin({
filename: "/css/[name].css", replace this line
filename: "css/[name].css" with this line
}),