好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了
01.初始化项目(安装需要的包) //生成package.json npm init 安装基础包 npm install react react-dom --save npm install webpack@3.10.0 webpack-dev-server@2.7.1 --save-dev <!--webpack和webpack-dev-server同时全局安装一下--> npm install webpack@3.10.0 wepack-dev-server@2.7.1 -gwebpack-dev-server@2.7.3是最后兼容ie9的版本,升级之后会导致打包的文件在ie9下运行报错
安装和配置babel npm install babel-core 核心功能 babel-loader babel-polyfill 转换低版本代码 babel-preset-env 解析Es6 babel-preset-react 解析JSX --save-dev 安装loader包 npm install css-loader style-loader file-loader url-loader image-webpack-loader html-loader --save-dev
--save 会把依赖包名称添加到 package.json 文件 dependencies
dependencies是运行时依赖,是我们发布后还需要依赖的模块
--save-dev 则添加到 package.json 文件 devDependencies
devDependencies是开发时的依赖,在发布后用不到它,而只是在我们开发才用到它
当npm install时,会下载dependencies和devDependencies中的模块,当使用npm install –production或者注明NODE_ENV变量值为production时,只会下载dependencies中的模块
02.执行程序,创建文件 创建/.babelrc //.babelrc { "presets": [ "react", "env" ] } 创建webpack.config.js const path = require('path'); module.exports = { context: path.resolve(__dirname, 'app'), resolve: { extensions: ['.js', '.jsx'], //后缀名自动补全,可以使用jsx的文件后缀 modules: [ path.resolve(__dirname, 'node_modules') ] }, entry: 'http://www.likecs.com/app.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'app.js', }, module: { rules: [ { test: /\.(js|jsx)$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader', options: { presets: ['env', 'react'], } } }, { test: /index\.html/, use: [{ loader: 'file-loader', options: { name (file) { return 'index.[ext]' } } }] } ] }, }; 创建/app/component/App.js import React from 'react'; import Header from './Header'; import Content from './Content'; import Sidebar from './Sidebar'; class App extends React.Component { render() { return ( <div className="app"> <Header/> <Sidebar/> <Content/> </div> ) } } export default App; 创建/app/component/Header.js import React from 'react'; class Header extends React.Component { render() { return ( <div>this is header</div> ) } } export default Header; 创建/app/component/Sidebar.js import React from 'react'; class Sidebar extends React.Component { render() { return ( <div className="sidebar"> this is sidebar </div> ) } } export default Sidebar; 创建/app/component/Content.js import React from 'react'; class Content extends React.Component { render() { return ( <div className="content"> this is content </div> ) } } export default Content; 创建/app/app.js入口文件 import './index.html'; import React from 'react'; import ReactDom from 'react-dom'; import App from './component/App'; ReactDom.render( <App/>, document.getElementById('root') ); 创建/app/index.html <!DOCTYPE html> <html lang="en"> <head> <base href="http://www.likecs.com/"> <meta charset="UTF-8"> <title>hello world</title> </head> <body> <div></div> <script src="http://www.likecs.com/app.js"></script> </body> </html> 执行webpack打包 <!--控制台执行webpack会在/dist中生成一个app.js和index.html--> webpack 03.开启本地服务器 修改webpack.config.js //webpack-dev-server配置 devServer: { contentBase: './dist', historyApiFallback: true, inline: true,//源文件改变,会自动刷新页面 port: 1234,//设置默认监听端口,如果省略,默认为"8080" }, 修改package.json "scripts": { "start": "webpack-dev-server" }, 执行start npm start 03.添加css样式 新建/style/app.css html, body, #root, .app { width: 100%; height: 100%; margin: 0; padding: 0; } div { box-sizing: border-box; } .header { position: fixed; top: 0; left: 150px; right: 0; padding: 0 30px; height: 61px; background: #ebebeb; } .title { width: 90%; margin: 0 5%; height: 80px; line-height: 80px; color: #fff; font-size: 20px; text-align: center; border-bottom: 1px solid rgba(255, 255, 255, 0.2); } .sidebar { position: fixed; top: 0; bottom: 0; left: 0; width: 150px; background: #3d3c4a; } .header-con { position: relative; width: 100%; height: 60px; } .user { position: absolute; right: 0; top: 15px; height: 30px; line-height: 30px; color: gray; } .text { float: left; height: 30px; } .avatar { float: right; width: 30px; height: 30px; } .avatar img { width: 100%; height: 100%; border-radius: 90%; } .content { position: fixed; top: 60px; left: 150px; right: 0; bottom: 0; padding: 30px; color: gray; } 修改/app/component/Header.js import React from 'react'; class Header extends React.Component { render() { return ( <div className="header"> <div className="header-con"> <div className="user"> 欢迎您! </div> </div> </div> ) } } export default Header; 修改/app/component/Sidebar.js import React from 'react'; class Sidebar extends React.Component { render() { return ( <div className="sidebar"> <div className="title"> easterCat </div> </div> ) } } export default Sidebar; 修改/app/component/App.js ... import '../style/app.css'; 添加css依赖/webpack.config.js module: { rules: [ { test: /\.css$/, use: [{ loader: 'style-loader' }, { loader: 'css-loader' }], }, ] }, 打包运行 webpack