-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[How To] Use with tsx
runner
#148
Comments
For those interested in The trick is to get it to load your transformer before For example, with modern register-based transformers (and having previously used
or this for legacy loader-based transformers:
This will cause your transformer to load before Also, if you have custom
I have this all working with node 22.3.0, ts-patch 3.2.1, typescript 5.5.3 and tsx 4.16.2. |
I am probably stupid but it doesn't work for me. after adding a simple console log into my transformer I can see that what am I missing? |
I don't recall the details well, as I just played around until I got it to work (I'm no expert in this). One that sticks out is your comment "however the default function (is not) being called". If the transformer exports a default function that likely means it's a "loader" based transformer. Perhaps try Given I use a register-based export, I don't know if this will help, but I figure more information is better than less?! In my
The import path from 'path';
import { pathToFileURL } from 'node:url';
import { register } from 'node:module';
const loaderPath = path.resolve('node_modules/@wessberg/di-compiler/dist/esm/loader.js');
const loaderURL = pathToFileURL(loaderPath).href;
register(loaderURL, pathToFileURL('./')); This registers the transformed loader. You can likely skip all the path munging - it's just to help make the path resolution work since Now my usage is based on register style transformer loader. See here for the source of the loader I'm using, but for reference here is it pulled together post-packing as a single file: import TS__default from 'typescript';
import path from 'path';
import fs from 'fs/promises';
import urlModule from 'url';
import { resolveOptions, ALLOWED_EXTENSIONS, transform } from './common/common.js';
const transformOptions = resolveOptions(TS__default);
const load = async (url, context, nextLoad) => {
var _a;
if (ALLOWED_EXTENSIONS.has(path.extname(url))) {
const fileName = urlModule.fileURLToPath(url);
const rawSource = await fs.readFile(fileName, "utf-8");
if (rawSource != null) {
const { code: source } = transform(rawSource.toString(), fileName, {
...transformOptions,
typescript: TS__default
});
return {
format: (_a = context.format) !== null && _a !== void 0 ? _a : "module",
shortCircuit: true,
source
};
}
}
// Defer to the next hook in the chain.
return nextLoad(url, context);
}; Not a lot to learn here, unless you need to write your own register loader. Maybe your transformer has a register-based loader that you can use? If not then perhaps the above might help you write one (assuming you can't get a loader-based transformer working with I wish I could be more prescriptive in fixing your problem, but I don't fully understand it either. Hope something here helps you find a solution. Good luck! |
ok I get it. this is much more complicated than anticipated 😱 I am transforming some macros and I just inject the macro calls as functions using a loader now. thanks for the details though! |
I'm using tsx, a ts-node alternative for running
.ts
files with Node. Is there a some way to run ts-patch projects with tsx?The text was updated successfully, but these errors were encountered: