vite-ssr icon indicating copy to clipboard operation
vite-ssr copied to clipboard

Update CLI to Allow Specifying Vite Config Location

Open MichealPearce opened this issue 2 years ago • 1 comments

The vite cli allows specifying the location of the config via the command using -c or --config. Currently, vite-ssr does not allow this without a workaround by using --configFile instead. This could be fixed and make vite-ssr more inline with vite's cli command by updating the bit of code linked below to look for -c or --config and mapping it to configFile.

https://github.com/frandiox/vite-ssr/blob/fad2ca867992e544110cbc228cba3fc6041222fa/src/cli.ts#L14-L21

Simple fix would be:

for (let i = 0; i < args.length; i++) {
  const arg = args[i]
  const nextArg = args[i + 1]
  if (arg.startsWith('--')) {
    options[arg.replace('--', '')] =
      !nextArg || nextArg.startsWith('--') ? true : nextArg
  } else if((arg === '-c' || arg === '--config') && (nextArg && !nextArg.startsWith('-'))) {
    options.configFile = nextArg
  }
}

A possibly cleaner solution

const options = args.reduce((options, current, index, args) => {
	if (!current.startsWith('-')) return options
	
	const next = args[index + 1]
	const currentIsConfigOption = current === '-c' || current === '--config'
	const nextIsOptionName = next && next.startsWith('--')

	if (currentIsConfigOption)
		if (!next || nextIsOptionName) return options
		else options.configFile = next
	else if(current.startsWith('--')) {
		const optionName = current.replace('--', '')
		options[optionName] = !next || nextIsOptionName ? true : next
	}

	return options
}, {});

MichealPearce avatar Apr 10 '22 15:04 MichealPearce

The build command would also need some kind of solution I am now realizing

MichealPearce avatar Apr 11 '22 00:04 MichealPearce