Skip to content
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

plopfile.ts Typescript support #423

Open
benallfree opened this issue Apr 2, 2024 · 14 comments
Open

plopfile.ts Typescript support #423

benallfree opened this issue Apr 2, 2024 · 14 comments

Comments

@benallfree
Copy link
Contributor

benallfree commented Apr 2, 2024

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
Copy link

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
Copy link
Contributor Author

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

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

@urizennnn
Copy link

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
Copy link
Contributor Author

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

@urizennnn
Copy link

urizennnn commented Apr 5, 2024

   "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
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor Author

@urizennnn If you have time please test #427

@crutchcorn
Copy link
Member

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
Copy link
Contributor Author

Agreed 👍

@Coderclc
Copy link

Coderclc commented Apr 30, 2024

  • node -> v20.11.1
  • system -> window
    When I use the command 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

@benallfree
Copy link
Contributor Author

@Coderclc Can you send a PR please?

@Coderclc
Copy link

@Coderclc Can you send a PR please?

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

@benallfree
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants