首先在package.json下有
开发环境下运行这条命令
"dev": "node build/dev-server.js",
运行build下的dev-server.js
这个相当于自定义一个server 利用webpack-dev-middleware配合express来自动生成 自己写一个server.js
主要就是导入一些webpack.dev.conf或者config里面的一些生产环境下的配置 来自定义服务器
require('./check-versions')()
var config = require('../config')
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV) //如果没有设置为生产模式
}
var opn = require('opn')
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// automatically open browser, if not set will be false
var autoOpenBrowser = !!config.dev.autoOpenBrowser
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var proxyTable = config.dev.proxyTable
var app = express()
var compiler = webpack(webpackConfig)
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true //向控制台显示任何内容
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: () => {}
})
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
hotMiddleware.publish({ action: 'reload' })
cb()
})
})
// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
var options = proxyTable[context]
if (typeof options === 'string') {
options = { target: options }
}
app.use(proxyMiddleware(options.filter || context, options))
})
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(devMiddleware)
// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)
// serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))
var uri = 'http://localhost:' + port
var _resolve
var readyPromise = new Promise(resolve => {
_resolve = resolve
})
console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + '\n')
// when env is testing, don't need open it
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})
var server = app.listen(port)
module.exports = {
ready: readyPromise,
close: () => {
server.close()
}
}
var compiler = webpack(webpackConfig) ;webpackConfig就是在生产环境下的webpack的配置文件 这个里面合并了公共配置文件和生产环境下的专有配置文件 这里就是传入config重新编译一次。
entry: {
app: './src/main.js'
},
打包文件入口为这个路径下的文件 chunkName为app
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'development'
? config.dev.assetsPublicPath
: config.build.assetsPublicPath
},
path:打包后的文件存放在路径 这里找到之后是在dist下
filename: 则是生成文件的名字
publicPath:打包的时候,webpack会在静态文件路径前面添加publicPath的值,当我们把资源放到CDN上的时候,把publicPath的值设为CDN的值就可以了
静态资源最终访问路径 = output.publicPath + 资源loader或插件等配置路径
在这里开发环境下静态访问路径为‘/’ 即根路径下的 生成环境下静态访问路径为‘./’ 即当前路径下的
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'@': resolve('src')
},
},