PARAMANANDA PRADHAN
PARAMANANDA PRADHAN

Reputation: 1169

How svelte-kit build for staging env?

I want to build a svelte app using svelte-kit for staging environments. I did not find a suitable command to take .env.staging as its configs.

When I execute svelte-kit build it always takes .env.production

Please helps me with how to build for staging env.

Upvotes: 6

Views: 3216

Answers (3)

JP Damstra
JP Damstra

Reputation: 613

David's answer worked for me until "@sveltejs/kit": "^1.0.0". Now I use:

"scripts": {
  ...
  "stage": "vite build --mode staging",
  "prod": "vite build --mode production",
  ...

via npm run stage to build for staging and npm run prod to build for production. These commands map to .env.stage and .env.production files at the root of the project.

Upvotes: 5

David Dal Busco
David Dal Busco

Reputation: 8652

In recent SvelteKit version - e.g. 1.0.0-next.370 - the build scripts svelte-kit have been replaced by the direct usage of vite

used to be:

"scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",

is now:

  "scripts": {
    "dev": "vite dev",
    "build": "vite build",

Therefore it is possible to use Vite environment configuration files (documentation).

For example, I you would like to create an .env.staging file that contains your staging variables, you could add a following script to your `package.json:

  "scripts": {
    "dev": "vite dev",
    "build": "vite build",
    "staging": "npm run dev -- --mode staging"

which can be run in the command line with the corresponding target

> npm run staging

Upvotes: 3

RomanistHere
RomanistHere

Reputation: 892

https://github.com/vitejs/vite/issues/512#issuecomment-656547187 - check this answer (and topic), there are some workarounds.

Copy of the answer:

"scripts": {
    "build:dev": "APP_ENV=development vite build",
    "build:prod": "APP_ENV=production vite build",
}

In your vite.config.js, now you can manipulate it:

const mode = process.env.APP_ENV // This now exists.

module.exports = {
  mode: mode, // This will set the mode, to avoid confusions.
}

Also another github issue: https://github.com/sveltejs/kit/issues/1258

Upvotes: 6

Related Questions