Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between Svelte dev mode and build mode?

The dev mode using npm run dev, the release mode using npm build

How could i know that it's currently built on dev mode or not in the code, for example:

<script>
    import {onMount} from 'svelte';

    onMount(function(){
        if(DEVMODE) { // --> what's the correct one?
            console.log('this is x.svelte');
        }
    })
</script>
 
like image 218
Kokizzu Avatar asked Oct 07 '20 13:10

Kokizzu


Video Answer


2 Answers

Not sure about the correct method. I share what I did on my project.

  1. in rollup.config.js
import replace from "@rollup/plugin-replace";
const production = !process.env.ROLLUP_WATCH;
  1. inside plugins:[ ] block add this
replace({
          isProduction: production,
      }),

rollup.config.js will look like this.

},
    plugins: [
        replace({
            isProduction: production,
        }),
        svelte({
            
  1. Then use isProduction inside components .

if (!isProduction){ console.log('Developement Mode'); }

like image 138
dagalti Avatar answered Sep 28 '22 18:09

dagalti


If you are using sveltekit:

import { dev } from '$app/env';

if (dev) {
    //do in dev mode
}
like image 35
Samuel Avatar answered Sep 28 '22 18:09

Samuel