core: Error bundling @oclif/core into my project

Describe the bug Hi! We (shopify CLI) have a monorepo with our CLI and multiple plugins, and @oclif/core is a dependency on all of them.

We are trying to bundle each package with its dependencies, but when including @oclif/core in the bundle we can’t make it work: Running any command will crash on line 34 of command.ts

const pjson = requireJson<PJSON>(__dirname, '..', 'package.json')

I assume is looking for the @oclif/core package.json, but since the dependency is being bundled there isn’t one. Is there a way to avoid this? I see that this pjson is only used for a debug log, can we ignore the error if the file doesn’t exist instead of crashing? I can make the PR if you agree with this approach 😃

Thanks!

To Reproduce Having a CLI, bundle it with esbuild, running any command should trigger this.

Expected behavior Everything should work normally.

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

This is an example CLI where we are bundling with esbuild: https://github.com/isaacroldan/oclif-cli-bundled. This project includes the @oclif/plugin-commands.

After cloning you can notice the dist folder already contains the output, and there is no node_modules.

Without running npm i, run:

./bin/run.js 

It will work, because @oclif/core is bundled. But it is complaining that it can’t find the commands plugin. An of course ./bin/run.js commands doesn’t work

If you then run npm install, it will create the node_modules folder and ./bin/run.js commands will work, because oclif can find it in the node_modules folder.

So things happening here:

  • the plugin is not being bundled because there is no direct import from anywhere in our code, so esbuild ignores it (you can see in the dist folder that there is no reference to it)
  • in any case, oclif will try to find a package.json for the plugin, which will be imposible because will never have a node_modules folder.

Notes:

  • to re-build just run npm run build, that runs the script in bin/bundle.js
  • To test stuff, you need to remove the node_module folder, because oclif will try to dynamically load from there.

Let me know what you think 😃

Yep, is a bit late on my timezone but I’ll prepare and share something tomorrow 😃