无法读取React缩小的应用中未定义的属性“订阅”

时间:2018-08-13 10:53:18

标签: javascript reactjs webpack

因此,我面临着一个奇怪的问题,即如果我在html文件中引用我的应用程序的未压缩版本,则它可以完美运行。但是,如果我引用的是缩小版本,则会引发

uncaught typeerror: cannot read property 'subscribe' of undefined react错误。

我有两个不同的Webpack配置。

开发配置:

const webpack = require('webpack')
const path = require('path')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const autoprefixer = require('autoprefixer');

module.exports = {
  mode: "development",

  entry: {
    owners: "./src/app1.tsx",
    properties: "./src/app2.tsx",
  },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'Public')
  },

  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks: 'initial',
          name: 'vendor',
          test: 'vendor',
          enforce: true,
        },
      }
    },
    runtimeChunk: true
  },

  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json", "*.png", "*.jpg", "*.svg"]
  },

  devServer: {
    contentBase: 'Public'
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        include: /src/,
        loader: "ts-loader"
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',

        options: {
          presets: ["env"]
        }
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          'url-loader?limit=10000',
          'img-loader'
        ]
      },
      {
        test: /\.css$/,
        include: /src/,
        exclude: /node_modules/,
        loader: ExtractTextPlugin.extract('typings-for-css-modules-loader?modules&namedExport&camelCase&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'),
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader?sourceMap' })
      },

      { test: /\.svg$/, loader: 'svg-loader?pngScale=2' }]
  },

  externals: {
    "react": "React",
    "react-dom": "ReactDOM"
  },

  plugins: [
    new ExtractTextPlugin('[name].css'),
    new HtmlWebpackPlugin({
      filename: 'app1.html',
      template: './assets/app1.html'
    }),
    new HtmlWebpackPlugin({
      filename: 'app2.html',
      template: './assets/app2.html'
    })]
}

之所以有两个文件app1和app2是因为我在大项目中实际上有2个迷你React项目,每个项目都有其自己的唯一HTML ID,该ID挂接到其自己的Component上,并通过{{ 1}}。

产品版本:

ReactDom.render

Package.json

“ build”:“ webpack --config ./webpack.prod.js”, “ build:dev”:“ webpack --config ./webpack.dev.js”,

我有以上两个构建脚本。第一个创建缩小的文件,第二个创建未缩小的文件(有效的文件)。

什么可能导致此问题?

0 个答案:

没有答案