有没有一种方法可以选择在捆绑软件中添加功能? [英] Is there a way to optionally add function in bundle?

查看:60
本文介绍了有没有一种方法可以选择在捆绑软件中添加功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的任务是为CMS创建两个不同的捆绑软件,为产品团队创建另一个捆绑软件,我希望将几个功能捆绑在CMS捆绑软件中,但不在产品团队中.

Good day, My task is to create two different bundle one for CMS and another one for Product team and there are few function's which i want to be in bundle of CMS but not in the Product team.

我有XYZ类

Class XYZ {

constructor() {}

forCms() {}

forProduct() {}

}

我想要两个不同的捆绑包:

and I want two different bundle:

对于CMS(我需要forCms函数):

For CMS(i need forCms function):

Class XYZ {

constructor() {}

forCms() {} //<--- notice methods

}

对于产品(我需要产品功能):

For Product(I need forProduct function):

Class XYZ {

constructor() {}

forProduct() {} //<--- notice methods

}

我搜索了很多东西,然后发现了Env.变量,我将它们用于某些逻辑,例如:

I searched a lot and I found Env. variable and i use them for some logic like:

function abc() {
        const env = process.env.NODE_ENV;
        switch (env) {
            case "cms":
                //some logic
                break;
            default:
               //some logic
                 });
        }
}

我不知道,该如何实现?或ENV之外还有其他方法吗?在Webpack或Rollup之类的捆绑器中完成此任务?

I have no idea, how can I achieve this? or Is there any different way beside's ENV. to accomplish this inside bundler like Webpack or Rollup?

任何建议都会有所帮助!

Any suggestion would be helpful!

推荐答案

如果您正在寻找如何根据env变量将方法附加到原型的方法,则可以执行以下操作:

If you're looking to how to attach methods to prototype according to env variable, you could do something like:

export interface LayerManager {
    forCms: Function;
    forProduct: Function;
}

export class LayerManager {
    firstName: string = '';
    constructor(name: string) {}
}

// @ts-ignore
if (process.env.NODE_ENV === "cms") {
    LayerManager.prototype.forCms = function() {}
} else {
    LayerManager.prototype.forProduct = function() {}
}

游乐场

这篇关于有没有一种方法可以选择在捆绑软件中添加功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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