简体   繁体   English

使用带有 Angular 13 和 Webpack 的 ifdef-loader 进行条件编译?

[英]Conditional compilation using ifdef-loader with Angular 13 and Webpack?

I have an Ionic application that relies heavily on conditional compilation where I include or exclude blocks of code based on a set of config values similar to how m4 works.我有一个严重依赖条件编译的 Ionic 应用程序,我在其中根据一组类似于 m4 工作方式的配置值来包含或排除代码块。

I had been using https://github.com/nippur72/ifdef-loader successfully for this purpose.为此,我一直在成功使用https://github.com/nippur72/ifdef-loader

I am now faced with upgrading this app from Angular 10 to 13 (Ionic 5 to 6).我现在面临将此应用程序从 Angular 10 升级到 13(Ionic 5 到 6)的问题。

ifdef-loader did not work out of the box with Angular 10 but a patch ( https://gist.github.com/tristanidoux/892469f2c345bd6c6551eb19c705a016 ) to @angular-dev-kit allowed it to run. ifdef-loader 不能与 Angular 10 开箱即用,但 @angular-dev-kit 的补丁 ( https://gist.github.com/tristanidoux/892469f2c345bd6c6551eb19c705a016 ) 允许它运行。

This patch does not work with Angular 13 as all the files have changed and crawling through as much of the source as I can I don't yet see how to create a similar patch for Angular 13.此补丁不适用于 Angular 13,因为所有文件都已更改并尽可能多地搜索源代码,我还不知道如何为 Angular 13 创建类似的补丁。

So I have been attempting to use "@angular-builders/custom-webpack": "^13.0.0" using https://medium.com/angular-in-depth/customizing-angular-cli-build-an-alternative-to-ng-eject-v2-c655768b48cc as a guide.所以我一直在尝试使用 "@angular-builders/custom-webpack": "^13.0.0" 使用https://medium.com/angular-in-depth/customizing-angular-cli-build-an-alternative -to-ng-eject-v2-c655768b48cc作为指南。

I have the following custom.webpack.config.js file modeled on the ifdef-loader documentation:我有以下以 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 }
        ]
      }
    ]
  },

Unfortunately, this does not work as it appears ifdef-loader is not getting invoked.不幸的是,如果没有调用 def-loader,这似乎不起作用。

So three questions:那么三个问题:

  1. Am I making some obvious mistake in my config?我在我的配置中犯了一些明显的错误吗?

  2. Has anyone gotten ifdef-loader working with Angular 13?有没有人让 ifdef-loader 与 Angular 13 一起工作? If so, how?如果是这样,如何?

  3. Alternatively, is there some other solution for conditionally including/excluding blocks of code in an Angular 13 project?或者,是否有其他解决方案可以在 Angular 13 项目中有条件地包含/排除代码块?

Any pointers or suggestions would be greatly appreciated.任何指针或建议将不胜感激。

If export webpack configuration as function from webpack.config.js it's works:如果从 webpack.config.js 将 webpack 配置导出为 function,它是有效的:

module.exports = (config, options, targetOptions) => {
    const ifdef_opts = {
        DEBUG: config.mode === 'development',
        version: 3,
        "ifdef-verbose": true,                 // add this for verbose output
        "ifdef-triple-slash": false,           // 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 "
    };
    config.module.rules.some(({test, use}) => {
        if (use && test && 'dummy.ts'.match(test)) {
            use.push({
                loader: "ifdef-loader",
                options: ifdef_opts
            })
            return true;
        }
        return false;
    })

    return config;
};

After a week, I have been unable to determine how to insert a module into the typescript compilation pipeline in Angular's webpack implementation so I opted to create a patch for @angular-devkit/build-angular/@ngtools/webpack to call ifdef-loader directly.一周后,我无法确定如何在 Angular 的 webpack 实现中将模块插入到 typescript 编译管道中,因此我选择为 @angular-devkit/build-angular/@ngtools/webpack 创建一个补丁来调用 ifdef-loader直接地。

Patch here: Angular Webpack Patch补丁在这里: Angular Webpack 补丁

It's ugly but it works.这很丑陋,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM