Skip to content

Commit d2c3d8b

Browse files
committed
perf(compiler-sfc): lazy require typescript
1 parent 6f45f76 commit d2c3d8b

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '../../src/script/resolveType'
1111

1212
import ts from 'typescript'
13-
registerTS(ts)
13+
registerTS(() => ts)
1414

1515
describe('resolveType', () => {
1616
test('type literal', () => {

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

+28-16
Original file line numberDiff line numberDiff line change
@@ -708,13 +708,14 @@ function resolveGlobalScope(ctx: TypeResolveContext): TypeScope[] | undefined {
708708
}
709709
}
710710

711-
let ts: typeof TS
711+
let ts: typeof TS | undefined
712+
let loadTS: (() => typeof TS) | undefined
712713

713714
/**
714715
* @private
715716
*/
716-
export function registerTS(_ts: any) {
717-
ts = _ts
717+
export function registerTS(_loadTS: () => typeof TS) {
718+
loadTS = _loadTS
718719
}
719720

720721
type FS = NonNullable<SFCScriptCompileOptions['fs']>
@@ -723,7 +724,10 @@ function resolveFS(ctx: TypeResolveContext): FS | undefined {
723724
if (ctx.fs) {
724725
return ctx.fs
725726
}
726-
const fs = ctx.options.fs || ts.sys
727+
if (!ts && loadTS) {
728+
ts = loadTS()
729+
}
730+
const fs = ctx.options.fs || ts?.sys
727731
if (!fs) {
728732
return
729733
}
@@ -779,22 +783,25 @@ function importSourceToScope(
779783
} else {
780784
// module or aliased import - use full TS resolution, only supported in Node
781785
if (!__NODE_JS__) {
782-
ctx.error(
786+
return ctx.error(
783787
`Type import from non-relative sources is not supported in the browser build.`,
784788
node,
785789
scope
786790
)
787791
}
788792
if (!ts) {
789-
ctx.error(
790-
`Failed to resolve import source ${JSON.stringify(source)}. ` +
791-
`typescript is required as a peer dep for vue in order ` +
792-
`to support resolving types from module imports.`,
793-
node,
794-
scope
795-
)
793+
if (loadTS) ts = loadTS()
794+
if (!ts) {
795+
return ctx.error(
796+
`Failed to resolve import source ${JSON.stringify(source)}. ` +
797+
`typescript is required as a peer dep for vue in order ` +
798+
`to support resolving types from module imports.`,
799+
node,
800+
scope
801+
)
802+
}
796803
}
797-
resolved = resolveWithTS(scope.filename, source, fs)
804+
resolved = resolveWithTS(scope.filename, source, ts, fs)
798805
}
799806
if (resolved) {
800807
resolved = scope.resolvedImportSources[source] = normalizePath(resolved)
@@ -839,6 +846,7 @@ const tsConfigRefMap = new Map<string, string>()
839846
function resolveWithTS(
840847
containingFile: string,
841848
source: string,
849+
ts: typeof TS,
842850
fs: FS
843851
): string | undefined {
844852
if (!__NODE_JS__) return
@@ -853,7 +861,7 @@ function resolveWithTS(
853861
const normalizedConfigPath = normalizePath(configPath)
854862
const cached = tsConfigCache.get(normalizedConfigPath)
855863
if (!cached) {
856-
configs = loadTSConfig(configPath, fs).map(config => ({ config }))
864+
configs = loadTSConfig(configPath, ts, fs).map(config => ({ config }))
857865
tsConfigCache.set(normalizedConfigPath, configs)
858866
} else {
859867
configs = cached
@@ -918,7 +926,11 @@ function resolveWithTS(
918926
}
919927
}
920928

921-
function loadTSConfig(configPath: string, fs: FS): TS.ParsedCommandLine[] {
929+
function loadTSConfig(
930+
configPath: string,
931+
ts: typeof TS,
932+
fs: FS
933+
): TS.ParsedCommandLine[] {
922934
// The only case where `fs` is NOT `ts.sys` is during tests.
923935
// parse config host requires an extra `readDirectory` method
924936
// during tests, which is stubbed.
@@ -940,7 +952,7 @@ function loadTSConfig(configPath: string, fs: FS): TS.ParsedCommandLine[] {
940952
if (config.projectReferences) {
941953
for (const ref of config.projectReferences) {
942954
tsConfigRefMap.set(ref.path, configPath)
943-
res.unshift(...loadTSConfig(ref.path, fs))
955+
res.unshift(...loadTSConfig(ref.path, ts, fs))
944956
}
945957
}
946958
return res
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if (typeof require !== 'undefined') {
22
try {
3-
require('@vue/compiler-sfc').registerTS(require('typescript'))
3+
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
44
} catch (e) {}
55
}

0 commit comments

Comments
 (0)