extract-text-webpack-plugin该插件的主要是为了抽离css样式,防止将样式打包在js中引起页面样式加载错乱的现象;首先我先来介绍下这个插件的安装方法:
for webpack 2
npm install --save-dev extract-text-webpack-plugin
for webpack 1
npm install --save-dev extract-text-webpack-plugin@1.0.1
首先进入项目的根目录,然后执行以上命令进行插件的安装,插件安装完成后,接下来我们要做的就是在webpack.config.js中引入该插件
module.exports = {
module: {
rules: [
{
test: /.css$/,use: ExtractTextPlugin.extract({
fallback: "style-loader",use: "css-loader"
})
}
]
},plugins: [
new ExtractTextPlugin("styles.css"),]
}
module: {
rules: [
{
test: /.css$/,use: ExtractTextPlugin.extract({
fallback: "style-loader",use: "css-loader"
})
}
]
},plugins: [
new ExtractTextPlugin("styles.css"),]
}
// Create multiple instances
const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css');
const extractLESS = new ExtractTextPlugin('stylesheets/[name]-two.css');
const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css');
const extractLESS = new ExtractTextPlugin('stylesheets/[name]-two.css');
module.exports = {
module: {
rules: [
{
test: /.css$/,use: extractCSS.extract([ 'css-loader','postcss-loader' ])
},{
test: /.less$/i,use: extractLESS.extract([ 'css-loader','less-loader' ])
},]
},plugins: [
extractCSS,extractLESS
]
};
该插件有三个参数意义分别如下