简体   繁体   English

Webpack无法自动编译React文件中的代码更改

[英]Webpack not compiling changes to code in React file automatically

I recently started trying to use React along with Webpack to automatically compile changes when something is changed. 我最近开始尝试将React和Webpack一起使用,以在发生更改时自动编译更改。 However, despite me googling to my hearts content, I can not seem to find why it just won't work. 但是,尽管我一直在研究自己的内心世界,但我似乎无法找到为什么它行不通的原因。

app.js app.js

import React from 'react'
import ReactDOM from 'react-dom'
import {Router, Route, Link} from 'react-router'
import ProductList from './products'

class Main extends React.Component {
    constructor(props){
        let products = [];

        super(props);

        this.state = {
            name: "Harry"
        };
    }
    render(){
        return(
            <Storefront name={this.state.name} />
        );
    }
}

var a = 10;

let Storefront = (props) =>
<div className="container">
    <h4>Welcome to the React Store Front, {props.name}!</h4>
    <Link to="/products">View Products</Link>
</div>;

ReactDOM.render(
    <Router>
        <Route path="/" component={Main}/>
        <Route path="/products" component={ProductList}/>
    </Router>,
    document.getElementById("app")
);

package.json 的package.json

{
  "name": "static",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "history": "^1.13.1",
    "react-router": "^1.0.2"
  },
  "devDependencies": {
    "babel-core": "^6.3.17",
    "babel-loader": "^6.2.0",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "scripts": {
    "dev": "webpack-dev-server --inline --hot --progress --colors --port 8090",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "private": true
}

webpack.config.js webpack.config.js

module.exports = {
    context: __dirname,
    entry: ['webpack/hot/dev-server', './app.js'],
    output: {
        filename: "app.js",
        path: __dirname + "/dist"
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loaders: ["babel-loader?presets[]=react,presets[]=es2015"]
            },
            {
                test: /\.jsx$/,
                loaders: ['babel-loader?presets[]=react,presets[]=es2015']
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    watch: true
};

you have to use webpack-hot-middleware NPM link is NPM 您必须使用webpack-hot-middleware NPM链接是NPM

if you want to example then you can use react-router example React-router 如果您想举例,那么可以使用react-router示例React-router

I have used and my webpack.config file 我已经使用了我的webpack.config文件

/* eslint-disable no-var */
var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: [
    'webpack-hot-middleware/client',
    './scripts/index'
  ],
  devtool: 'eval-source-map',
  output: {
    path: __dirname,
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'scripts')
    }]
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM