Skip to content

Commit 65e5c22

Browse files
authored
fix(css): enhance error message for missing preprocessor dependency (#11485)
1 parent 3aab14e commit 65e5c22

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
emptyCssComments,
4646
generateCodeFrame,
4747
getHash,
48+
getPackageManagerCommand,
4849
isDataUrl,
4950
isExternalUrl,
5051
isObject,
@@ -1694,8 +1695,9 @@ function loadPreprocessor(
16941695
return (loadedPreprocessors[lang] = _require(resolved))
16951696
} catch (e) {
16961697
if (e.code === 'MODULE_NOT_FOUND') {
1698+
const installCommand = getPackageManagerCommand('install')
16971699
throw new Error(
1698-
`Preprocessor dependency "${lang}" not found. Did you install it?`,
1700+
`Preprocessor dependency "${lang}" not found. Did you install it? Try \`${installCommand} -D ${lang}\`.`,
16991701
)
17001702
} else {
17011703
const message = new Error(

packages/vite/src/node/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,25 @@ const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g
12571257
export function escapeRegex(str: string): string {
12581258
return str.replace(escapeRegexRE, '\\$&')
12591259
}
1260+
1261+
type CommandType = 'install' | 'uninstall' | 'update'
1262+
export function getPackageManagerCommand(
1263+
type: CommandType = 'install',
1264+
): string {
1265+
const packageManager =
1266+
process.env.npm_config_user_agent?.split(' ')[0].split('/')[0] || 'npm'
1267+
switch (type) {
1268+
case 'install':
1269+
return packageManager === 'npm' ? 'npm install' : `${packageManager} add`
1270+
case 'uninstall':
1271+
return packageManager === 'npm'
1272+
? 'npm uninstall'
1273+
: `${packageManager} remove`
1274+
case 'update':
1275+
return packageManager === 'yarn'
1276+
? 'yarn upgrade'
1277+
: `${packageManager} update`
1278+
default:
1279+
throw new TypeError(`Unknown command type: ${type}`)
1280+
}
1281+
}

0 commit comments

Comments
 (0)