plopjs / plop

Consistency Made Simple
http://plopjs.com
MIT License
7.06k stars 277 forks source link

plopfile.ts Typescript support #423

Open benallfree opened 5 months ago

benallfree commented 5 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

cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts

Node.js v20.5.1 and below

cross-env NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts
urizennnn commented 5 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

benallfree commented 5 months ago

In your npm scripts or when running from the command line.

NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts

urizennnn commented 5 months ago

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"
  }
}
benallfree commented 5 months ago
   "plop": "NODE_OPTIONS='--import tsx' npx plop --plopfile=plopfile.ts"
urizennnn commented 5 months ago
   "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'
}
benallfree commented 5 months ago

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

urizennnn commented 5 months ago

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

benallfree commented 5 months ago

@urizennnn If you have time please test #427

crutchcorn commented 5 months ago

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)

benallfree commented 5 months ago

Agreed 👍

Coderclc commented 4 months ago
benallfree commented 4 months ago

@Coderclc Can you send a PR please?

Coderclc commented 4 months ago

@Coderclc Can you send a PR please?

Fix the use of plofile.ts in README.md?

benallfree commented 4 months ago

@Coderclc Yes, if you have time and have tested it. I believe --plopfile=plopfile.ts is also not required.