Webpack生成版本 - TypeError e [t]未定义

时间:2018-01-24 16:40:25

标签: webpack webpack-2

所以我让应用程序在本地工作正常,构建也正常,但是当我在生产中访问我的HomePage(唯一有错误的路由)时,我收到此消息。

enter image description here

enter image description here

我不知道这可能意味着什么,这是我的webpack配置文件。

webpack.base.config.js

const path = require('path')
const webpack = require('webpack')
const vueConfig = require('./vue-loader.config')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin');


const isProd = process.env.NODE_ENV === 'production'

module.exports = {
  devtool: isProd
    ? false
    : '#cheap-module-source-map',
  output: {
    path: path.resolve(__dirname, '../dist'),
    publicPath: isProd ? 'https://d267jrn1ysxlb3.cloudfront.net/dist/' : '/dist/',
    filename: '[name].[chunkhash].js'
  },
  resolve: {
    alias: {
      'public': path.resolve(__dirname, '../public'),
      '@': path.resolve('__dirname', '../')
    }
  },
  module: {
    noParse: /es6-promise\.js$/, // avoid webpack shimming process
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        // exclude: /node_modules/
      },
      {
        test: /\.(png|gif|jpg|jpeg)$/,
        loader: 'url-loader',
        options: {
          limit: 8192,
          name: '[path][name].[ext]?[hash]',
          publicPath: isProd ? 'https://d267jrn1ysxlb3.cloudfront.net/' : '/'
        }
      },
      {
        test: /\.(woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader',
        options: {
          name: '[name].[ext]?[hash]',
          publicPath: isProd ? 'https://d267jrn1ysxlb3.cloudfront.net/assets/fonts/' : '/assets/fonts/'
        }
      },
      {
        test: /\.css$/,
        use: isProd
          ? ExtractTextPlugin.extract({
              use: {
                loader: 'css-loader',
                options: {
                  minimize: true
                }
              },
              fallback: 'vue-style-loader',
            })
          : ['vue-style-loader', 'css-loader']
      }
    ]
  },
  performance: {
    maxEntrypointSize: 300000,
    hints: isProd ? 'warning' : false
  },
  plugins: isProd
    ? [
        new webpack.optimize.UglifyJsPlugin({
          compress: { 
            warnings: false,
            drop_console: true
          },
          minify: true
        }),
        new ExtractTextPlugin({
          filename: 'common.[chunkhash].css'
        }),
        new CompressionPlugin({
          include: /\/dist/,
          algorithm: "gzip",
          test: /\.js$|\.css$|\.html$/,
          threshold: 10240,
          minRatio: 0.8
        })
      ]
    : [
        new FriendlyErrorsPlugin()
      ]
}

webpack.client.config.js

const glob = require('glob')
const webpack = require('webpack')
const merge = require('webpack-merge')
const base = require('./webpack.base.config')
const SWPrecachePlugin = require('sw-precache-webpack-plugin')
const VueSSRClientPlugin = require('vue-server-renderer/client-plugin')

const config = merge(base, {
  entry: {
    app: './src/entry-client.js'
  },
  resolve: {
    alias: {
      'create-api': './create-api-client.js'
    }
  },
  plugins: [
    // strip dev-only code in Vue source
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
      'process.env.VUE_ENV': '"client"'
    }),
    // extract vendor chunks for better caching
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: function (module) {
        // a module is extracted into the vendor chunk if...
        return (
          // it's inside node_modules
          /node_modules/.test(module.context) &&
          // and not a CSS file (due to extract-text-webpack-plugin limitation)
          !/\.css$/.test(module.request)
        )
      }
    }),
    // extract webpack runtime & manifest to avoid vendor chunk hash changing
    // on every build.
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest'
    }),
    new VueSSRClientPlugin()
  ]
})

if (process.env.NODE_ENV === 'production') {
  config.plugins.push(
    // auto generate service worker
    new SWPrecachePlugin({
      cacheId: 'vue-hn',
      filename: 'service-worker.js',
      minify: true,
      dontCacheBustUrlsMatching: /./,
      staticFileGlobsIgnorePatterns: [/\.map$/, /\.json$/],
      runtimeCaching: [
        {
          urlPattern: '/',
          handler: 'networkFirst'
        },
        {
          urlPattern: /\/(top|new|show|ask|jobs)/,
          handler: 'networkFirst'
        },
        {
          urlPattern: '/item/:id',
          handler: 'networkFirst'
        },
        {
          urlPattern: '/user/:id',
          handler: 'networkFirst'
        }
      ]
    })
  )
}

module.exports = config

有人知道从哪里开始吗?

您可以在v2.propertista.com上看到此应用程序崩溃。)

1 个答案:

答案 0 :(得分:0)

我有同样的问题,但是在我的React应用程序上:

  • 开发Webpack捆绑在一起时,一切工作正常。我的控制台日志中没有任何错误,并且从一页到另一页的访问很顺利
  • 切换到也“最小化”的生产版本时,我发现一页无法正确加载(出现黑屏)
  • 在控制台日志中,我看到了e[t] is undefined,就像您一样
  • 除此之外,在转到该页面之前刷新并加载其他页面可以使其再次正常工作

我所做的是寻找有问题的组件。几个小时后,我发现问题是我从node_modules导入的组件。这很有意义,因为其他页面(索引页面除外)都在使用它-我想这就是为什么在解决该问题之前访问任何页面(索引之外)的原因。

要解决这个问题,我必须在webpack.config.js中更改一件事。

我有以下几行:

optimization: {
  splitChunks: {
    cacheGroups: {
      commons: {
        test: /[\\/]node_modules[\\/](react|react-dom|redux)[\\/]/,
        name: 'vendor',
        chunks: 'all',
      },
    },
  },
},

添加测试可以解决我的第三方问题。 例如,如果问题所在的第三方组件来自react-intl,则将测试RegExp更改为以下内容:

test: /[\\/]node_modules[\\/](react|react-dom|redux|react-intl)[\\/]/,