Open benallfree opened 7 months ago
For anyone who would like to use a Typescript
plopfile.ts
instead of JS, I found that tsx loaders work:Node.js v20.6 and above
NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts
Node.js v20.5.1 and below
NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts
brilliant but where will this be declared
In your npm scripts or when running from the command line.
NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts
In your npm scripts or when running from the command line.
NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts
so something like this?
{
"name": "generator-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"plop": "plop --import tsx' npx plop --plopfile=plopfile.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/yeoman-generator": "^5.2.14",
"hygen": "^6.2.11",
"plop": "^4.0.1",
"yeoman-generator": "^7.1.1"
}
}
"plop": "NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts"
"plop": "NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts"
PS C:\Users\Victor\Desktop\generator-test> npm run plop
> generator-test@1.0.0 plop
> NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts
'NODE_OPTIONS' is not recognized as an internal or external command,
operable program or batch file.
so i updated that and used
" plop": "npx --node-arg='--import' --node-arg='tsx' plop --plopfile=plopfile.ts"
but got this
PS C:\Users\Victor\Desktop\generator-test> npm run plop
> generator-test@1.0.0 plop
> npx --node-arg='--import' --node-arg='tsx' plop --plopfile=plopfile.ts
npx: the --node-arg argument has been removed.
npx: the --node-arg argument has been removed.
See `npm help exec` for more information
[PLOP] Something went wrong with reading your plop file TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\Victor\Desktop\generator-test\plopfile.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
at defaultLoad (node:internal/modules/esm/load:143:22)
at async ModuleLoader.load (node:internal/modules/esm/loader:403:7)
at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:45)
at async link (node:internal/modules/esm/module_job:78:21) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
That error means you are using Node <v20.6. For that, you need to use --loader
instead of --import
.
Try this:
Node 20.6+
cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts
Node <20.6
cross-env NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts
That error means you are using Node <v20.6. For that, you need to use
--loader
instead of--import
.Try this:
Node 20.6+
cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts
Node <20.6
cross-env NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts
i'm on node v21.7.1
@urizennnn If you have time please test #427
The original text of this issue at the top is a WONDERFUL solution to this problem. So nice, in fact, that I vote we DONT support TS files in Plop and instead document that as our official support (not joking)
Agreed 👍
cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts
Error 'node: internal/process/esm loader: 34'
InternalBinding ('errors'). triggerUncaughtException(
^
Error [ERR-MODULE-NOT-FOUND]: Cannot find module“
After my testing, he treated plop as a file for TSX to recognize, rather than believing that plop is an instruction
After importing, it needs to be followed by an =
--import=tsx
Using cross-env NODE_OPTIONS='--import=tsx' plop --plopfile=plopfile.ts
can solve this problem
@Coderclc Can you send a PR please?
@Coderclc Can you send a PR please?
Fix the use of plofile.ts in README.md?
@Coderclc Yes, if you have time and have tested it. I believe --plopfile=plopfile.ts
is also not required.
For anyone who would like to use a Typescript
plopfile.ts
instead of JS, I found that tsx loaders work:Node.js v20.6 and above
Node.js v20.5.1 and below