react+webpack手动搭建项目(低版+ES5)

1、新建文件夹pro

2、npm init      一路回车   生成package.json

3、安装webpack, 4.0以上版本

安装命令:npm i webpack webpack-cli webpack-dev-server -D

4、配置文件,componentcssiconimgindex.htmlmain.jswebpack.config.js

5、安装react@15和 react-dom环境:npm i react@15 react-dom@15 -D

react+webpack手动搭建项目(低版+ES5)_第1张图片

编辑代码   

在component创建App.js文件,然后创建组件

App.js

APP.js    es5写法

var React = require("react")

var Demo = React.createClass({
    render(){
       return(
           

22222222222222222222

) } }) module.exports = Demo //暴漏组件,在main.js接收;

main.js    整合组件,做渲染

var ReactDOM = require("react-dom")
var React = require("react")


var Demo = require("./components/app")

ReactDOM.render(,document.getElementById("out"))

webpack文档

http://www.css88.com/doc/webpack2/

安装babel

npm install babel-loader babel-core babel-preset-es2015 babel-preset-react --save-dev

配置webpack.config.js文件     --->文档-->配置

var path = require('path');

//引入暴露
module.exports = {
  entry: './main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'foo.bundle.js'
  },

  //js转换模块
  module: {
  loaders: [
     {
       test: /\.js$/,
       exclude: /(node_modules|bower_components)/,
       loader: 'babel-loader',
       query: {
         presets: ['es2015',"react"]
       }
     }
   ]
 },

  //热更新监听
  devServer: {
  contentBase: path.join(__dirname, "dist"),
  compress: true,
  port: 9000,
  inline:true       //实时更新
}
};

 此时,可用webpack命令运行

npm i babel-loader@7 --save-dev

命令更换:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack-dev-server"
  },

npm run dev

 

ok,现在运行losthost:9000,页面就出现了

 

你可能感兴趣的:(react,React)