vue项目配置多页面打包后,优化的JS代码块不执行是怎么回事?

发布于 2022-09-13 01:28:30 字数 1357 浏览 27 评论 0


const path = require('path')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin


module.exports = {
  pages: {
    index: {
      entry: 'src/pages/index/main.js',
      template: 'public/index.html',
      filename: 'index.html',
      title: 'Index Page',
      chunks: []
    },
    page1: {
      entry: 'src/pages/page1/main.js',
      template: 'public/index.html',
      filename: 'page1.html',
      title: 'Page1',
      chunks: []
    }
  },
  configureWebpack: {
    devtool: 'source-map',
    optimization: {
      splitChunks: {
        chunks: 'all',
        minChunks: 1,
        minSize: 2000,
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            chunks: 'initial', // only package third parties that are initially dependent
            priority: 10
          },
          commons: {
            name: 'chunk-commons',
            test: /[\\/]src[\\/]components[\\/]/, // can customize your rules
            priority: 11,
          }
        }
      }
    },
    plugins: [new BundleAnalyzerPlugin()]
  }
}

以上是我的配置代码,运行该项目后JS虽然正确的分割了,但是JS在浏览器并没有执行。

image.png

image.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2022-09-20 01:28:30

这个问题我自己已经解决了,我使用vue-inspect看了下配置,pages.page1.chunks最终会编译到HtmlWebpackPlugin的实例里面,类似这样:

new HtmlWebpackPlugin(
      {
        title: 'Index Page',
        templateParameters: function () { /* omitted long function */ },
        chunks: [
          'chunk-vendors',
          'chunk-common',
          'index'
        ],
        template: 'public/index.html',
        filename: 'index.html'
      }
    ),
    /* config.plugin('html-page1') */
    new HtmlWebpackPlugin(
      {
        title: 'Page1',
        templateParameters: function () { /* omitted long function */ },
        chunks: [
          'chunk-vendors',
          'chunk-common',
          'page1'
        ],
        template: 'public/index.html',
        filename: 'page1.html'
      }
    )

所以我把pages.page1.chunks里面的项的值改成我自己分割的chunk的名称,然后就可以正常运行了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文