角树摇晃而不剥离开发代码,我应该寻找什么?

问题描述

我正在使用所有与Angular相关的软件包的最新版本(即Angular 10)。

我想向组件中添加一些代码,但是我只希望该代码存在于dev中,而不是在生产版本中。它需要在产品构建中完全剥离。我发现this comment,表明环境是自动执行此操作的(因为它们是const)。

我尝试在我的应用程序中使用确切的代码,但是开发代码仍在生产版本中存在。我将代码复制到了用ng new制作的新测试应用程序中,并且在该程序中可以正常工作。

我要寻找什么东西,如何解决?这可能是因为我有CommonJS依赖项,如果是这样,我可以对此做任何事情(因为我无法删除那些依赖项)?

一些注意事项:

  • angular-cli仓库here上已经出现了一个问题。
  • 我已经彻底搜索了environment对象,从未将其写入代码库中的任何位置。 (无论如何,它仅在少数几个地方使用。)
  • if (false) { }为界的代码已正确剥离。
  • environment{.prod}.ts末尾删除服务导出并不能解决问题。
  • 删除所有CommonJS依赖项不能解决问题。

这里是environment.prod.tsenvironment.ts是相同的,只是用false而不是true):

export const environment = {
  production: true
};

export * from './services/services';

以下是我正在测试的main.ts

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { environment } from 'environments/environment';
import { AppModule } from './app/app.module';

// tslint:disable:no-console

if (environment.production) {
  console.warn('this is a prod build');
  enableProdMode();
}

if (!environment.production) {
  console.warn('this is a dev build');
}

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch(err => console.error(err));

运行ng build -c my-prod-config后,这里是相关的输出代码:

o.X.production && (console.warn('this is a prod build'),Object(i.R) ()),o.X.production || console.warn('this is a dev build'),s.d().bootstrapModule(fi).catch (e=>console.error(e))

这是angular.json的相关部分:

"my-prod-config": {
  "optimization": true,"outputHashing": "all","sourceMap": false,"extractCss": true,"namedChunks": false,"aot": true,"extractLicenses": true,"vendorChunk": false,"buildOptimizer": true,"stylePreprocessorOptions": {
    "includePaths": [
      "src/styles"
    ]
  },"fileReplacements": [
    {
      "replace": "src/environments/environment.ts","with": "src/environments/environment.prod.ts"
    }
  ],"baseHref": "./"
}

这里是tsconfig.base.json

{
  "compileOnSave": false,"compilerOptions": {
    "downlevelIteration": true,"importHelpers": true,"module": "es2020","outDir": "./dist/out-tsc","sourceMap": true,"declaration": false,"moduleResolution": "node","baseUrl": "src/","experimentalDecorators": true,"allowJs": true,"target": "es2015","lib": [
      "es2018","dom"
    ],"paths": {
      "path1": [
        "app/modules/stripped-from-stack-overflow-example1"
      ],"path2": [
        "app/modules/stripped-from-stack-overflow-example2"
      ]
    }
  },"files": [
    "src/main.ts","src/polyfills.ts"
  ],"angularCompilerOptions": {
    "fullTemplateTypeCheck": true,"strictTemplates": true,"strictInjectionParameters": true
  }
}

这里是package.json

{
  "name": "my-app","version": "0.0.0","license": "MIT","scripts": {
    "section stripped": "section stripped"
  },"private": true,"dependencies": {
    "@angular/animations": "10.0.8","@angular/common": "10.0.8","@angular/compiler": "10.0.8","@angular/core": "10.0.8","@angular/forms": "10.0.8","@angular/platform-browser": "10.0.8","@angular/platform-browser-dynamic": "10.0.8","@angular/router": "10.0.8","@ng-idle/core": "9.0.0-beta.1","@ng-idle/keepalive": "9.0.0-beta.1","@ngneat/until-destroy": "8.0.1","angular-svg-icon": "10.0.0","brace": "0.11.1","caniuse-lite": "1.0.30001111","chart.js": "2.9.3","core-js": "3.6.5","css-vars-ponyfill": "2.3.2","detect-browser": "5.1.1","element-closest-polyfill": "1.0.2","file-saver": "2.0.2","fomantic-ui": "2.8.6","jsonexport": "3.0.1","moment": "2.24.0","ngx-drag-drop": "2.0.0","rxjs": "6.6.2","tslib": "^2.0.0","typeface-roboto": "0.0.75","uuid": "8.3.0","zone.js": "0.10.3"
  },"devDependencies": {
    "@angular-devkit/build-angular": "0.1000.5","@angular/cli": "10.0.5","@angular/compiler-cli": "10.0.8","@angular/language-service": "10.0.8","@types/chart.js": "2.7.54","@types/file-saver": "2.0.1","@types/uuid": "8.0.1","codelyzer": "^6.0.0","rimraf": "3.0.2","rxjs-tslint-rules": "4.34.0","ts-node": "8.10.2","tslint": "6.1.3","tslint-angular": "3.0.2","typescript": "3.9.7","webpack-bundle-analyzer": "3.8.0"
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)