本文介绍了如何在进行余烬生产构建时隐藏.hbs和.js中的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的Ember应用程序是2.8版开发的,我们遵循POD结构.

Our Ember application is developed in 2.8 version and we follow POD structure.

我们正在生成以下两个不同的版本

We are generating two different builds as below

1)对于我们的开发服务器:无需更改代码,只需构建

1) For our dev server : No change in code,just do build

2)对于我们的测试服务器:在HBS中删除一个部分,并从router.js中删除一条路由,然后进行构建

2) For our test server: Delete a section in HBS and also remove a route from router.js then do build

然后我们使用" npm运行脚本构建"进行构建,该构建在以下package.json中配置

And we take build using "npm run-scripts build" which is configured in our package.json as below

"scripts": {
    "build": "ember build",
    "start": "ember server",
    "test" : "ember test"
  },

我想知道在ember 2.8v中我是否可以写一个条件以根据构建删除该部分.

I would like to know in ember 2.8v can i have a condition written to remove the section based on build.

就像我给出 npm运行脚本buildDev 一样,它将进行常规构建

Like if i give npm run-scripts buildDev it will do the regular build

如果我给出 npm运行脚本buildTest ,它将删除部分并给出一个构建

and if i give npm run-scripts buildTest it will do sections removals and give a build

但在package.json中,两者都将配置为

but in package.json both will be configured like

"scripts": {
    "build"    : "ember build",
    "buildDev" : "ember build --environment=production",
    "buildTest": "ember build --environment=production",
    "start": "ember server",
    "test" : "ember test"
  },

推荐答案

您是否只想禁用某个部分,还是需要从构建中删除?

Do you only want to disable a section or do you need to remove it from the build?

如果您只想禁用某些功能,这很简单:

Its pretty easy if you want to only disable something:

首先,您可以在 config/environment.js 中使用环境变量.像这样:

First you can use environment variables in your config/environment.js. So something like this:

if(process.env.DEV_BUILD) {
  ENV.disableSomething = true;
}

在您的 package.json 中,您可以执行以下操作:

In your package.json you could do this:

"build": "cross-env DEV_BUILD=1 ember build",

这使用 cross-env 来设置Windows和* nix系统中的env变量.

This uses cross-env to set the env variable in windows and *nix systems.

然后您可以导入配置并使用它:

You can then import the config and use it:

import ENV from 'hss/config/environment';

...

if(!ENV.disableSomething) {
  this.route('foo');
}

...


但是,如果您想实际删除一些代码,则可以为此编写自己的插件,以将某些变量替换为 true false .然后缩小器将删除代码.


However if you want to actually remove some code you could write your own addon for this to replace some variable with true or false. then the minifier will remove the code.

这篇关于如何在进行余烬生产构建时隐藏.hbs和.js中的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:43