webpack+react配置
2016-12-16 17:05
344 查看
安装依赖项 在项目目录下 ,初始化一个 package.json 文件,执行: npm init 安装 react 和 react-dom 依赖: npm install react react-dom --save 安装 webpack 和 webpack-dev-server 依赖: npm install webpack webpack-dev-server --save-dev 安装 babel 依赖: npm install babel-loader babel-core babel-preset-react babel-preset-es2015 --save-dev项目结构:webpack.config.js
/** * Created by joesbell on 2016/12/16. */ var webpack=require("webpack"); var path = require('path'); var appPath=path.resolve(__dirname, './app/main.js'); var buildPath= path.resolve(__dirname, './build'); module.exports = { entry:[ 'webpack/hot/dev-server', 'webpack-dev-server/client?http://localhost:8080', appPath ], output: { path:buildPath, filename: 'bundle.js', }, module: { // 将jsx语法文件转为js语法,es6转成es5 loaders: [{ test: /\.jsx?$/, loaders: ['babel-loader?presets[]=es2015,presets[]=react'] }] }, // module: { // loaders: [{ // test: /\.jsx?$/, // exclude: /node_modules/, // loader: 'babel-loader', // }] // }, // 将es6代码转化为es5 babel: { presets: ['es2015'] }, };组件中的文件:
/** * Created by joesbell on 2016/12/16. */ import React from 'react'; import ReactDOM from 'react-dom'; class Hello extends React.Component { render() { return <h1>Hello</h1> } } ReactDOM.render(<Hello />, document.getElementById('content'));main.js主文件:
import Hello from './component/productBox.js'; import Hello1 from './component/hello.js';构建和启动静态文件服务器:
"scripts": {"build": "webpack", "dev": "webpack-dev-server --devtool eval --progress --colors --hot --content-base build" },执行
npm run dev启动 server,此时打开
http://localhost:8080。实现自动刷新:
entry: [ //自动刷新加载 'webpack/hot/dev-server', 'webpack-dev-server/client?http://localhost:8080', APP_PATH ],
相关文章推荐
- React系列学习笔记:1.React与webpack基本配置
- webpack配置ES6 + react套装开发
- webpack +Reactjs +babel 加载器配置
- 配置Webpack-dev-server+初步了解react中元素、组件、事件、props传递
- webpack入门+react环境配置
- webpack react基础配置一
- 基于 Webpack 2 的 React Router 懒加载路由配置
- 手把手教你webpack、react和node.js环境配置(上篇)
- 手把手教你webpack、react和node.js环境配置(下篇)
- webpack 1.x构建react项目简单配置
- React + Typescript + Webpack 开发环境配置
- react webpack 环境配置
- React +webpack 基础配置
- webpack react基础配置二 热加载
- 配置React的Babel 6和Webpack 2环境
- WebStorm 下React和webpack的初始配置
- webpack入门+react环境配置
- webpack入门+react环境配置
- webpack入门+react环境配置
- webpack 2的react开发配置实例