Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accessing Vite mode from config.ts #4183

Open
4 tasks done
Peque opened this issue Sep 6, 2024 · 5 comments
Open
4 tasks done

Allow accessing Vite mode from config.ts #4183

Peque opened this issue Sep 6, 2024 · 5 comments
Labels

Comments

@Peque
Copy link

Peque commented Sep 6, 2024

Is your feature request related to a problem? Please describe.

With Vite, I can access the --mode flag with:

import { loadEnv, defineConfig } from 'vite'

export default ({ mode }) => {
  return defineConfig({
    titleTemplate: mode,
  })
}

With Vitepress, however, this does not seem to be possible, since this command always results in mode == "development":

vitepress dev docs --mode foo

Describe the solution you'd like

Executing this command:

vitepress dev docs --mode foo

Should make this code pass:

import { loadEnv, defineConfig } from 'vitepress'

export default ({ mode }) => {
  if (mode != "foo") {
    throw new Error(mode);
  }
  return defineConfig({
    titleTemplate: mode,
  })
}

Describe alternatives you've considered

Haven't found a suitable alternative. Are there any?

Additional context

No response

Validations

@brc-dd
Copy link
Member

brc-dd commented Sep 6, 2024

You can use env variables as a workaround:

// MODE=foo vitepress dev docs

const mode = process.env.MODE || 'default'

@Peque
Copy link
Author

Peque commented Sep 6, 2024

@brc-dd Is is possible to load a different CSS depending on the MODE env variable during build?

I am trying from .vitepress/theme/index.js:

import DefaultTheme from 'vitepress/theme'

const mode = import.meta.env.MODE || 'blue'
import(`./style-${mode}.css`)

export default DefaultTheme

Which works for development, but for building, it seems like both:

MODE=blue vitepress build docs
MODE=red vitepress build docs

Would yield the same result.

@Peque
Copy link
Author

Peque commented Sep 6, 2024

Replying to my own question, in case this is useful for someone else, you can use in config.ts:

return defineConfig({
  vite: {
    define: {
      'process.env.MODE': JSON.stringify(mode),
    },
  },
}

And in index.ts:

import DefaultTheme from 'vitepress/theme'

const mode = process.env.MODE
import(`./style-${mode}.css`)

export default DefaultTheme

@brc-dd
Copy link
Member

brc-dd commented Sep 6, 2024

Ah, you need to prefix it with VITE_ if you want to use it in client.

VITE_MODE=foo vitepress build

import.meta.env.VITE_MODE // in theme, markdown/vue, etc.

process.env.VITE_MODE // in config, data/path loader, etc.

https://vitejs.dev/guide/env-and-mode.html#env-files

@Peque
Copy link
Author

Peque commented Sep 22, 2024

Related to this: #4222.

Opened a separate issue since it is independent (i.e.: allow accesing Vite mode from config.ts would still be a feature request). 😊

@github-actions github-actions bot added the stale label Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants