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使用 #9

Open
klouskingsley opened this issue Dec 17, 2016 · 1 comment
Open

webpack使用 #9

klouskingsley opened this issue Dec 17, 2016 · 1 comment

Comments

@klouskingsley
Copy link
Owner

klouskingsley commented Dec 17, 2016

hot module replacement 热替换(react版本)

  • 使用模块
    [webpack-dev-server]、[react-hot-loader]
  • loader配置
 {
  test: /\.jsx$/,
  loaders: ['react-hot', 'babel?plugins[]=transform-runtime&presets[]=es2015&presets[]=stage-0&presets[]=react']
 },
  • 注意事项,react 0.14版本需要在jsx源文件底下添加如下代码,参考链接
if (module.hot){
  module.hot.accept()
}
  • webpack命令
webpack-dev-server --inline --hot --progress --port 8080
  • 注意
    如果在docker下执行需要添加 --port 0.0.0.0

  • 如果不想在每个jsx源文件下加module.hot.accept()可以使用react-transform-hmr

    • .babelrc
{
  "presets": ["es2015", "stage-0", "react"],
  "env": {
    // only enable it when process.env.NODE_ENV is 'development' or undefined
    "development": {
      "plugins": ["transform-runtime", ["react-transform", {
        "transforms": [{
          "transform": "react-transform-hmr",
          // if you use React Native, pass "react-native" instead:
          "imports": ["react"],
          // this is important for Webpack HMR:
          "locals": ["module"]
        }]
        // note: you can put more transforms into array
        // this is just one of them!
      }]]
    }
  }
}
  • loader
{test: /\.jsx$/, loader: 'babel'}
@klouskingsley
Copy link
Owner Author

klouskingsley commented Dec 22, 2016

webpack 1.13 在babel生成source-map的时候有个bug,除了'source-map'之外其他的在浏览器上都看不到source文件,需要升级到1.14 stackoverflow

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

No branches or pull requests

1 participant