时间:2021-05-25
前端工程化开发的一个重要标志就是热替换技术,它大大的提高开发效率,使我们专注于写代码,webpack2中的热替换相比较1更加简洁。
1. 先看效果
2.目录结构
3.项目目录结构文件描述
bin 执行文件
node_modules node包
public 静态资源文件
src 项目js文件
.bablrc babel配置文件
webpack.config.dev.js开发模式webpack配置
webpack.config.pro.js生产模式webpack配置
3.技术依赖
babel-loader,css-loader,sass-loader,style-loader,webpack-hot-middleware,webpack-hot-middleware
热加载最重要的几个配置是packge.json/devserver.js/webpack.config.dev.js/webpack.config.prod.js/.babelrc
packge.json
{ "name": "react-hmr-demo", "version": "1.0.0", "description": "react-hmr-demo", "main": "index.js", "scripts": { "dev": "node bin/devserver.js", "build":" webpack --config webpack.config.prod.js ", "start":"node bin/server.js" }, "repository": { "type": "git", "url": "git+https://github.com/leinov/react-hmr-demo.git" }, "author": "leinov", "license": "ISC", "dependencies": { "express": "^4.15.4", "react": "^15.6.1", "react-dom": "^15.6.1" }, "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", "babel-preset-react-hmre": "^1.1.1", "css-loader": "^0.28.5", "eventsource-polyfill": "^0.9.6", "node-sass": "^4.5.3", "sass-loader": "^6.0.6", "style-loader": "^0.18.2", "webpack": "^3.5.5", "webpack-dev-middleware": "^1.12.0", "webpack-hot-middleware": "^2.18.2" }}scripts的几个命令
webpack.config.dev.js
var path = require('path');var webpack = require('webpack'); module.exports = { devtool: 'cheap-module-eval-source-map',//打包构建信息 entry: [ 'eventsource-polyfill',//for IE 'webpack-hot-middleware/client',//webpack服务连接到浏览器接收更新 './src/index.js' ], output: { filename: 'boundle.js', publicPath: '/dist/js/' }, module: { loaders: [{ test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ //哪些文件下的需要用到babel }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }, { test: /\.scss$/, loaders: ['style-loader', 'css-loader', 'sass-loader'], }, { test: /\.(png|jpg|gif)$/, loaders: ['url?limit=8192&name=images/[name].[ext]'], } ] } , resolve:{ alias: { css: path.resolve(__dirname, 'public/static/css/'), //css目录别名 } }, plugins: [ new webpack.HotModuleReplacementPlugin(),//热替换插件 new webpack.NoEmitOnErrorsPlugin() ]};.babelrc
devserver.js
这样就可以实现热加载了,在项目根目录执行
webpack.config.prod.js
生产的webpack主要完成开发后的编译打包,在plugins里多了压缩插件,在项目根目录执行
bin/server.js
生产环境启动文件
这里是项目地址 https://github.com/leinov/react-hmr-demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了react-webpack2-热模块替换[HMR],分享给大家,具体如下:模块热替换功能会在应用程序运行过程中替换、添加或删除模块,而无需重新加载页面
环境主要依赖版本webpack@4.8.1webpack-cli@2.1.3webpack-dev-server@3.1.4react@16.3.2babel-
本文介绍了Angular4实现热加载开发,分享给大家,具体如下:安装插件npminstall@angularclass/hmr-D配置插件//main.tsif
本文介绍了webpack实现热加载自动刷新的方法,分享给大家,具体如下:一、webpack-dev-server一个轻量级的服务功能:修改代码及时呈现到浏览器上
简要介绍:在React-router中,暴露了3个接口,如果结合webpack的codesplitting,就通过切换路由实现按需加载。1.webpack的co