muk*_*ely 2 configuration vue.js nuxt.js
我正在使用 nuxt.js开发一个网站,并且希望有条件地在nuxt.config.js中有一个配置选项:我想在运行npm rungenerate(构建静态)时更改路由器基础
这是开发环境的完整配置文件(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)/
})
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该配置对于这两种设置都适用(因此它可以编译,应用程序可以正确运行),但我想使其自动,因为当我想查看静态版本时,我经常忘记取消注释路由器设置。
我没有太多地研究这个问题,只是阅读了一些SO问题并用谷歌搜索了一下(对于这样的事情:nuxt.js - >如何配置生产/开发设置或这个:https: //github.com/nuxt/nuxt .js/issues/2940)。
您可以使用环境变量并在配置文件中包含此环境变量的条件:
const pkg = require('./package')
let config = {
mode: 'universal',
head: {},
...
}
if (process.env.NODE_GENERATION_TYPE === 'static') {
config.router = {
base: '/app/'
}
}
module.exports = config
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用以下命令行来生成静态网站:
NODE_GENERATION_TYPE=static npm run generate
Run Code Online (Sandbox Code Playgroud)
您还可以设置一个脚本package.json以使其更漂亮:
{
"scripts": {
"generate:static": "NODE_GENERATION_TYPE=static dev",
"dev": "..."
},
...
}
Run Code Online (Sandbox Code Playgroud)
然后你就可以使用运行它npm run generate:static
| 归档时间: |
|
| 查看次数: |
5509 次 |
| 最近记录: |