gpt4 book ai didi

Angular 树抖动不剥离开发代码,我应该寻找什么?

转载 作者:行者123 更新时间:2023-12-03 15:47:25 24 4
gpt4 key购买 nike

我正在使用所有与 Angular 相关的软件包的最新版本(所以是 Angular 10)。
我想向组件添加一些代码,但我只希望这些代码存在于开发中,而不是生产版本中。它需要在产品构建中完全剥离。我找到了 this comment ,这表明环境会自动执行此操作(因为它们是 const )。
我尝试在我的应用程序中使用该确切代码,但开发代码仍在生产版本中。我将代码复制到我用 ng new 制作的新测试应用程序中,它确实在那里正常工作。
我应该寻找什么东西,我该如何解决这个问题?这是否可能是因为我有 CommonJS 依赖项,如果是这样,我可以做些什么(因为我无法删除这些依赖项)?
一些注意事项:

  • 已在 angular-cli 存储库中打开了一个问题 here .
  • environment对象从未写入代码库中的任何地方,我已经彻底搜索过。 (反正也只用在少数地方。)
  • if (false) { } 为界的代码被正确剥离。
  • environment{.prod}.ts 末尾删除服务导出不能解决问题。
  • 删除所有 CommonJS 依赖项并不能解决问题。

  • 这是 environment.prod.ts ( environment.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"
    }
    }

    最佳答案

    您可以应用与 environment.ts 相同的逻辑;创建 main.prod.ts (没有特定于开发人员的代码)和 main.dev.ts (使用特定于开发人员的代码),然后使用 fileReplacements在你的配置中。
    prod 的配置是:

     "fileReplacements": [
    ...
    {
    "replace": "src/main.ts",
    "with": "src/main.prod.ts"
    }

    关于Angular 树抖动不剥离开发代码,我应该寻找什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63415056/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com