esyes v1.0.3
esyes
Run your TypeScript files quickly and with more positivity.
esyes transforms your JavaScript and TypeScript files using
esbuild-kit. It works seamlessly and
requires no configuration to get the job done.
It also translates import.meta.env features on the fly, so
you can use JavaScript or TypeScript modules written with import.meta.env
directly in Node.
Hat tip to @NullVoxPopuli for the idea, originally implemented in this PR in the Starbeam repository.
Table of Contents
- Install
- Usage: Instead of the
nodeCommand - Usage: As a Loader
import.meta.envSupport- Advanced: Internals Tracing
- Maintainers
- Contributing
- License
Install
When using esyes to run npm scripts, install via your package manager.
$ pnpm i -D esyesYou can also install esyes globally via your package manager.
$ pnpm i -g esyesYou can also install esyes with volta, which allows you
to bind it to a node version.
$ volta install esyes node@20If you install
esyesvia volta and you're using it in a project with a pinned version of Node in its package.json,esyeswill automatically use that version.
Usage: Instead of the node Command
$ cat hi.ts
const hello = "hello" as const;
console.log(hello);
$ esyes hi.ts
helloAll node flags and environment variables are passed through to node, so it's a
true drop-in replacement for the node command.
There's one divergence:
nodewithout any arguments will run the Node REPL, whileesyeswith no arguments prints usage information.
Usage: As a Loader
You can use esyes directly as a Node
loader without any
experimental warnings.
$ cat hi.ts
const hello = "hello" as const;
console.log(hello);
$ node --import esyes hi.ts
hello
esyesuses Node's new module.register API. This is the API that the warnings you might have seen advise you to use.
import.meta.env Support
esyes will automatically transform many of the import.meta.env features
supported by Vite so that you can
use them directly in your JavaScript or TypeScript code.
$ cat hi.ts
const hello = "hello" as const;
if (import.meta.env.DEV) {
console.log(hello);
} else {
console.log("not in dev mode!");
}
$ node --import esyes hi.ts
hello
$ MODE=prod node --import esyes hi.ts
not in dev modeThis transform applies to all files, including files in your node_modules,
which makes using import.meta.env transparent, even if some of the code you're
working on is in node_modules (such as when working in monorepos).
The transform is expected to be extremely fast when no import.meta.env is used
(it just does a quick check for import.meta.env before doing any other work).
Advanced: Internals Tracing
If the loader isn't doing what you expect, you can enable trace logging to see all of the files that the loader is processing, and the processing steps it's taking.
$ cat hi.ts
const hello = "hello" as const;
if (import.meta.env.DEV) {
console.log(hello);
} else {
console.log("not in dev mode!");
}
$ EYES_LOADER_LOG=log.txt node --import esyes hi.ts
hello
$ cat log.txt
load: "file:///.../hi.ts"
search for "import.meta.env" (from 0):
found at: 23
lookahead for ".":
found: "38..39"
lookahead for /^(MODE|DEV|PROD|TRACE)/:
found: "39..42"
replacing:
range: "23..42"
with: "true"
search for "import.meta.env": "not found"Maintainers
Contributing
License
MIT © 2023 Yehuda Katz