2017-03-06 89 views
1

我一直在嘗試使用Webpack-Dev-Server來配置Webpack 2熱模塊替換。Webpack熱模塊替換不注入更新的代碼

webpack.config.js如下:

const path = require('path'); 
    const Webpack = require('webpack'); 
    const ExtractTextPlugin = require('extract-text-webpack-plugin'); 

    const HotModuleReplacement = new Webpack.HotModuleReplacementPlugin(); 
    const NamedModulesPlugin = new Webpack.NamedModulesPlugin(); 
    const NoEmitOnErrorsPlugin = new Webpack.NoEmitOnErrorsPlugin(); 

    const extractCSS = new ExtractTextPlugin('main.css'); 
    const extractSCSS = new ExtractTextPlugin('styles.css'); 

    const config = { 
     entry: [ 
      'webpack-dev-server/client?http://localhost:8080', 
      // bundle the client for webpack-dev-server 
      // and connect to the provided endpoint 
     'webpack/hot/only-dev-server', 
     // bundle the client for hot reloading 
     // only- means to only hot reload for successful updates 
     './src/index.js', 
     ], 
     output: { 
      path: path.resolve(__dirname, 'dist'), 
      filename: 'bundle.js', 
      publicPath: 'http://localhost:8080/', 
      hotUpdateChunkFilename: 'hot/hot-update.js', 
      hotUpdateMainFilename: 'hot/hot-update.json', 
     }, 
     module: { 
      rules: [ 
       { 
        test: /\.(js|jsx)$/, 
        exclude: [ 
         path.resolve(__dirname, 'node_modules'), 
        ], 
        loader: 'babel-loader', 
        options: { 
         presets: ['react', 'es2015', 'stage-2'], 
        }, 
       }, 
       { 
        test: /\.scss$/, 
        loader: extractSCSS.extract({ 
         fallback: 'style-loader', 
         use: ['css-loader', 'postcss-loader', 'sass-loader'], 
        }), 
       }, 
       { 
        test: /\.css$/, 
        loader: extractCSS.extract({ 
         fallback: 'style-loader', 
         use: ['css-loader', 'postcss-loader'], 
        }), 
       }, 
      ], 
     }, 
     plugins: [ 
      extractCSS, 
      extractSCSS, 
      HotModuleReplacement, 
      NamedModulesPlugin, 
      NoEmitOnErrorsPlugin, 
     ], 

     devtool: 'source-map', 
     devServer: { 
      publicPath: 'http://localhost:8080/', 
      contentBase: './', 
      inline: true, 
      hot: true, 
      historyApiFallback: true, 
      stats: { 
       colors: true, 
      }, 
     }, 
    }; 

    module.exports = config; 

我有了這個文件夾結構:

/ 
/src 
/src/index.js 
/src/index.scss 

,我想我需要使用HMR API在index.js像這樣:

import MockComponent from './MockComponent/MockComponent'; 

export default class App { 
    constructor() { 
     this.mock = new MockComponent(); 
    } 
    render() { 
     return `<div class="element">${this.mock.render()}</div>`; 
    } 
} 

let app = {}; 
app = new App(); 
const mainDiv = document.querySelector('#root'); 
mainDiv.innerHTML = app.render(); 

// Hot Module Replacement API 
if (module.hot) { 
    module.hot.accept(); 
} 

問題是,當我在代碼中進行更改時控制檯我:

chrome console

但隨後的HMR似乎它沒有這樣做所再現的任何變化。

請問有人能幫我解決這個問題嗎?

非常感謝

+0

你最終解決了這個問題嗎?這個答案有幫助嗎? –

+0

謝謝你的回答,但我還沒有排序。我從我的開發環境中刪除了'ExtractTextPlugin',但是現在我得到一個'[HMR]更新失敗:錯誤:對http://localhost/hot/hot-update.json的清單請求超時。 at XMLHttpRequest.request.onreadystatechange(http:// localhost:8080/main.bundle.js:38:22)在控制檯中輸出以替換組件樣式(請參閱此分支https:// github。 COM/andrixb/WebpackSassStarter/BLOB/testHMR/webpack.config.js)。我有兩個單獨的配置dev和prod,所以我沒有像你所顯示的那樣使用environment var。 – andrixb

回答

0

這個問題似乎是,你沒有disableExtractTextPlugin,而在發展模式。在不禁用它的情況下,您傳遞的fallback加載程序將無法運行,在這種情況下,style-loaderExtractTextPlugin不支持HMR本身。

在生產中,您不希望ExtractTextPlugin被禁用,因此一種方法是使用一個配置將使用--env標誌。請注意,通常建議使用分離配置,一個用於生產,另一個用於開發,儘管您可以使用像webpack-merge這樣的工具來共享通用位。

所以在你的package.json你要更新腳本,然後改變你的配置是一個函數,因此它可以接受env對象作爲參數。

的package.json

{ 
    ... 
    scripts: { 
    "start": "webpack-dev-server --env.dev", 
    "build": "webpack" 
    }, 
    ... 
} 

webpack.config.js

... 

const config = (env = {}) => ({ 
    ... 
    plugins: [ 
    new ExtractTextPlugin({ 
     filename: 'styles.css', 
     disable: env.dev === true 
    }) 
    ] 
}) 

我在這裏不包括大量的簡潔,但應該給你一個不錯的主意。請參閱下面的我使用的版本...

[email protected] 
[email protected] 
[email protected] 
[email protected] 

它的進口需要注意的是,當你可以devServer實現與hot: true HMR和plugins一個new HotModuleReplacementPlugin(),其他裝載機和需要配置更改生產「生活「在瀏覽器中更改不同類型的代碼...這裏有幾個浮現在腦海:

0

這活重裝已經解決(請參閱本回購https://github.com/andrixb/WebpackSassStarter):

  • 添加在webpack.config.jsHtmlWebpackPlugin具有以下配置:

    new HtmlWebpackPlugin({ title: 'FE Build Setup', hash: true, template: './src/index.html', }),

  • src文件夾中的index.html那是外面去除靜態腳本和鏈接標籤內移動(index.html現在使用的只是一個模板)
  • webpack.config.js刪除 在entry

    'webpack-dev-server/client? http://localhost:8080', // bundle the client for webpack-dev-server // and connect to the provided endpoint 'webpack/hot/only-dev-server', // bundle the client for hot reloading // only- means to only hot reload for successful updates

    和在output

    publicPath: 'http://localhost:8080/', hotUpdateChunkFilename: 'hot/hot-update.js', hotUpdateMainFilename: 'hot/hot-update.json',

  • 從開發環境(在webpack.config.jsExtractTextPlugin@Skip傑克

  • 所建議在webpack.config.js

編輯的 devServer配置