我们已经建立了一个Nuxt/VueJS项目.

Nuxt有自己的名为nuxt.config.js的配置文件,我们在其中配置webpack和其他构建设置.

在我们的包裹里.json,我们已经包含了lodash包.

在我们的代码中,我们一直小心地只加载导入我们需要的内容,例如:

import orderBy from 'lodash/orderBy'

nuxt.config.js年,lodash被添加到vendor个名单中.

然而,当我们创建构建时,webpack总是包含整个lodash库,而不是只包含我们在代码中使用的内容.

我读了很多教程,但还没有找到答案.如果只是一个网页项目,其中一些答案肯定会奏效.但在我们的例子中,它是通过nuxt配置文件实现的.

期待得到帮助.

下面是部分nuxt.配置.js文件.仅包括相关/重要部分:

const resolve = require('resolve')
const webpack = require('webpack')

module.exports = {
  /*
  ** Headers of the page
  */
  head: {
  },
  modules: [
    ['@nuxtjs/component-cache', { maxAge: 1000 * 60 * 10 }]
  ],
  plugins: [
    { src: '~/plugins/intersection', ssr: false },
  ],
  build: {
    vendor: ['moment', 'lodash'],
    analyze: {
      analyzerMode: 'static'
    },
    postcss: {
      plugins: {
        'postcss-custom-properties': false
      }
    },
    plugins: [
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    /*
    ** Run ESLINT on save
    */
    extend (config, ctx) {
      // config.resolve.alias['create-api'] = `./create-api-${ctx.isClient ? 'client' : 'server'}.js`

    }
  }
}

推荐答案

您只能 Select npm install个所需的软件包

Lodash可以每custom builds个进行分解.你可以找到一个已经可用的列表here.你可以这样使用它们:npm i -S lodash.orderby.我没有判断,但您可能还需要将import orderBy from 'lodash/orderBy'更改为import orderBy from 'lodash.orderby'.

Vue.js相关问答推荐

如何在AXIOS调用响应中动态导入视频并创建它的绝对路径?

设置Vite Vue 3需要多个应用程序输出目录

将 html 文件移出 dist vite 中的 src 文件夹

在预渲染模式下部署 nuxt 3 时出错

Vue.js 3 运行时挂载组件实例

处理多张卡片中的按钮

Nuxt 和 Vite 有什么区别?

如何在 VUE.js 中重新刷新时保持多层 b-collapse 菜单打开?

Nuxt 2.15.7 的构建错误 - 无法解析 CSS @font-face URL

使用 Laravel 作为后端刷新 Vue.js SPA 的身份验证令牌

使用 v-slot:body 时 Vuetify v-data-table 没有响应

Laravel Vue - 如何让 vue 在公共文件夹中查找图像

Express.js + lint 出错

Amazon EC2 错误:监听 EACCES 0.0.0.0:80

如何使用 JavaScript 按索引删除数组元素?

了解 Nuxt.js 中的State状态和Getters:Getters不起作用

在 vue.js 组件中react this.props.children

将 ref 的内容从子组件传递到另一个 VueJS 子组件

Vue 2.0 设置路由 - 不要使用new来产生副作用

在Vue中调用方法时的括号