css-loader残留的疑问
npm i -D extract-text-webpack-plugin
吐血,在这里卡住了下,果然不多看看就落伍了。请看截图:
意思这里的插件将被另外一个名为mini-css-extract-plugin取代
mini-css-extract-plugin
插件详细使用地址
将第一个插件删除
npm uninstall extract-text-webpack-plugin
安装适合webpack5的插件:
npm install --save-dev mini-css-extract-plugin
配置webpack.config.json如下:
const path = require('path');
// const ExtractText = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV !== 'production';
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
],
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: devMode ? '[name].css' : '[name].[contenthash].css',
chunkFilename: devMode ? '[id].css' : '[id].[contenthash].css',
})
]
};
这个先不管devMode 这个属性,主要是用来判断环境的,开发还是生产的环境。
‘[name].css’ : ‘[name].[contenthash].css’,这个name表示文件名,因为是在mian.js使用的import引入,所以文件是安装依赖的js文件获取的名字,这里是main.css。contenthash是用来防止缓存的(缓存让你摸不清头脑,为什么不生效呢),根据内容生成,可以在后面使用[contenthash:8]表示八位的hash,内容改变才改变,存在一些细节差异,后面再进行补充。
执行打包命令:
npm run build
生成文件如下:
实现了抽离,但是我们的html没有引入,可手动引入。并且html没有经过webpack进行打包,接下来让html也被例如打包的进程。
html打包
安装插件 npm install --save-dev html-webpack-plugin
配置webpack.config.json
const path = require('path');
// const ExtractText = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const devMode = process.env.NODE_ENV !== 'production';
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
],
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: devMode ? '[name].css' : '[name].[contenthash].css',
chunkFilename: devMode ? '[id].css' : '[id].[contenthash].css',
}),
new HtmlWebpackPlugin({
filename: '[name].html'
}),
]
};
这样直接打包会默认生成一个main.html,但是这个html并不是你自己指定的,虽然引入了main.js和main.css,关键是没有自己的html结构,内容。
template: './src/index.html'
来指定打包的文件。minify:{
minimize:false, // 是否为最小
removeAttrbuteQuotes:true, // 是否去除引号
removeComments:true, // 是否去掉注释
collapseWhitespace:false, // 是否去掉空格
minifyCss:true, // 是否压缩css
removeEmptyElements:false, // 是否清理内容为空的元素
},
查看下dist文件夹,发现老的文件依然存在,这个会成为一个很大的问题,没有的文件也在打包文件中,因此我们需要删除。手动删除虽然也是个办法,但是程序员得做点想程序员的事。
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
},
这样就可以直接清除dist目录后再进行打包了。跟记忆中的方式不太一样,是因为它是webpack5啊,在webpack4中需要引入一个插件:clean-webpack-plugin
,引入后在plugins中new一下就好了。