【问题标题】:Conditional compilation using ifdef-loader with Angular 13 and Webpack?使用 ifdef-loader 和 Angular 13 和 Webpack 进行条件编译?
【发布时间】:2022-01-10 23:38:38
【问题描述】:

我有一个 Ionic 应用程序,它严重依赖条件编译,我根据一组类似于 m4 的工作方式的配置值包含或排除代码块。

我已经成功地使用了https://github.com/nippur72/ifdef-loader

我现在面临着将此应用从 Angular 10 升级到 13(Ionic 5 到 6)。

ifdef-loader 在 Angular 10 中无法开箱即用,但 @angular-dev-kit 的补丁 (https://gist.github.com/tristanidoux/892469f2c345bd6c6551eb19c705a016) 允许它运行。

此补丁不适用于 Angular 13,因为所有文件都已更改并尽可能多地通过源代码进行爬网我还没有看到如何为 Angular 13 创建类似的补丁。

所以我一直在尝试使用 "@angular-builders/custom-webpack": "^13.0.0" 使用 https://medium.com/angular-in-depth/customizing-angular-cli-build-an-alternative-to-ng-eject-v2-c655768b48cc 作为指导。

我有以下基于 ifdef-loader 文档的 custom.webpack.config.js 文件:

// ifdef-loader config

const opts = {
   APP: false,
   WEB: true,
   DEBUG: true,
   version: 3,
   "ifdef-verbose": true,                 // add this for verbose output
   "ifdef-triple-slash": true,           // add this to use double slash comment instead of default triple slash
   "ifdef-fill-with-blanks": true,         // add this to remove code with blank spaces instead of "//" comments
   "ifdef-uncomment-prefix": "// #code "  // add this to uncomment code starting with "// #code "
};

module.exports = {
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
          { loader: 'ts-loader' },
          { loader: 'ifdef-loader', options: opts }
        ]
      }
    ]
  },

不幸的是,这不起作用,因为如果def-loader 没有被调用。

所以三个问题:

  1. 我在我的配置中犯了一些明显的错误吗?

  2. 有没有人让 ifdef-loader 与 Angular 13 一起工作?如果有,怎么做?

  3. 或者,是否有其他解决方案可以在 Angular 13 项目中有条件地包含/排除代码块?

任何指针或建议将不胜感激。

【问题讨论】:

    标签: typescript ionic-framework webpack conditional-compilation angular13


    【解决方案1】:

    一周后,我无法确定如何在 Angular 的 webpack 实现中将模块插入到 typescript 编译管道中,所以我选择为 @angular-devkit/build-angular/@ngtools/webpack 创建一个补丁来调用ifdef-loader 直接。

    在这里打补丁:Angular Webpack Patch

    虽然很丑,但是很管用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2017-03-25
      • 2011-03-27
      • 1970-01-01
      • 2019-10-31
      • 2019-10-07
      相关资源
      最近更新 更多