webpack搭建react項目

來源:互聯網
上載者:User

標籤:rip   跨域   img   pos   bundle   .config   erro   Proxy 伺服器   tree   

遙看套路挖掘機,不見當年老司機。this is a boilerplate

1.建立一個項目目錄檔案夾,暫且叫seed

cd seednpm init

然後一直斷行符號,最後yes,最終產生一個package.json檔案

2.建立以下

  • src   檔案夾用於存放js,css,img等源碼和引用的靜態檔案
  • .babelrc    babel編譯器設定檔
  • favicon.ico    瀏覽器預設讀取的一個icon
  • index.html    入口首頁
  • webpack.config.js  webpack設定檔

3.用npm安裝npm包

npm install webpack webpack-dev-server --save-dev
webpack和webpack-dev-server是打包工具和輔助開發的伺服器,該伺服器能熱載入代碼,自動重新整理頁面,Proxy 伺服器解決前端開發時跨域問題
見http://www.cnblogs.com/fengnovo/p/5983638.html
npm install babel-core babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-0 --save-dev

這幾個是babel編譯器的npm包。

npm install css-loader postcss-loader style-loader --save-dev

webpack需要處理樣式檔案打包的處理器

npm install react react-dom --save

react項目的兩個基礎npm包

再在package.json裡的scripts對應的key裡增加一句 "start": "webpack-dev-server --port=3000 --inline --host 0.0.0.0 --colors --hot",
這樣,就可以在命令列用npm start啟動一個程式開發伺服器並即時熱更新開發時的代碼。

"scripts": {    "start": "webpack-dev-server --port=3000 --inline --host 0.0.0.0 --colors --hot",    "test": "echo \"Error: no test specified\" && exit 1"  },

4.webpack.config.js配置,直接上代碼

module.exports = {    entry: __dirname+‘/src/main.js‘,  //指明編譯開始的入口    output: {        path: __dirname+‘/bundle.js‘  //指明編譯好的檔案所在目錄    },    module: {                //webpack處理器模組,要處理什麼就加什麼處理器即loader        loaders:[            {                test: /\.js$/,                exclude: /node_modules/,                loader: ‘babel‘      //babel處理器,處理jsx/es6/es7            },{                test: /\.css$/,                       exclude: /node_modules/,                loader: ‘style-loader!css-loader‘  //處理css,style樣式            }        ]    },    solove: [        ‘‘,‘.js‘,‘.jsx‘        //import時檔案不寫尾碼,可以自動尋找.jsx和.js尾碼檔案    ],    devServer: {            //webpack-dev-server配置,與webpack-dev-server --port=3000 --inline --host 0.0.0.0 --colors --hot相輝映        contentBase: ‘./‘,        colors: true,        inline: true,        historyApiFallback: true    }}

 

5.配置完webpack,裡面的babel只是告知webpack用babel處理js,但是還要配置babel處理哪些檔案格式,直接上代碼

{    "presets": [        "react",        "es2015",        "stage-0"    ]}

6.inde.html

<!DOCTYPE html><html><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>novo Music</title></head><body>    <div id="app"></div>    <script src="./bundle.js"></script></body></html>

 ./bundle.js就是webpack.config.js裡面配置的輸出檔案路徑,然後被index.html引入

7.需要在src目錄下建立一個main.js作為webpack.config.js配置裡面所提到的入口編譯檔案

import React from ‘react‘;import ReactDOM from ‘react-dom‘;ReactDOM.render(    <div>Hello World</div>, document.getElementById(‘app‘))

 

boilerplate套路這就樣

項目所在github:https://github.com/fengnovo/diary/tree/master/seed

 

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.