原创不易,未经作者允许禁止转载!!
为什么要搭建本地服务器
npm run build
编译npm run build
module.exports = {
entry: "./src/index.js",
output: {
filename: "js/bundle.js",
path: path.resolve(__dirname, "build"),
},
watch:true,
}
npm script:{
"watch": "webpack --watch"
}
# npm run watch
npm install --save webpack-dev-server
script:{
"serve":"webpack serve"
}
npm install --save express webpack-dev-middleware
const express = require("express")
const webpack = require("webpack")
const webpackDevMiddleware = require("webpack-dev-middleware")
const app = express()
const config = require("./webpack.config")
const compiler = webpack(config)
app.use(webpackDevMiddleware(compiler,{
publicPath:config.output.publicPath
}),()=>{
console.log("这里是回调函数")
})
app.listen(3000,()=>{
console.log("Server running")
})
proxy是我们开发中常用的一个配置选项,它的目的设置代理来解决跨域访问的问题
devServer: {
hot: true,
hostOnly:true,
host:"0.0.0.0",
port:8080,
open:true,
compress:true,
proxy:{
"/api":{
target:"http://localhost:8888",
pathRewrite:{
"^/api":""
},
secure:false,
changeOrigin:true
}
}
}
module.exports = {
entry: "./src/index.js",
output: {
filename: "js/bundle.js",
path: path.resolve(__dirname, "build"),
},
watch:true,
mode: "development",
devServer:{
hot:true
},
}
if(module.hot){
module.hot.accept("./App.vue",()=>{
console.log("vue更新了")
})
}
有一个问题:在开发其他项目时,我们是否需要经常手动去写入 module.hot.accpet相关的API呢?
npm install vue-loader vue-template-compiler
const VueLoaderPlugin = require("vue-loader/lib/plugin")
module: {
rules: [
{
test: /\.vue$/,
use: ["vue-loader"]
},
]
},
plugins:[new VueLoaderPlugin()]
npm install @pmmmwh/react-refresh-webpack-plugin react-refresh
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin")
plugins: [
new ReactRefreshWebpackPlugin(),
],
module.exports = {
presets: [
["@babel/preset-env", {
useBuiltIns: "usage",
corejs: 3.8
}],
["@babel/preset-react"],
["@babel/preset-typescript"]
],
plugins: [
['react-refresh/babel']
]
}
image-20210502184548214