Webpack 生产构建 - TypeError:e[t] 未定义。

7

我已经成功地在本地运行了这个应用程序,构建也没问题,但当我访问我的主页(唯一一个出错的路由)时,我会收到这个消息。

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上实时查看该应用程序崩溃。)


我现在也遇到了相同的问题,你找到任何解决方案或答案了吗? - Rene Polo
我解决了这个问题,我相当确定我的部署出了问题,一些 webpack 文件是旧的缓存版本,而不是新代码。 - Borjante
1个回答

2

我在我的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,我将把测试正则表达式更改为以下内容:
test: /[\\/]node_modules[\\/](react|react-dom|redux|react-intl)[\\/]/,

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接