Skip to content

Commit 4936d2e

Browse files
authoredNov 30, 2023
fix(compiler-sfc): throw error when failing to load TS during type resolution (#8883)
1 parent 5199a12 commit 4936d2e

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed
 

‎packages/compiler-sfc/src/script/resolveType.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,25 @@ let loadTS: (() => typeof TS) | undefined
719719
* @private
720720
*/
721721
export function registerTS(_loadTS: () => typeof TS) {
722-
loadTS = _loadTS
722+
loadTS = () => {
723+
try {
724+
return _loadTS()
725+
} catch (err: any) {
726+
if (
727+
typeof err.message === 'string' &&
728+
err.message.includes('Cannot find module')
729+
) {
730+
throw new Error(
731+
'Failed to load TypeScript, which is required for resolving imported types. ' +
732+
'Please make sure "typescript" is installed as a project dependency.'
733+
)
734+
} else {
735+
throw new Error(
736+
'Failed to load TypeScript for resolving imported types.'
737+
)
738+
}
739+
}
740+
}
723741
}
724742

725743
type FS = NonNullable<SFCScriptCompileOptions['fs']>
@@ -768,7 +786,12 @@ function importSourceToScope(
768786
scope: TypeScope,
769787
source: string
770788
): TypeScope {
771-
const fs = resolveFS(ctx)
789+
let fs: FS | undefined
790+
try {
791+
fs = resolveFS(ctx)
792+
} catch (err: any) {
793+
return ctx.error(err.message, node, scope)
794+
}
772795
if (!fs) {
773796
return ctx.error(
774797
`No fs option provided to \`compileScript\` in non-Node environment. ` +
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
if (typeof require !== 'undefined') {
2-
try {
3-
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
4-
} catch (e) {}
2+
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
53
}

0 commit comments

Comments
 (0)
Please sign in to comment.