React连系webpack设置

装置 node.js

装置 package.json文件

(npm init -y)

装置后的package.json

{
  "name": "c_react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jspm": "^0.16.53"
 },

装置webpack (模块打包机)

npm install webpack -gd(全局装置)
npm install webpack --save-dev(部分装置)

装置babel

npm install --save-dev babel-loader babel-core//这个比较慢
或
npm install cnpm -gd
cnpm install --save-dev babel-loader babel-core//能够挑选装置这个

装置转移的插件

npm install babel-preset-env  babel-preset-react --save-dev

进入webpack官网

在本身的项目目次下建:
app.js

import bar from './bar';
bar();
//
bar.js
export default function bar() {
  
}
//
webpack.config.js
    module.exports = {
  entry: './app.js',
  output: {
    filename: 'bundle.js'
  }
}

装置 react相干库

npm install react react-dom --save

运转webpack –watch


webpack-dev-server//供应的一个效劳
npm install  webpack-dev-server -gd
npm install  webpack-dev-server --save-dev

运转 webpack-dev-server –content-base build/

自动革新(automatic refresh)

 webpack-dev-server --content-base build/  --inline
 

热更新 (hot replacement)

cnpm install react-hot-loader  --save-dev


 webpack-dev-server --content-base build/  --hot

处置惩罚款式

 cnpm install style-loader css-loader  --save-dev

优化项目构造

上面都是须要装置的敕令,和设置的文件
接下来看完全的代码
webpack.config.js
var path=require("path");

module.exports = {
  devtool:'source-map',
  entry: './app.js',
  output: {
    path:path.resolve(__dirname,"build"),
    publicPath:"/assets/",
    filename:"bundle.js"
  },
  module: {
  rules: [
    { test: /\.js$/, exclude: /node_modules/, loader: "react-hot-loader!babel-loader" },
    { test: /\.css$/, exclude: /node_modules/, loader: "style-loader!css-loader" }
  ]
}
}
package.json
{
  "name": "c_react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start":"webpack-dev-server --content-base build/  --hot"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.4",
    "react-hot-loader": "^1.3.1",
    "style-loader": "^0.18.2",
    "webpack": "^3.4.0",
    "webpack-dev-server": "^2.6.1"
  },
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  }
}
app.js
import React from 'react';
import ReactDOM from 'react-dom'; 
import HelloWorld from './components/HelloWorld';
import './css/main.css';

ReactDOM.render(<HelloWorld/>,document.getElementById("app"));
HelloWorld.js
import React from 'react';

class HelloWorld extends React.Component{
  render(){
    return(
      <div>hello world1</div>
    );
  };

}

//export {HelloWorld as default};
export default HelloWorld;
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>react环境的搭建2</title>
</head>
<body>
    <div id="app"></div>
    <input type="text">
   <script src="assets/bundle.js">
    
   </script> 
</body>
</html>

这就是完全的一个环境

这是我的目次图

图片形貌

    原文作者:师妹儿
    原文地址: https://segmentfault.com/a/1190000010365118
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞