ts-patch icon indicating copy to clipboard operation
ts-patch copied to clipboard

Discussion: Consider adding support for nx transform style plugins

Open danielpza opened this issue 3 months ago • 0 comments

The nx plugin interface is slightly different from the ones accepted by ts-patch. This results in fragmentation when authoring typescript plugins, where plugins need to add support for different format expected for different tools.

Although they don't provide too much documentation, looking at the source code (1) (2) it seems they expect a plugin to either:

  • have a default export be a function, in which case it will be treated as a "before" hook
  • have optional before, after and afterDeclarations exports

This translates roughly to the after and afterDeclarations ts-patch options:

nx ts-patch
before hook default behavior (or after: false)
after hook after: true
afterDeclarations hook afterDeclarations: true

I'm not sure if there's a standard around typescript transformer plugins, so it's worth raising an issue on nx side as well. It seems ts-patch interface has been around for longer.

On the other hand I see some benefits in the nx approach, for example in typescript-transform-paths we need to define to entries in the plugin list with ts-patch if we want to transform both the emitted .js and .d.ts files:

    // Note: To transform paths for both the output .js and .d.ts files, you need both of the below entries
    "plugins": [
      // Transform paths in output .js files
      { "transform": "typescript-transform-paths" },

      // Transform paths in output .d.ts files (Include this line if you output declarations files)
      { "transform": "typescript-transform-paths", "afterDeclarations": true },
    ],

Ideal scenario for plugin authors: they export a single entry point that can be read by both ts-patch and nx (and any other tool that accepts ts transformers)

    "plugins": [
      // when the plugin exports both `before` and `afterDeclarations`, it can transform both .js and .d.ts files
      { "transform": "typescript-transform-paths" },
    ],

Reference:

Related issues:

  • https://github.com/nonara/ts-patch/issues/104
  • https://github.com/nartc/mapper/pull/556
  • https://github.com/LeDDGroup/typescript-transform-paths/issues/166

danielpza avatar Sep 05 '25 06:09 danielpza