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

[PLOP] Something went wrong with reading your plop file #294

Closed
ryparker opened this issue Nov 29, 2021 · 6 comments
Closed

[PLOP] Something went wrong with reading your plop file #294

ryparker opened this issue Nov 29, 2021 · 6 comments

Comments

@ryparker
Copy link

ryparker commented Nov 29, 2021

Full error when running plop at version: ^3.0.0:

yarn run v1.22.17
$ plop
(node:68705) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
[PLOP] Something went wrong with reading your plop file /Users/ryanparker/GitHub/parliamentarian/plopfile.js:1
export default function (plop) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 0)
✨  Done in 0.51s.

Plop file at root ./plopfile.js :

export default function (plop) {
  plop.setGenerator("Component", {
    description: "Create a new component",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "What will be the Component's name?",
      },
    ],
    actions: [
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.tsx",
        templateFile: "templates/component/base/Component.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.stories.tsx",
        templateFile: "templates/component/base/Component.stories.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.test.tsx",
        templateFile: "templates/component/base/Component.test.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/index.ts",
        templateFile: "templates/component/base/index.ts.hbs",
      },
    ],
  });
}

Also tested the Typescript implementation and received a different error:

./plopfile.ts:

import { NodePlopAPI } from 'plop';

export default function (plop: NodePlopAPI) {
  plop.setGenerator("Component", {
    description: "Create a new component",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "What will be the Component's name?",
      },
    ],
    actions: [
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.tsx",
        templateFile: "templates/component/base/Component.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.stories.tsx",
        templateFile: "templates/component/base/Component.stories.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/{{name}}.test.tsx",
        templateFile: "templates/component/base/Component.test.tsx.hbs",
      },
      {
        type: "add",
        path: "src/components/{{name}}/index.ts",
        templateFile: "templates/component/base/index.ts.hbs",
      },
    ],
  });
}

Error when running plop:

yarn run v1.22.17
$ plop
[PLOP] Something went wrong with reading your plop file TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/ryanparker/GitHub/parliamentarian/plopfile.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.file: (node:internal/modules/esm/get_format:72:15)
    at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
    at defaultLoad (node:internal/modules/esm/load:13:42)
    at ESMLoader.load (node:internal/modules/esm/loader:303:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 0) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
@crutchcorn
Copy link
Member

Hmm. Are you able to share with me what "type" your package.json is? (The closest parent package.json)

It should be "type": "module" to use ESM package.json files

@ryparker
Copy link
Author

I don't have type configured in my package.json. Is it possible to use plop without changing my project to ESM?

@ryparker
Copy link
Author

Renaming the file from plopfile.js to plopfile.mjs works without me having to configure "type": "module" in package.json. But the Typescript error seems like a different problem.

@crutchcorn
Copy link
Member

FWIW you can also use module.exports without either an MJS or CJS extension if you don't want to update your package JSON as well

I mention this in the changelog a bit, but not in the README. I'll do so tomorrow

@crutchcorn
Copy link
Member

I added a section to the README (and therefore the homepage) about ESM support & package.json.

However, it's worth mentioning that your .ts error is correct. In order to use TS, you need to pre-compile your plopfile. However, do feel free to make a PR to request support for running .ts files without doing so. I'd absolutely love to add support for v4 (hopefully sooner rather than later after some code cleanup)

@crutchcorn
Copy link
Member

Closing in favor of #296

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

2 participants