nuxt.config.js中的条件属性

时间:2019-03-26 09:30:16

标签: vue.js configuration nuxt

我正在使用nuxt.js 开发一个网站,并希望有条件在 nuxt.config.js 中使用一个配置选项:我想更改路由器我运行 npm run generate (构建静态)

时的基础

这是开发环境的完整配置文件( npm run dev ):

const pkg = require('./package')

module.exports = {
  mode: 'universal',

  // if I uncomment the following three lines, the config is OK for npm run generate.
  // router: {
  //   base: '/app/'
  // },

  /*
  ** 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 }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Montserrat:400,500,600&subset=latin-ext' }
    ]
  },

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

  /*
  ** Global CSS
  */
  css: [
    '@/assets/css/main.scss',
  ],

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

  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    // Doc: https://bootstrap-vue.js.org/docs/
    'bootstrap-vue/nuxt',
    // Doc: https://github.com/vanhoofmaarten/nuxt-mq
    [
      'nuxt-mq',
      {
        // Default breakpoint for SSR
        // Breakpoints are bootstrap-vue Breakpoints
        // Doc: https://bootstrap-vue.js.org/docs/components/layout
        defaultBreakpoint: 'default',
        breakpoints: {
          xs: 576, // 576 not included
          sm: 768, // 768 not included
          md: 992, // 992 not included
          lg: 1200, // 1200 not included
          xl: Infinity
        }
      }
    ]
  ],
  bootstrapVue: {
    bootstrapCSS: false, // or `css`
    bootstrapVueCSS: false // or `bvCSS`
  },
  /*
  ** Axios module configuration
  */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
  },

  serverMiddleware: [
    '~/api/contact'
  ],

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }
}

该配置对于这两种设置都可以正常工作(因此可以编译,该应用程序可以正常运行),但是我想使其自动运行,因为我经常忘记取消注释路由器设置查看静态版本。

我没怎么研究这个问题,只是阅读了一些SO问题,然后用Google搜索了一下(对于诸如nuxt.js -> Howto configure production/development settingshttps://github.com/nuxt/nuxt.js/issues/2940之类的东西)。

1 个答案:

答案 0 :(得分:1)

您可以使用环境变量,并在配置文件中包含此环境变量的条件:

const pkg = require('./package')

let config = {
  mode: 'universal',
  head: {},
  ...
}

if (process.env.NODE_GENERATION_TYPE === 'static') {
  config.router = {
    base: '/app/'
  }
}

module.exports = config

然后,您需要使用以下命令行来生成您的静态网站:

NODE_GENERATION_TYPE=static npm run generate

您还可以在package.json中设置一个脚本,使其更漂亮:

{
  "scripts": {
    "generate:static": "NODE_GENERATION_TYPE=static dev",
    "dev": "..."
  },
  ...
}

您将可以使用npm run generate:static

运行它