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

Set base with environment variable #7743

Closed
4 tasks done
NicholasLYang opened this issue Apr 14, 2022 · 4 comments
Closed
4 tasks done

Set base with environment variable #7743

NicholasLYang opened this issue Apr 14, 2022 · 4 comments

Comments

@NicholasLYang
Copy link

Clear and concise description of the problem

I'm running vite as part of bunch of nested npm scripts:

{ 
  "scripts": {
    "build": "npm run build:wasm && npm run build:js",
    "build:js": "tsc && vite build",
    "build:wasm": "wasm-pack build --target web --release",
    }
}

I'd like to pass a base url, but due to a limitation with npm, it's rather hard to pass in arguments to nested scripts. Instead, I'd like the ability to have it passed as an environment variable, i.e. BASE_URL=/foobarbaz/ npm run build

Suggested solution

Not really sure honestly, would love some guidance.

Alternative

Bug the npm/pnpm people to get better argument propagation. I'll try this too!

Additional context

No response

Validations

@patak-dev
Copy link
Member

patak-dev commented Apr 14, 2022

Related #3522

@NicholasLYang we are working to properly support relative base in Vite 3 here:

The idea is that you will use base: '' or base: 'auto' (still discussing the details), and every path in your app will be relative or use new URL(..., import.meta.url).

Could you check if that PR fixes this issue? The generated HTML and assets can then be deployed with any base.

@sapphi-red
Copy link
Member

I think it is possible to do it like this.

// BASE="/foo/" npm run build
import { defineConfig } from 'vite'

export default defineConfig({
  base: process.env.BASE
})

@patak-dev
Copy link
Member

Oh, I should read more closely next time 🤦🏼
Thanks @sapphi-red!

Just for completeness, if you ever want to access an env file in the config, you need to manually loaded. See https://vitejs.dev/config/#environment-variables

@NicholasLYang
Copy link
Author

@sapphi-red thanks! That'll work for me. I'll close this issue if there's nothing else to discuss

@github-actions github-actions bot locked and limited conversation to collaborators Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants