如何压缩自己的css和js,请问webpack如何压缩css和js,求具体例子代码,谢谢。

257043e773be206e57c63b19ff0c1a72.png

小女王的骑士

可以用UglifyJsPlugin压缩js和css,如果需要需要单独把css提取出来,可以用extract-text-webpack-pluginconst path = require("path");

const htmlWP = require("html-webpack-plugin");

const webpack = require("webpack");

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

module.exports = {

entry: "./src/script/main.js",

output: {

path: path.join(__dirname + "/dist"),

filename: "js/bundle.js"

},

module: {

rules: [{

test: /\.css$/,

use: [

"style-loader",

"css-loader"

]

},

{

test:/\.(png|svg|jpg|gif)$/,

use:[

"file-loader"

]

},

{

test:/\.js$/,

use:[

"babel-loader"

],

exclude: path.join(__dirname + "/node_modules"),

include: path.join(__dirname + "/src")

}],

},

plugins: [

new htmlWP({

filename: "index.html",

template: "index.html",

minify: {

collapseWhitespace: false,

removeComments: true

}

}),

new webpack.LoaderOptionsPlugin({

options: {

postcss: [require("autoprefixer")({

browsers: ["last 5 versions"]

})]

}

}),

new UglifyJsPlugin({

compress: {

warnings: false

}

}),

new webpack.DefinePlugin({

"process.env": {

NODE_ENV: JSON.stringify("production")

}

})

]

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值