繁体   English   中英

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

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

我有一个严重依赖条件编译的 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 项目中有条件地包含/排除代码块?

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

如果从 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;
};

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

补丁在这里: Angular Webpack 补丁

这很丑陋,但它有效。

暂无
暂无

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

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