mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): throw error when failing to load TS during type resolution (#8883)
This commit is contained in:
parent
5199a12f88
commit
4936d2e11a
|
@ -719,7 +719,25 @@ let loadTS: (() => typeof TS) | undefined
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export function registerTS(_loadTS: () => typeof TS) {
|
export function registerTS(_loadTS: () => typeof TS) {
|
||||||
loadTS = _loadTS
|
loadTS = () => {
|
||||||
|
try {
|
||||||
|
return _loadTS()
|
||||||
|
} catch (err: any) {
|
||||||
|
if (
|
||||||
|
typeof err.message === 'string' &&
|
||||||
|
err.message.includes('Cannot find module')
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Failed to load TypeScript, which is required for resolving imported types. ' +
|
||||||
|
'Please make sure "typescript" is installed as a project dependency.'
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'Failed to load TypeScript for resolving imported types.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FS = NonNullable<SFCScriptCompileOptions['fs']>
|
type FS = NonNullable<SFCScriptCompileOptions['fs']>
|
||||||
|
@ -768,7 +786,12 @@ function importSourceToScope(
|
||||||
scope: TypeScope,
|
scope: TypeScope,
|
||||||
source: string
|
source: string
|
||||||
): TypeScope {
|
): TypeScope {
|
||||||
const fs = resolveFS(ctx)
|
let fs: FS | undefined
|
||||||
|
try {
|
||||||
|
fs = resolveFS(ctx)
|
||||||
|
} catch (err: any) {
|
||||||
|
return ctx.error(err.message, node, scope)
|
||||||
|
}
|
||||||
if (!fs) {
|
if (!fs) {
|
||||||
return ctx.error(
|
return ctx.error(
|
||||||
`No fs option provided to \`compileScript\` in non-Node environment. ` +
|
`No fs option provided to \`compileScript\` in non-Node environment. ` +
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
if (typeof require !== 'undefined') {
|
if (typeof require !== 'undefined') {
|
||||||
try {
|
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
|
||||||
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue