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

Document the use of ES6 syntax #148

Closed
VincentLanglet opened this issue Feb 19, 2019 · 5 comments
Closed

Document the use of ES6 syntax #148

VincentLanglet opened this issue Feb 19, 2019 · 5 comments

Comments

@VincentLanglet
Copy link

When I'm using

plop --plopfile plop/plopfile.ts

For the file

export default (plop) => { ... }

I get the error

/Users/vlanglet/.../plop/plopfile.ts:1
(function (exports, require, module, __filename, __dirname) { export default (plop) => {
                                                              ^^^^^^

SyntaxError: Unexpected token export

And of course it works fine with

module.exports = function (plop) { ... }

How can we use the ES6 syntax ? There is no doc about it, I think adding one could be useful.
I don't know if it's changing something but I'm using a create-react-app project, so I can't modify my babel config without ejecting...

@amwmedia
Copy link
Member

Because plop runs in nodejs and not in the browser, it relies on the capabilities of the node runtime. Currently, es modules are not supported in node directly. If you want to use them, you'll need something like esm. Otherwise, you'll need to use require() and module.exports for your node code, including the plopfile.

@VincentLanglet
Copy link
Author

@amwmedia Thanks for your fast answer.
Do you know how to use esm with plop ?

I thought the require option was here for that. But with the export default and:

plop --plopfile plop/plopfile.js --require esm

I get

TypeError: require(...) is not a function

@amwmedia
Copy link
Member

honestly, I've never tried to get this working. When I'm working in nodejs, I'm accustomed to using these types of conventions. If you're interested in digging deeper and figuring out how to allow plopfiles to work with either syntax, I'd certainly be open to changes within plop to accommodate.

@VincentLanglet
Copy link
Author

After a little debug and some tests, I found a solution:

index.js

const esmRequire = require('esm')(module);

module.exports = esmRequire('./generators').default;

generetors.js

import ... from '...';

export default plop => {
  ...
};

cspotcode pushed a commit to cspotcode/plop that referenced this issue Mar 20, 2020
@stropho stropho mentioned this issue Mar 2, 2021
@jvgeee
Copy link

jvgeee commented Jan 5, 2024

If anyone else finds this in the future (2024 and beyond) and is wondering how the hell to get Plopfiles running properly, e.g.

Something went wrong with reading your plop file TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension or SyntaxError: Unexpected token 'export' (when using export default function ())

For me the solution was to rename plopfile.js to plopfile.mjs.

You can then use ESM imports (e.g. import whatever from "cool-package") and export default

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

3 participants