const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const path = require('path')
const baseWebpackConfig = require('./webpack.base.conf')
//一個拷貝資源的插件,后面我會介紹用法
const CopyWebpackPlugin = require('copy-webpack-plugin')
//生成html模板的插件,一個經常用到的wbepack插件
const HtmlWebpackPlugin = require('html-webpack-plugin')
//一個更友好展示錯誤日志的插件
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
//一個自動打開可用端口的包
const portfinder = require('portfinder')
//當前環境的host
const HOST = process.env.HOST
//當前環境的port
const PORT = process.env.PORT && Number(process.env.PORT)
//開發環境的配置
const devWebpackConfig = merge(baseWebpackConfig, {
module: {
//loader的配置,具體內容可以參考utils文件
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
},
// cheap-module-eval-source-map is faster for development
devtool: config.dev.devtool,
devServer: {
//重新加載server時,控制台對一些錯誤以warning的方式提示
clientLogLevel: 'warning',
//當使用 HTML5 History API 時,任意的 404 響應都可能需要被替代為 index.html
historyApiFallback: {
rewrites: [
{ from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
],
},
//啟用 webpack 的模塊熱替換特性
hot: true,
//告訴服務器從哪里提供內容。只有在你想要提供靜態文件時才需要,這里我們禁用
contentBase: false,
//是否壓縮
compress: true,
host: HOST || config.dev.host,
port: PORT || config.dev.port,
//是否自動打開瀏覽器
open: config.dev.autoOpenBrowser,
//編譯出錯時是否有提示
overlay: config.dev.errorOverlay
? { warnings: false, errors: true }
: false,
//靜態內容的路徑,此路徑下的打包文件可在瀏覽器中訪問
publicPath: config.dev.assetsPublicPath,
//接口的代理
proxy: config.dev.proxyTable,
//啟用 quiet 后,除了初始啟動信息之外的任何內容都不會被打印到控制台。這也意味着來自 webpack 的錯誤或警告在控制台不可見。
quiet: true, // necessary for FriendlyErrorsPlugin
//監視文件的選項
watchOptions: {
poll: config.dev.poll,
}
},
plugins: [
//DefinePlugin 允許創建一個在編譯時可以配置的全局常量。這里生成了一個當前環境的常量
new webpack.DefinePlugin({
'process.env': require('../config/dev.env')
}),
//模塊熱替換插件,修改模塊時不需要刷新頁面
new webpack.HotModuleReplacementPlugin(),
//當開啟 HMR 的時候使用該插件會顯示模塊的相對路徑
new webpack.NamedModulesPlugin(),
//在編譯出現錯誤時,使用 NoEmitOnErrorsPlugin 來跳過輸出階段。這樣可以確保輸出資源不會包含錯誤。
new webpack.NoEmitOnErrorsPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
//打包后js文件放在body的最后
inject: true
}),
//將static的內容拷貝到開發路徑,忽略這個文件夾下“.XX”的文件
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
])
]
})