Svelte Environment Variables
Installation
npm install @dr-montasir/svelte-env-vars
How to use?
In the rollup.config.js file just add two lines of code like so
import addEnv from '@dr-montasir/svelte-env-vars';
and
addEnv()
import addEnv from '@dr-montasir/svelte-env-vars'; // here line 1
const production = !process.env.ROLLUP_WATCH;
export default {
...,
plugins : [
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
},
}),
addEnv(), // here line 2
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: 'bundle.css' }),
...,
],
...
}
.env file
Create .env in the svelte project root and for example add the API_URL as follow
API_URL=http://localhost:5000/api
Make a test
Check the validity of configuration by adding console.log(process.env.API_URL);
in any svelte component
// App.svelte
`<script>`
...
console.log(process.env.API_URL); // http://localhost:5000/api
...
`</script>`