}
}
-let ts: typeof TS
+let ts: typeof TS | undefined
+let loadTS: (() => typeof TS) | undefined
/**
* @private
*/
-export function registerTS(_ts: any) {
- ts = _ts
+export function registerTS(_loadTS: () => typeof TS) {
+ loadTS = _loadTS
}
type FS = NonNullable<SFCScriptCompileOptions['fs']>
if (ctx.fs) {
return ctx.fs
}
- const fs = ctx.options.fs || ts.sys
+ if (!ts && loadTS) {
+ ts = loadTS()
+ }
+ const fs = ctx.options.fs || ts?.sys
if (!fs) {
return
}
} else {
// module or aliased import - use full TS resolution, only supported in Node
if (!__NODE_JS__) {
- ctx.error(
+ return ctx.error(
`Type import from non-relative sources is not supported in the browser build.`,
node,
scope
)
}
if (!ts) {
- ctx.error(
- `Failed to resolve import source ${JSON.stringify(source)}. ` +
- `typescript is required as a peer dep for vue in order ` +
- `to support resolving types from module imports.`,
- node,
- scope
- )
+ if (loadTS) ts = loadTS()
+ if (!ts) {
+ return ctx.error(
+ `Failed to resolve import source ${JSON.stringify(source)}. ` +
+ `typescript is required as a peer dep for vue in order ` +
+ `to support resolving types from module imports.`,
+ node,
+ scope
+ )
+ }
}
- resolved = resolveWithTS(scope.filename, source, fs)
+ resolved = resolveWithTS(scope.filename, source, ts, fs)
}
if (resolved) {
resolved = scope.resolvedImportSources[source] = normalizePath(resolved)
function resolveWithTS(
containingFile: string,
source: string,
+ ts: typeof TS,
fs: FS
): string | undefined {
if (!__NODE_JS__) return
const normalizedConfigPath = normalizePath(configPath)
const cached = tsConfigCache.get(normalizedConfigPath)
if (!cached) {
- configs = loadTSConfig(configPath, fs).map(config => ({ config }))
+ configs = loadTSConfig(configPath, ts, fs).map(config => ({ config }))
tsConfigCache.set(normalizedConfigPath, configs)
} else {
configs = cached
}
}
-function loadTSConfig(configPath: string, fs: FS): TS.ParsedCommandLine[] {
+function loadTSConfig(
+ configPath: string,
+ ts: typeof TS,
+ fs: FS
+): TS.ParsedCommandLine[] {
// The only case where `fs` is NOT `ts.sys` is during tests.
// parse config host requires an extra `readDirectory` method
// during tests, which is stubbed.
if (config.projectReferences) {
for (const ref of config.projectReferences) {
tsConfigRefMap.set(ref.path, configPath)
- res.unshift(...loadTSConfig(ref.path, fs))
+ res.unshift(...loadTSConfig(ref.path, ts, fs))
}
}
return res