1

In Angular 19 app i'm trying to remove all data-testid attributes from html files if the production environment is running, but the onLoad() method doesn't run. The first console.log works every time so the plugin is properly enabled.

import type { Plugin, PluginBuild } from 'esbuild';
import fs from 'fs';

const removeTestIdPlugin: Plugin = {
  name: 'remove-testid-attributes',
  setup(build: PluginBuild ): void {

    console.log('start'); // it runs
    build.onLoad({ filter: /\.html$/ }, async (args) => {
      let file = await fs.promises.readFile(args.path, 'utf8');
      console.log(file); // never runs

      file = file.replace(/\s+data-testid="[^"]*"/g, '');

      return {
        contents: file,
        loader: 'text',
      };
    });
  },
};

export default removeTestIdPlugin;

im including it like this in angular.json file:

"architect": {
      "build": {
        "builder": "@angular-builders/custom-esbuild:application",// custom esbuild
        "options": {
          "outputPath": "dist/frontend",
          "index": "src/index.html",
          "browser": "src/main.ts",
          "polyfills": ["zone.js"],
          "tsConfig": "tsconfig.app.json",
          "inlineStyleLanguage": "css",
          "assets": [
            {
              "glob": "**/*",
              "input": "public"
            }
          ],
          "styles": ["src/styles.css"],
          "scripts": [],
          "plugins": ["./esbuild-transform.ts"] // here
        },
      }
}

What am i doing wrong?

4
  • How exactly are you including your custom plugin to the build system? Does the processing happen before or after Angular's processing? Commented Apr 1 at 11:00
  • @JSONDerulo according to @angular-builders/custom-esbuild in angular.json file, i edited my post. Im runnung ng build command for this Commented Apr 1 at 11:11
  • I guess that is because angular htmls are turned into ts before esbuild is doing anything Commented Apr 1 at 19:09
  • @Andrei I thought so too, so I checked with { filter: /.*/} if the second console.log would run for any file, but unfortunately it didn't work either, it was as if build.onLoad never run Commented Apr 2 at 8:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.