nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23k stars 2.3k forks source link

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

Open khludenevav opened 6 months ago

khludenevav commented 6 months ago

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

¯\(ツ)

AgentEnder commented 6 months ago

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.

work933k commented 4 months ago

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",
cainrus commented 3 months ago

@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');