使用當前的webpack配置能不能打包構建項目呢?固然能夠,但這不是咱們想要的,因此,讓咱們來看一看生產環境須要怎麼配置webpack吧javascript
webpack.pro.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
module.exports = {
entry:{
main:['babel-polyfill','./src/index.js'],
vendors: ['react','react-dom','react-router-dom','whatwg-fetch']
}
,
output:{
path:path.resolve(__dirname,'dist'),
filename:'bundle.[hash:4].js'
},
module:{
rules:[
{
test: /\.(woff|eot|ttf|svg|png|jpg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: '1024' ,
name: '[name].[hash:4].[ext]'
}
},
]
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: '1024',
name: '[name].[hash:4].[ext]'
}
},
]
},
{
test: /\.(css|less)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ["css-loader","less-loader"]
})
},
{
test:/\.(js|jsx)$/,
use:"babel-loader",
exclude:/node_modules/
}
]
},
devtool: false,
plugins:[
// html 模板插件
new HtmlWebpackPlugin({template:'./src/index.html',favicon: './public/favicon.png'}),
// 啓用做用域提高,讓代碼文件更小、運行的更快
new webpack.optimize.ModuleConcatenationPlugin(),
// 提取公共代碼vendors
new webpack.optimize.CommonsChunkPlugin({
name:'vendors',
filename:'[name].[hash:4].js'
}),
// 抽離出css
new ExtractTextPlugin("style.css"),
// 壓縮js代碼
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
pure_funcs: ['console.log']
}
}),
// 定義全局常量
new webpack.DefinePlugin({
"process.env": {
"NODE_ENV": JSON.stringify("production")
}
}),
// 加署名
new webpack.BannerPlugin('Copyright by Zero https://github.com/l-Lemon/blog')
]
}
複製代碼
在package.json
的 script 中加入css
"build": "webpack --config webpack.pro.config.js"
複製代碼
運行 npm run build
根目錄會生成 dist
文件夾 和壓縮後的代碼。html
webapck.base.js
文件來存咱們公共配置const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
// 抽離css
const extractCss = new ExtractTextPlugin("style.css")
// html 模版
const htmlTemplate = new HtmlWebpackPlugin({template:'./src/index.html',favicon: './public/favicon.png'})
const config = {
output:{
path:path.resolve(__dirname,'dist'),
filename:'bundle.[hash:4].js'
},
module:{
rules:[
{
test: /\.(woff|eot|ttf|svg|png|jpg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: '1024' ,
name: '[name].[hash:4].[ext]'
}
},
]
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: '1024',
name: '[name].[hash:4].[ext]'
}
},
]
},
{
test: /\.(css|less)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ["css-loader","less-loader"]
})
},
{
test:/\.(js|jsx)$/,
use:"babel-loader",
exclude:/node_modules/
}
]
},
}
module.exports = {
htmlTemplate,
extractCss,
config
}
複製代碼
webpack.config.js
爲const path = require('path')
const baseConfig = require('./webpack.base')
module.exports = {
entry:{
main:['babel-polyfill','./src/index.js'],
},
...baseConfig.config,
plugins:[
baseConfig.htmlTemplate,
baseConfig.extractCss
],
devServer:{
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 9000,
proxy: {
"/api": {
target: "http://127.0.0.1:3000/",
pathRewrite: {"^/api" : ""}
}
}
}
}
複製代碼
webpack.pro.config.js
爲const webpack = require('webpack')
const baseConfig = require('./webpack.base')
module.exports = {
entry:{
main:['babel-polyfill','./src/index.js'],
// 將第三方庫包單獨打包,充分利用瀏覽器緩存
vendors: ['react','react-dom','react-router-dom','whatwg-fetch']
},
...baseConfig.config,
devtool: false,
plugins:[
// html 模板插件
baseConfig.htmlTemplate,
// 啓用做用域提高,讓代碼文件更小、運行的更快
new webpack.optimize.ModuleConcatenationPlugin(),
// 提取公共代碼vendors
new webpack.optimize.CommonsChunkPlugin({
name:'vendors',
filename:'[name].[hash:4].js'
}),
// 抽離出css
baseConfig.extractCss,
// 壓縮js代碼
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
pure_funcs: ['console.log']
}
}),
// 定義全局常量
new webpack.DefinePlugin({
"process.env": {
"NODE_ENV": JSON.stringify("production")
}
}),
// 加署名
new webpack.BannerPlugin('Copyright by Zero https://github.com/l-Lemon/blog')
]
}
複製代碼
好了,如今咱們再來試試npm run dev
和npm run build
命令,沒問題均可以完美運行。java
這篇文章咱們介紹了生產環境webpack的配置,而且抽出了公共配置,重構了開發環境和生產環境的配置。node
下篇咱們來介紹實現單元測試react
React項目架構webpack