question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to extend the cli

See original GitHub issue

Clear and concise description of the problem

I’m currently creating a SSR plugin for Vite, I would like to give the user the ability to start the server as SPA mode too.

But if I run something like vite --spa will give an error:

throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);

Suggested solution

just add a cli option to the Plugin interface, pass the vite cli object to it, this pseudocode shows it adds an additional option to the serve command.

{
  name: 'foo',
  cli(cli: CAC) {
    const serveCommand = cli.commands.find(({name})=> name==='[root]')
    serveCommand.option('--spa', `start dev server in SPA mode`)
  }
}

I’ve been saw lots of plugin creators create a separated cli just for there plugin, it really should be a part of the vite command itself, this way, fits so natural and saving time, plus giving a change to add additional options to the default commands.

What you guys think about this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
patak-devcommented, Apr 7, 2021

If we allow extending the vite cli, we need to start thinking about possible collisions with future features. At this point, I agree that it is better to avoid the extra complexity in core. You could create an extensible-vite package to be used by others if they think that this approach has merits.

0reactions
yuanjinyongcommented, Jul 8, 2021

I find there is a “allowUnknownOptions” property in vite/dist/node/cli.js. And it used at

  checkUnknownOptions() {
    const {options, globalCommand} = this.cli;
    if (!this.config.allowUnknownOptions) {
      for (const name of Object.keys(options)) {
        if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) {
          throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
        }
      }
    }
  }

I tried change the code vite/dist/node/cli.js as following:

cli
    .command('build [root]').allowUnknownOptions()
    .option('--target <target>', `[string] transpile target (default: 'modules')`)

Add a allowUnknownOptions() call after command init method, then the vite build command will ignore unknown args.

But how can I set allowUnknownOptions to true do not change the source code? Because I want to pass some args to my vite plugin.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending the CLI | OpenShift Container Platform 3.11
Overview. This topic reviews how to install and write extensions for the CLI. Usually called plug-ins or binary extensions, this feature allows you...
Read more >
How to install and manage Azure CLI extensions
With extensions, you gain access to experimental and pre-release commands along with the ability to write your own CLI interfaces. This article ...
Read more >
How To Make Your CLI More Intuitive - Nordic APIs
Here are some ways to improve the CLI experience. ... increase the user's ability to reason, understand, and extend their interactions.
Read more >
What is a command-line interface (CLI)? - TechTarget
A command-line interface (CLI) is a text-based user interface (UI) used to run programs, manage computer files and interact with the computer.
Read more >
How to extend a disk in Windows using diskpart - Qualitest
You can extend the size of the space allocated to the OS. Requirements: Access to the Windows command line, and optionally a file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found