使用更高版本的webpack会导致bundle.js文件中出现JavaScript错误

时间:2018-02-01 16:09:25

标签: reactjs webpack webpack-dev-server

我有一个使用webpack的反应应用程序,当我使用最新版本的webpack时,它会在bundle.js文件中导致javascript错误。这是我得到的错误:

export class UnknownBeacon extends Component {

  constructor(props) {
    super(props);
    this.state = 
      {
        mainAppRef : props.mainAppRef
      };
  }

  render() {
    return (
      <Grid>
        <Row>
        <Image
          source={require("./scenerios/Welcome.png")}
          style={styles.MainContainer}
         />
         </Row>
         <Row style={ {backgroundColor: tflBlue} } >
         <TfLButtonWhite onPress={this.state.mainAppRef._startScanning} name={"Start Scanning"} />
         <TfLButtonWhite onPress={this.state.mainAppRef._stopScanning} name={"Stop Scanning"} />
         </Row>  
        </Grid>
    );
  }
}

这是bundle.js文件的一个片段。

bundle.js:5 Uncaught ReferenceError: window is not defined

错误来自这条线。

(function(modules) { // webpackBootstrap
/******/    function hotDisposeChunk(chunkId) {
/******/        delete installedChunks[chunkId];
/******/    }
/******/    var parentHotUpdateCallback = window["webpackHotUpdate"];
/******/    window["webpackHotUpdate"] = 
/******/    function webpackHotUpdateCallback(chunkId, moreModules) { // eslint-disable-line no-unused-vars
/******/        hotAddUpdateChunk(chunkId, moreModules);
/******/        if(parentHotUpdateCallback) parentHotUpdateCallback(chunkId, moreModules);
/******/    } ;

如果我转到较低版本的webpack和webpack-dev-server,则错误消失但我必须使用较新版本,因为它支持旧版浏览器。这是我的package.json文件。

var parentHotUpdateCallback = window["webpackHotUpdate"];

这是我的webpack.config.js文件。

{
  "name": "ava",
  "version": "0.1.0",
  "private": true,
  "main": "app.js",
  "dependencies": {
    "broadwayjs": "0.0.2",
    "html-webpack-plugin": "^2.30.1",
    "query-string": "^5.0.1",
    "react": "^15.6.2",
    "react-dom": "^15.6.2",
    "react-scripts": "1.0.14",
    "react16-spinjs": "^3.0.1",
    "start": "^5.1.0",
    "style-loader": "^0.19.0",
    "underscore": "^1.8.3"
  },
  "scripts": {
    "start": "webpack-dev-server --open",
    "dev": "webpack -d --watch",
    "build": "react-scripts build webpack",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-polyfill": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-syntax-object-rest-spread": "^6.13.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.11.0"
  }
}

0 个答案:

没有答案