代码之家  ›  专栏  ›  技术社区  ›  asanas

我们如何在Nuxt中只包含lodash所需的模块?Vuejs项目?

  •  16
  • asanas  · 技术社区  · 8 年前

    我们已经构建了一个Nuxt/VueJS项目。

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

    在我们的包装中。json,我们已经包含了lodash包。

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

    import orderBy from 'lodash/orderBy'
    

    在里面 nuxt编号。配置。js公司 ,将lodash添加到 vendor 列表

    然而,当我们创建构建时,Web包总是包含整个 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`
    
        }
      }
    }
    
    1 回复  |  直到 8 年前
        1
  •  14
  •   Greaka    8 年前

    你可以 npm install 仅限所需的软件包

    Lodash可根据 custom builds . 您可以找到已经可用的列表 here . 您可以这样使用它们: npm i -S lodash.orderby . 我没有检查,但您可能还需要更改 import orderBy from 'lodash/orderBy' 到 import orderBy from 'lodash.orderby' 。