react-hot-loader記錄

來源:互聯網
上載者:User

1.

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

2.

npm install --save-dev react-hot-loader@3.0.0-beta.6或者npm install --save-dev react-hot-loader@next

3.

.babelrc配置{  "presets": [    ["es2015", {"modules": false}],       // webpack現在已經支援原生的import語句了, 並且將其運用在tree-shaking特性上    "stage-2",      // 規定JS運用的語言規範層級      // Stage 2 是 "草案", 4 是 "已完成", 0 is "稻草人(strawman)"。      // 詳情查看 https://tc39.github.io/process-document/    "react"      // 轉譯React組件為JS代碼  ],  "plugins": [    "react-hot-loader/babel"      // 開啟react代碼的模組熱替換(HMR)  ]}

4.

config配置:const { resolve } = require('path');const webpack = require('webpack');module.exports = {  context: __dirname,  entry:  [    'react-hot-loader/patch',    'webpack/hot/only-dev-server',    './app/main.js'  ],  output: {    path: resolve(__dirname, 'build'),//打包後的檔案存放的地方    filename: "bundle.js",//打包後輸出檔案的檔案名稱    publicPath: "/"  },  devServer: {    contentBase: resolve(__dirname, 'build'),    hot: true,    publicPath:'/'  },  module: {    rules: [      {        test: /\.jsx?$/,        use: [          'babel-loader',        ],        exclude: /node_modules/      },    ],  },  plugins: [    new webpack.HotModuleReplacementPlugin(),    new webpack.NamedModulesPlugin(),  ],  devtool: "cheap-eval-source-map",};

5.

入口檔案中配置:import {AppContainer} from 'react-hot-loader'import Redbox from 'redbox-react'const render =  (Component) => {  ReactDOM.render(    <AppContainer errorReporter={Redbox}>      <Component />    </AppContainer>,    document.querySelector('#odiv')  )};render(App);if(module.hot) {  module.hot.accept('./app', () => {    render(App)  });}或者const oEle = document.querySelector('#odiv');render(  <AppContainer errorReporter={Redbox}>    <App />  </AppContainer>,  oEle)if (module.hot) {  module.hot.accept('./app', () => {    const NextApp = require('./app').default;    render(      <AppContainer errorReporter={Redbox}>        <NextApp />      </AppContainer>,      oEle    )  });}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.