From c3d98bb8f298390db0e6bf09ee952ff56777e2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 1 Aug 2023 16:36:14 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): throw errors for loading TS --- .../compiler-sfc/src/script/resolveType.ts | 27 +++++++++++++++++-- packages/vue/compiler-sfc/register-ts.js | 4 +-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index 146c454729c..516be5548d8 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -715,7 +715,25 @@ let loadTS: (() => typeof TS) | undefined * @private */ 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 check "typescript" dependency is installed.' + ) + } else { + throw new Error( + 'Failed to load TypeScript for resolving imported types.' + ) + } + } + } } type FS = NonNullable @@ -764,7 +782,12 @@ function importSourceToScope( scope: TypeScope, source: string ): 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) { return ctx.error( `No fs option provided to \`compileScript\` in non-Node environment. ` + diff --git a/packages/vue/compiler-sfc/register-ts.js b/packages/vue/compiler-sfc/register-ts.js index 7a073b3a3f8..36de2a350d6 100644 --- a/packages/vue/compiler-sfc/register-ts.js +++ b/packages/vue/compiler-sfc/register-ts.js @@ -1,5 +1,3 @@ if (typeof require !== 'undefined') { - try { - require('@vue/compiler-sfc').registerTS(() => require('typescript')) - } catch (e) {} + require('@vue/compiler-sfc').registerTS(() => require('typescript')) } From 0cfcc9c4608c9963bf4319ee6bdbba0401439435 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 30 Nov 2023 18:40:25 +0800 Subject: [PATCH 2/2] tweak message --- packages/compiler-sfc/src/script/resolveType.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index 516be5548d8..9829899bba0 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -725,7 +725,7 @@ export function registerTS(_loadTS: () => typeof TS) { ) { throw new Error( 'Failed to load TypeScript, which is required for resolving imported types. ' + - 'Please check "typescript" dependency is installed.' + 'Please make sure "typescript" is installed as a project dependency.' ) } else { throw new Error(