0

I want to strip a ts code before angular compiler. Here is how it looks like before the compilation:

...
 /* prod:start */
 const title: string = "Production title!"
 /* prod:end */
 /* dev:start */
 const title: string = "Dev title!"
 /* dev:end */

To strip ts code i'm using webpack-remove-block-loader loader in custom-webpack configurations

module.exports = {
  module: {
    rules: [
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        enforce: 'pre',
        use: [
          {
            loader: 'webpack-remove-block-loader',
            options: {
              active: true,
              blocks: excludedClients.blocks,
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
    ...

To make custom-webpack works i'm using @angular-builders/custom-webpack with this config in angular.json:

"build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./custom-webpack.config.js",
              "mergeRules": {
                "module": {
                  "rules": "prepend"
                }
              },
    ...

The problem is that it doesn't work. If i remove enforce: 'pre' flag from custom-webpack config for loader that strip ts code - it'll remove blocks of code from already compiled ts files, that's wrong, otherwise with enforce: 'pre' it should works before angular compiler, but it doesn't, code is still have comments for both types of build(prod/dev).

And yes, i know that it's possible to use options from environment.ts files but it's not flexible enough. Thanks!

rkalita
  • 575
  • 4
  • 16
  • Since your goal seems to be to manage different build configurations, I strongly suggest to use the `configurations` section in the `angular.json` file. You can define file replacements for different `environment.xxx.ts` files there. https://angular.io/guide/build#configure-target-specific-file-replacements – derpirscher Jan 16 '21 at 16:39
  • @derpirscher as i wrote, i know about this ability, but it's not what i want. I need to strip blocks with parts of routes/imports/object properties and so on. As an example you need to use one or other component depends on build configuration(`angular.json`). In this case you can use `environment` properties(if/else), but you also need to remove an import of component that you don't need, otherwise angular compiler will compile unnecessary code(component/module) – rkalita Jan 17 '21 at 03:41

0 Answers0