1、webpack-dev-server

npm install -D webpack-dev-server
把数据都写在了内存里,而不是硬盘的dist目录

基本配置

  1. "scripts": {
  2. "test": "echo \"Error: no test specified\" && exit 1",
  3. "build": "webpack --mode development",
  4. "serve": "webpack serve --mode development"
  5. }

webpack-dev-middleware(了解)

这是一个容器,把webpack处理后的文件传递给一个服务器,webpack-dev-server在内部使用了它,也可以进行自定义配置
npm install --save-dev express webpack-dev-middleware

  1. const express = require('express')
  2. const webpackDevMiddleware = require('webpack-dev-middleware')
  3. const webpack = require('webpack')
  4. const app = express()
  5. // 获取配置文件
  6. const config = require('./webpack.config.js')
  7. const compiler = webpack(config)
  8. app.use(webpackDevMiddleware(compiler))
  9. // 开启端口上的服务
  10. app.listen(3000, () => {
  11. console.log('服务运行在 3000端口上')
  12. })

2、HMR

只对局部发生数据变化的组件进行更新展示,在webpack.config.js中配置devServer

  1. module.exports = {
  2. mode: 'development',
  3. devtool: false,
  4. entry: './src/index.js',
  5. output: {
  6. filename: 'js/main.js',
  7. path: path.resolve(__dirname, 'dist')
  8. },
  9. target: 'web',
  10. devServer: {
  11. hot: true
  12. }
  13. }
  1. import './title'
  2. if (module.hot) {
  3. module.hot.accept(['./title.js'], () => {
  4. console.log('title.js模块更新')
  5. })
  6. }

react热更新

npm install react-hot-loader
https://github.com/gaearon/react-hot-loader

  1. {
  2. "plugins": ["react-hot-loader/babel"]
  3. }
  1. import { hot } from 'react-hot-loader/root';
  2. const App = () => <div>Hello World!</div>;
  3. export default hot(App);

或者
npm i -D @pmmmwh/react-refresh-webpack-plugin react-refresh

  1. const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
  2. plugins: [
  3. new ReactRefreshWebpackPlugin()
  4. ]
  1. module.exports = {
  2. plugins: [
  3. ['react-refresh/babel']
  4. ]
  5. }

Vue热更新(处理vue文件)

vue-loader
webpack里面处理Vue文件的时候就处理了热更新
npm install -D vue-loader vue-template-compiler
或者
npm i vue-loader@14 vue-template-compiler -D
vue3 使用正常版本
vue2 使用16版本之前
如果是vue-cli,不需要额外安装,热更新是开箱即用
14版本的vue-loader不需要写plugin

  1. const { VueLoaderPlugin } = require('vue-loader')
  2. module.exports = {
  3. module: {
  4. rules: [
  5. // ... 其它规则
  6. {
  7. test: /\.vue$/,
  8. loader: 'vue-loader'
  9. }
  10. ]
  11. },
  12. plugins: [
  13. new VueLoaderPlugin()
  14. ]
  15. }

3、path

  1. output: {
  2. filename: 'js/main.js',
  3. path: path.resolve(__dirname, 'dist'),
  4. publicPath: '/lg'
  5. },
  6. devServer: {
  7. hot: true,
  8. publicPath: '/lg',
  9. contentBase: path.resolve(__dirname, 'public'),
  10. watchContentBase: true
  11. }

output中的:这是打包之后的资源输出目录

  • publicPath:index.html内部的引用路径,域名+ publicPath + filename
  • publicPath如果是’./‘,就变成了相对路径,devServer开启服务不能找到资源,默认情况是空的
  • publicPath如果是’/‘,就是绝对路径,本地打包的无法找到

devServer中的:

  • publicPath: 指定本地服务所在的目录
  • contentBase: 我们打包之后的资源如果说依赖其它的资源,此时就告知去哪找
  • watchContentBase:监听被依赖的未打包其他资源更改

output和devServer里的publicPath要保持一致,devServer里的设置了之后,访问本地服务器要加上设置的值,localhost:8080/lg
如果output不加,则会访问不到打包出来的js

4、devServer常用配置

  1. devServer: {
  2. hot: true,
  3. hotOnly: true,
  4. port: 4000,
  5. open: false,
  6. compress: true,
  7. historyApiFallback: true
  8. }

hotOnly:修复语法错误等报错后,只更新修改的地方,而不是整体刷新
port:指定端口
open:启动服务时是否打开浏览器
compress:服务端开始gzip 压缩打包的文件
historyApiFallback:history路由模式下,刷新页面路径请求不到页面404,设置为true,页面替换为index.html,也可以传入对象,配置rewrites属性

5、proxy代理

  1. devServer: {
  2. hot: true,
  3. proxy: {
  4. // /api/users
  5. // http://localhost:4000/api/users
  6. // https://api.github.com/api/users
  7. '/api': {
  8. target: 'https://api.github.com',
  9. pathRewrite: { "^/api": "" },// http://localhost:8080/api/users -> https://api.github.com/users
  10. changeOrigin: true//修改host为代理的
  11. }
  12. }
  13. }

6、resolve模块解析原则

配置模块如何解析

  1. resolve: {
  2. extensions: [".js", ".json", '.ts', '.jsx', '.vue'], //导入模块少写后缀名
  3. alias: {
  4. '@': path.resolve(__dirname, 'src')
  5. }
  6. }


source-map和之前的一样

7、编译TS

npm i ts-loader -D

  1. {
  2. test: /\.ts$/,
  3. use: ['ts-loader']
  4. }

但是无法处理ts里面写es6新的语法,使用babel-loader来处理TS
npm i @babel/preset-typescript -D

  1. {
  2. test: /\.ts$/,
  3. use: ['babel-loader']
  4. }
  1. module.exports = {
  2. presets: [
  3. ['@babel/preset-env', {
  4. useBuiltIns: 'usage',
  5. corejs: 3
  6. }],
  7. ['@babel/preset-typescript']
  8. ]
  9. }

但是编译时会忽略一些ts语法错误
所以可以在packag.json里新增编译命令,确定没问题了再打包

  1. "scripts": {
  2. "test": "echo \"Error: no test specified\" && exit 1",
  3. "build": "npm run ck && webpack",
  4. "serve": "webpack serve",
  5. "ck": "tsc --noEmit"
  6. }