nx icon indicating copy to clipboard operation
nx copied to clipboard

Writing custom Task runner using esm modules and/or typescript.

Open khludenevav opened this issue 1 year ago • 1 comments

Description

I'm using custom tasks runner in order to customize cache. And it works fine, but I see 2 issues

  1. it only supports cjs modules. Because uses require import. image

  2. I don't want to publish my plugin for my perspective it should be local. Could Nx support executor written on TS?

How I currently reference plugin in nx.json

"tasksRunnerOptions": {
    "default": {
      "runner": "./libs/nx-task-runner",
      "options": {
        "captureStderr": true
      }
    }
  },

libs/nx-task-runner/package.json content:

...
  "main": "lib/index.js",
...

Motivation

Publishing task runner package is additional step and slows down the process of making any changes in it.

Suggested Implementation

Probably somehow using ts-node will help to launch code from ts package.

Alternate Implementations

¯\(ツ)

khludenevav avatar Feb 15 '24 22:02 khludenevav

ESM support for plugins is tracked here: #15682, no ETA on that. We could look into typescript for the tasks runner though, could be interesting.

AgentEnder avatar Feb 16 '24 20:02 AgentEnder

Temporary solution, add a script to your package.json to compile it from TS to JS and commit:

"scripts": {
    "build:custom-runner": "tsc tools/task-runners/custom-runner/index.ts",

work933k avatar Apr 17 '24 10:04 work933k

@khludenevav you can register runtime ts transpiler(ts-node or swc) via index.js like

require('ts-node').register({ /* options */ });

module.exports = require('./src/index.ts');

cainrus avatar May 07 '24 09:05 cainrus