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

fix(css): enhance error message for missing preprocessor dependency #11485

Merged
merged 7 commits into from
Jul 31, 2023
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,10 @@ function loadPreprocessor(
return (loadedPreprocessors[lang] = _require(resolved))
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
const packageManager =
process.env.npm_config_user_agent?.split(' ')[0].split('/')[0] || 'npm'
Copy link
Contributor Author

@EastSun5566 EastSun5566 Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that the pkgFromUserAgent function can be reused in the same way as import { pkgFromUserAgent } from '@vite/create-vite' or import { pkgFromUserAgent } from '@vite/shared', but currently, it is not exposed in that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to duplicate it for now. It would be nice to move this into utils.ts though, and have a separate function that returns package manager specific commands.

e.g. with yarn and pnpm, we want yarn add and pnpm add instead. That way we can do something like ${installCommand} -D ${lang} instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right... I completely forgot that yarn does not support the i/install command. However, pnpm does support pnpm i <package_name> as an alias for pnpm add <package_name>. Nonetheless, pnpm add is the more officially supported method.

I will refactor it to utils.ts and make it more verbose. Thank you for your suggestion!

throw new Error(
`Preprocessor dependency "${lang}" not found. Did you install it?`,
`Preprocessor dependency "${lang}" not found. Did you install it? Try \`${packageManager} i -D ${lang}\`.`,
)
} else {
const message = new Error(
Expand Down