Nuxt 应用程序编译耗时超过 4 分钟

Adr*_*ich 3 nuxt.js

我正在构建一个 Nuxt 应用程序。我做了一些研究,但没有找到明确的解决方案。

我发现了一个类似的 GitHub 问题(https://github.com/nuxt/nuxt.js/issues/3486),但无法找到明确的解决方案:

在此处输入图片说明

它正在“正常”编译,不超过 1 分钟。我刚刚向 Vue 组件添加了大约 300 行 html。突然变得非常低。

没有明确的错误、警报或警告消息,只有性能非常低。如何跟踪这种性能下降?

所以这是 nuxt.config.js 文件

const pkg = require('./package')
const webpack = require("webpack")

module.exports = {
  mode: 'universal',
  debug: true,
  prettify: false,
  /*
  ** Headers of the page
  */
  head: {
    title: pkg.name,
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    script: [
      { src: "https://cdn.jsdelivr.net/npm/sweetalert2@8" },
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },

  buildDir: '../functions/nuxt',

  build:{
    publicPath: '/',
    vendor: ['axios','firebase', "jquery", 'popper', "bootstrap", 'bootbox'],
    extractCSS: true,
    babel: {
      presets: [
        'es2015',
        'stage-0'
      ],
      plugins: [

        [
          "transform-runtime",
          {
           "polyfill":true,
           "regenerator":true
          },
          "~/plugins/firebase.js",
          "~/plugins/bootboxPlugin.js"
        ],
        new webpack.ProvidePlugin({
          jQuery: 'jquery',
          $: 'jquery',
          jquery: 'jquery'
        })
      ]

    },
    prettify: false
  },
  /*
  ** Global CSS
  */
  css: [
    'bootstrap/dist/css/bootstrap.css'
  ],

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [

  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://bootstrap-vue.js.org/docs/
    'bootstrap-vue/nuxt',
    '@nuxtjs/pwa',
  ],

  /*
  ** Build configuration
  */

  build: {
    prettify: false,
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
      config.devtool = ctx.isClient ? 'eval-source-map' : 'inline-source-map'
      prettify = false
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我不确定 prettify : false 指令应该放在哪里,所以我在很多地方都尝试过,因为我不确定 vueLoader 在哪里发生。

同样在 Nuxt 文档中说

注意:此配置自 Nuxt 2.0 起已被删除,请改用 build.loaders.vue。

所以这让我更加困惑。这个 build.loaders.vue 发生在哪里?

Ald*_*und 6

它不是nuxt错误。责怪更漂亮的人。这是导致此问题的问题:https : //github.com/prettier/prettier/issues/4784

解决方案:

1)不要使用大型嵌套模板,将其拆分为多个组件 -> 从代码质量来看,这是最佳解决方案

2)prettify: falseloaders选项中设置

https://nuxtjs.org/api/configuration-build/#loaders

https://github.com/vuejs/component-compiler-utils/blob/master/lib/compileTemplate.ts#L173

示例nuxt配置

export default {
  build: {
    loaders:  {
      vue: {
         prettify: false
      }
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

  • 设置 `pretify: false` 到底有什么作用? (2认同)