fantasticon icon indicating copy to clipboard operation
fantasticon copied to clipboard

[bug] - Print verbose alert of error when parsing config file instead of failing silently

Open rihards-simanovics opened this issue 8 months ago • 4 comments

Just had a weird encounter where Fantasticon refused to parse the .fantasticonrc.js even though it was formatted correctly.

The issue turned out to be with how the package.json was declaring the type of the package. It was set to "module", a default set by Vite create template, which meant that all common .js files needed to have a .cjs extension, I was only able to figure this out after manually adding the console.error(err).

Expected behaviour

Create a --verbose flag or print out a verbose error reporting on traceback and any other useful info i.e.:

user@hostpc:~/git/internal/project$ bun run generate-icons
$ fantasticon
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/git/internal/project/.fantasticonrc.js from /home/user/git/internal/project/node_modules/fantasticon/lib/cli/config-loader.js not supported.
.fantasticonrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename .fantasticonrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/user/git/internal/project/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at /home/user/git/internal/project/node_modules/fantasticon/lib/cli/config-loader.js:26:20
    at Generator.next (<anonymous>)
    at fulfilled (/home/user/git/internal/project/node_modules/fantasticon/lib/cli/config-loader.js:5:58) {
  code: 'ERR_REQUIRE_ESM'
}
SyntaxError: Unexpected token 'm', "module.exp"... is not valid JSON
    at JSON.parse (<anonymous>)
    at /home/user/git/internal/project/node_modules/fantasticon/lib/cli/config-loader.js:30:25
    at Generator.next (<anonymous>)
    at fulfilled (/home/user/git/internal/project/node_modules/fantasticon/lib/cli/config-loader.js:5:58)
Failed parsing configuration at '.fantasticonrc.js'

Actual Behaviour

All I get is Failed parsing configuration at '.fantasticonrc.js', which is needless to say is not helpful.

Also might be a good idea to add .cjs exension to a set of default imports or make this fetch dynamic by using regex.

issue blames:

  • https://github.com/tancredi/fantasticon/blob/76154ac30bbc227c69bcc94b429118e0bf74c9e5/src/cli/config-loader.ts#L21
  • https://github.com/tancredi/fantasticon/blob/76154ac30bbc227c69bcc94b429118e0bf74c9e5/src/cli/config-loader.ts#L17

rihards-simanovics avatar Jun 09 '24 04:06 rihards-simanovics