如何区分 Svelte 开发模式和构建模式? [英] How to differentiate between Svelte dev mode and build mode?

查看:40
本文介绍了如何区分 Svelte 开发模式和构建模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dev模式使用npm run dev,release模式使用npm build

我怎么知道它当前是建立在开发模式还是不在代码中,例如:

解决方案

不确定正确的方法.我分享了我在我的项目中所做的事情.

  1. rollup.config.js

<块引用>

从@rollup/plugin-replace"导入替换;const 生产 = !process.env.ROLLUP_WATCH;

  1. plugins:[ ] 块中添加这个

<块引用>

替换({isProduction: 生产,}),

rollup.config.js 看起来像这样.

},插件: [代替({isProduction: 生产,}),苗条({

  1. 然后在组件内部使用 isProduction.

<块引用>

if (!isProduction){ console.log('开发模式');}

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>
 

解决方案

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'); }

这篇关于如何区分 Svelte 开发模式和构建模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆