使用webpack和react搭建項目

來源:互聯網
上載者:User

標籤:alert   安裝配置   ext   close   depend   scale   產生   nbsp   version   

看了N多部落格,日誌,一邊迷茫一邊摸索。本文記錄流程。我怕自己忘了。。。並且修複了部落格園首頁推薦那個日誌中遇到的bug

1、webstorm建立一個空白項目,比如webpack_demo

2、因為要用react和es6文法,調整webstorm-設定-語言-javascript-jsx,確定。這樣檔案不會報錯。

3、建立app(存放入口檔案,component組件),static(存放打包後的檔案),webpack(存放webpack設定檔)三個檔案夾

4、在webpack_demo根目錄,開啟cmd或者終端,輸入npm init,一路斷行符號

5、完成後,繼續輸入npm install webpack -g。完成webpack的配置

6、在app中建立main.js;在webpack中建立設定檔webpack.config.js;在static中建立一個index.html;在static中建立一個js目錄

7、在index頁面寫以下內容

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />    <title>webpack_Demo</title></head><body><div class="content"></div><script src="./js/app.js" type="text/javascript" charset="utf-8"></script></body></html>
View Code

8、配置webpack.config.js

var path = require("path");module.exports = {    entry:{  //入口檔案        "app":path.join(__dirname,"../app/main.js")  //app對應產生的檔案名稱    },    output:{        path:path.join(__dirname,"../static/js/"),        filename:"[name].js"   //這裡[name]就是表示對應entry對象的name,然後產生的後戳是.js    }}
View Code

9、隨便在main.js寫點什麼,比如alert

10、在cmd中運行

 webpack --config ./webpack/webpack.config.js
View Code

11、成功後訪問index.html,沒成功重複上面的操作

12、每次編譯都很煩,可以在package.json中添加對應的配置,代碼如下

{  "name": "web_pack",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "build":"webpack --config ./webpack/webpack.config.js" //添加一個build 值是打包用到的命令  },  "author": "",  "license": "ISC",  "dependencies": {    "webpack": "^3.0.0"  }}
View Code

13、再次運行npm run build

14、安裝配置webpack-dev-server,實現熱更新。

  1、在cmd中執行npm i webpack-dev-server --save-dev

  2、在package.json中修改scripts

 "test": "node_modules\.bin\webpack-dev-server --config ./webpack/webpack.config.js --port 8089 --open", 
View Code

  3、index頁面的js,使用絕對路徑,比如  http://localhost:8089/app.js

  4、cmd中執行 npm run test,可以開啟一個本機伺服器,每次更改代碼後,自動重新整理頁面

15、繼續安裝react,在cmd中執行  npm i babel babel-core babel-loader babel-preset-es2015 babel-preset-react react react-dom --save

16、在main.js中隨便寫點react的代碼,比如hello world

17、在webpack.config.js添加babel-loader用來解析jsx和es6

var path = require("path");module.exports = {    entry:{        "app":path.join(__dirname,"../app/main.js")     },    output:{        path:path.join(__dirname,"../static/js/"),        filename:"[name].js"           },    module:{        loaders:[            {                test:/\.(js|jsx)$/,                    loader:"babel-loader",                exclude:/node_module/,                    query:{                    presets:["react","es2015"]                        }            }        ]    }}
View Code

18、這個時候如果以上都正確,瀏覽器會自動重新整理出helloworld

19、可以再下載style-loader和css-loader處理樣式

使用webpack和react搭建項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.