Skip to content

Commit

Permalink
Merge pull request #33 from vuejs/main
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
encourage317125 committed Jun 19, 2023
2 parents d138167 + d2c3d8b commit 8aaee63
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"version": "3.3.4",
"packageManager": "pnpm@8.4.0",
"packageManager": "pnpm@8.6.2",
"type": "module",
"scripts": {
"dev": "node scripts/dev.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../src/script/resolveType'

import ts from 'typescript'
registerTS(ts)
registerTS(() => ts)

describe('resolveType', () => {
test('type literal', () => {
Expand Down
44 changes: 28 additions & 16 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,14 @@ function resolveGlobalScope(ctx: TypeResolveContext): TypeScope[] | undefined {
}
}

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']>
Expand All @@ -723,7 +724,10 @@ function resolveFS(ctx: TypeResolveContext): FS | undefined {
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
}
Expand Down Expand Up @@ -779,22 +783,25 @@ function importSourceToScope(
} 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)
Expand Down Expand Up @@ -839,6 +846,7 @@ const tsConfigRefMap = new Map<string, string>()
function resolveWithTS(
containingFile: string,
source: string,
ts: typeof TS,
fs: FS
): string | undefined {
if (!__NODE_JS__) return
Expand All @@ -853,7 +861,7 @@ function resolveWithTS(
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
Expand Down Expand Up @@ -918,7 +926,11 @@ function resolveWithTS(
}
}

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.
Expand All @@ -940,7 +952,7 @@ function loadTSConfig(configPath: string, fs: FS): TS.ParsedCommandLine[] {
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
Expand Down
1 change: 1 addition & 0 deletions packages/vue/compiler-sfc/index.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@vue/compiler-sfc')
1 change: 1 addition & 0 deletions packages/vue/compiler-sfc/index.browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@vue/compiler-sfc'
2 changes: 1 addition & 1 deletion packages/vue/compiler-sfc/register-ts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (typeof require !== 'undefined') {
try {
require('@vue/compiler-sfc').registerTS(require('typescript'))
require('@vue/compiler-sfc').registerTS(() => require('typescript'))
} catch (e) {}
}
10 changes: 10 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
"./compiler-sfc": {
"import": {
"types": "./compiler-sfc/index.d.mts",
"browser": "./compiler-sfc/index.browser.mjs",
"default": "./compiler-sfc/index.mjs"
},
"require": {
"types": "./compiler-sfc/index.d.ts",
"browser": "./compiler-sfc/index.browser.js",
"default": "./compiler-sfc/index.js"
}
},
Expand Down Expand Up @@ -99,5 +101,13 @@
"@vue/runtime-dom": "3.3.4",
"@vue/compiler-sfc": "3.3.4",
"@vue/server-renderer": "3.3.4"
},
"peerDependencies": {
"typescript": "*"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
}
}
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8aaee63

Please sign in to comment.