Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack 1.x HMR hot reload 配置指南· #4

Open
noyobo opened this issue Mar 24, 2017 · 3 comments
Open

webpack 1.x HMR hot reload 配置指南· #4

noyobo opened this issue Mar 24, 2017 · 3 comments
Labels

Comments

@noyobo
Copy link
Owner

noyobo commented Mar 24, 2017

1. 获取 webpack compiler 实例

const webpack = require('webpack');
const webpackConfig = require('webpack.config.dev.js');

const compiler = webpack(webpackConfig);

compiler.plugin('done', (stats) => {
    console.log('webpack build finished');
});

2. 使用 webpack-dev-server 启动服务

const WebapckDevServer = require('webpack-dev-server');

const server = new WebapckDevServer(compiler, {
    hot: true
});

server.listen(8080, (err) => {
  console.log('Server at http://localhost:8080')
});

3. webpack.config.dev.js 关键配置

const client = require.resolve('webpack-dev-server/client');
const hotServer = require.resolve('webpack/hot/only-dev-server');
const options = require('../utils/parseOptions');

// 给所有 entry 加上 webpack-dev-server/client,webpack/hot/only-dev-server
Object.keys(webpackConfig.entry).forEach(point => {
  webpackConfig.entry[point].unshift(`${hotServer}`);
  webpackConfig.entry[point].unshift(
    `${client}?${options.protocol}//${options.host}:${options.port}/`
  );
});

4. module-hot-accept-loader 修改入口文件源码

'use strict';

/**
 * append 'modult.hot.accept()' to entry point source.
 */
module.exports = function(source, inputMap) {
  if (this.cacheable) {
    this.cacheable();
  }

  const callback = this.async();

  if (/\bmodule.hot\b/.test(source)) {
    return callback(null, source, inputMap);
  }

  const entry = this.options.entry;
  const resourcePath = this.resourcePath;

  let resourcePathInEntry = false;

  Object.keys(entry).forEach(point => {
    if (Array.isArray(entry[point])) {
      resourcePathInEntry = entry[point].some(entryFile => {
        return entryFile.startsWith(resourcePath);
      });
    }
  });

  if (!resourcePathInEntry) {
    return callback(null, source, inputMap);
  }

  return callback(
    null,
    `${source}
    
// HMR append by rax-scripts/loaders/module-hot-accept.js
// @see https://github.com/alibaba/rax
module.hot.accept(function(err){
  if (err) {
    console.log(err);
  }
});`,
    inputMap
  );
};
@noyobo noyobo added the webpack label Mar 24, 2017
@noyobo
Copy link
Owner Author

noyobo commented Mar 24, 2017

@noyobo
Copy link
Owner Author

noyobo commented Mar 24, 2017

@noyobo
Copy link
Owner Author

noyobo commented May 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant