接到上一章,如何搭建webpack4的开发调试,如果有没有了解的可以去看看:https://www.jianshu.com/p/be44baced73b
接到上一章的配置
webpakc.config.js
const webpack = require('webpack'); const path = require('path'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const srcDir = path.join(__dirname, './src'); const distDir = path.join(__dirname, './dist'); module.exports = { entry: { index: [ 'webpack/hot/dev-server', 'webpack-dev-server/client?http://localhost:80', "./src/js/index.js" ] }, output: { path: path.resolve(__dirname, 'dist'),
webpack.dev.service.js
var WebpackDevServer = require("webpack-dev-server"); var webpack = require("webpack"); var webpackConfig = require('./webpack.config.js'); var compiler = webpack(webpackConfig); var bird = require('birdv3'); var server = new WebpackDevServer(compiler, { watchContentBase: true, disableHostCheck: true,
pathRewrite: {
'^/api': ''//一般不会重写
}
注意disableHostCheck 、 allowedHosts是允许绑定本地Host域名的
proxy 是允许指定接口调用直接使用服务端接口,需要服务端支持代理,避免以后每次开发都要解决跨域问题
PS: 如果不喜欢使用webpack自带的proxy,也可以使用birdv3,这也是一个服务端代理框架。个人认为使用webpack已经能完全满足日常开发需求,但是如果有需要birdv3的可以找我!这里就不详述怎么使用birdv3了,谢谢
附上github地址:https://github.com/majianguang/h5Base