标签:init res host java 通过 react span render 步骤
mkdir app //创建app目录 用来存放项目源文件
mkdir dist // 创建dist目录 用来存放打包好的文件
touch .gitignore //创建.gitignore 用来添加git 忽略的文件
touch webpack.config.js //创建webpack的配置文件
cd app //进入到app目录
touch index.js //在app目录中创建 index文件 入口文件
mkdir component //创建componet目录 用来装组件
npm init -y
npm install webpack --save
module.exports = { context:__dirname+"/app", //源文件目录 entry:{ app:"./index.js" //在源文件目录下去找index.js 文件作为打包的入口文件 }, output:{ path:__dirname+"/dist", //生成的文件存放目录 filename:"[name].bundle.js" //生成的文件 name 表示entry下面的app } }
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev":"./node_modules/.bin/webpack" //这条命令是新增加的
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"webpack": "^2.2.1"
}
}
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <title>首页</title> </head> <body> </body> <scriptsrc="app.bundle.js"></script> </html>
doucment.write("hello world!!!!")
npm install babel-loader babel-core babel-preset-es2015 --save
module.exports = {
context:__dirname+"/app",
entry:{
app:"./index.js"
},
output:{
path:__dirname+"/dist",
filename:"[name].bundle.js"
},
module:{
rules:[
{
test:/\.jsx?$/,
exclude: /node_modules/,
use: [{
loader: "babel-loader",
options: { presets: ["react","es2015"] }
}],
},
]
}
}
npm install react react-dom babel-preset-react --save
import React from ‘react‘; import ReactDOM from ‘react-dom‘; class IndexComponent extends React.Component{ render(){ return <h1>hello world!!!</h1> } } var oBox = document.getElementById("box"); ReactDOM.render(<IndexComponent/>,oBox)
npm run dev 然后运行 index.html 文件查看效果
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>首页</title> </head> <body> <divid="box"></div> </body> <scriptsrc="app.bundle.js"></script> </html>
npm install css-loader style-loader --save
module.exports = { context:__dirname+"/app", entry:{ app:"./index.js" }, output:{ path:__dirname+"/dist", filename:"[name].bundle.js" }, module:{ rules:[ { test:/\.jsx?$/, exclude: /node_modules/, use: [{ loader: "babel-loader", options: { presets: ["react","es2015"] } }], }, { //这里的内容是新增加的对样式的支持 test: /\.css$/, use: ["style-loader", "css-loader"], }, ] } }
import React from ‘react‘; import ReactDOM from ‘react-dom‘; import styles from ‘./css/index.css‘; //这里内容属于新增加的 class IndexComponent extends React.Component{ render(){ return <h1>hello world!!!</h1> } } var oBox = document.getElementById("box") ReactDOM.render(<IndexComponent/>,oBox)
h1{ color: green; }
npm install webpack-dev-server --save
"dev": "./node_modules/.bin/webpack"
"dev": "./node_modules/.bin/webpack-dev-server --content-base dist"
标签:init res host java 通过 react span render 步骤
原文地址:http://www.cnblogs.com/dadifeihong/p/6443272.html