gpt4 book ai didi

webpack - mini-css-extract-plugin 不适用于 webpack-dev-server

转载 作者:行者123 更新时间:2023-12-03 18:01:49 27 4
gpt4 key购买 nike

当我跑 npm run dev mini-css-extract-plugin 捆绑,但当我运行时 npm run server mini-css-extract-plugin 不捆绑。

const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
mode: 'development',

entry: {
app: './resources/index.js'
},

output: {
path: path.resolve(__dirname, 'app/assets/js'),
publicPath: '/assets/js',
filename: 'app.js'
},

devServer: {
contentBase: path.join(__dirname, 'app'),
port: 9000,
},

module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')
],
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},

optimization: {
splitChunks: {
cacheGroups: {
js: {
test: /\.js$/,
name: "commons",
chunks: "all",
minChunks: 7,
},
css: {
test: /\.(css|sass|scss)$/,
name: "commons",
chunks: "all",
minChunks: 2,
}
}
}
},

plugins: [
new MiniCssExtractPlugin({filename: "../css/[name].css"}),
new VueLoaderPlugin()
],

resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
}
};


{
"name": "second",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --mode development",
"pro": "webpack --mode production",
"server": "webpack-dev-server",
"watch": "webpack --watch"
},
....

webpack-dev-server 检测 js 文件中的更改并重新加载页面和 js 功能工作但是当 scss 文件更改时 webpack-dev-server 也会检测到这一点但不捆绑。

我想知道是什么问题?

最佳答案

这是我的 webpack.config.js
你可以尝试删除其他css加载器(sass postcss),制作一个mvd(最小可行的演示),试试吧。
希望对你有帮助

const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
// change to .tsx if necessary
entry: {
app: './src/app.tsx'
},
output: {
filename: '[name].[contenthash].js'
},
resolve: {
alias: {
assets: path.resolve(__dirname, '../src/assets/'),
},
extensions: [".ts", ".tsx", ".js", ".jsx"]
},
module: {
rules: [
{
test: /\.css$/,
use: [
isDevelopment ? 'style-loader' : {
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: '../',
hmr: isDevelopment,
},
},
{
loader: 'css-loader',
options: {
modules: true
}
}
]
},
{
// 图片格式正则
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
{
loader: 'url-loader',
// 配置 url-loader 的可选项
options: {
// 限制 图片大小 9KB,小于限制会将图片转换为 base64格式
limit: 10000,
// 超出限制,创建的文件格式
// build/images/[图片名].[hash].[图片格式]
name: 'images/[name].[hash].[ext]'
}
}
]
},
// changed from { test: /\.jsx?$/, use: { loader: 'babel-loader' }, exclude: /node_modules/ },
{ test: /\.(t|j)sx?$/, use: { loader: 'ts-loader' }, exclude: /node_modules/ },

// addition - add source-map support
{ enforce: "pre", test: /\.js$/, exclude: /node_modules/, loader: "source-map-loader" }
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css',
}),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development', // use 'development' unless process.env.NODE_ENV is defined
DEBUG: false
})
],
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
}
}

关于webpack - mini-css-extract-plugin 不适用于 webpack-dev-server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50460155/

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