Skip to content

Commit 7f28cc3

Browse files
committed
fix: fix on windows, add windows test
1 parent 7e9e04c commit 7f28cc3

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,52 @@ describe('resolveType', () => {
12341234
])
12351235
})
12361236

1237+
test.runIf(process.platform === 'win32')(
1238+
'node subpath imports on Windows',
1239+
() => {
1240+
const files = {
1241+
'C:\\Test\\package.json': JSON.stringify({
1242+
imports: {
1243+
'#t1': '.\\t1.ts',
1244+
'#t2': '..\\t2.ts',
1245+
'#t3': 'C:\\Test/t3.ts',
1246+
'#o/*.ts': '.\\Other\\*.ts',
1247+
},
1248+
}),
1249+
'C:\\Test\\t1.ts': 'export type T1 = { foo: string }',
1250+
'C:\\t2.ts': 'export type T2 = { bar: number }',
1251+
'C:\\Test\\t3.ts': 'export type T3 = { baz: number }',
1252+
'C:\\Test\\Other\\t4.ts': 'export type T4 = { qux: string }',
1253+
}
1254+
1255+
const { props, deps } = resolve(
1256+
`
1257+
import type { T1 } from '#t1'
1258+
import type { T2 } from '#t2'
1259+
import type { T3 } from '#t3'
1260+
import type { T4 } from '#o/t4.ts'
1261+
defineProps<T1 & T2 & T3 & T4>()
1262+
`,
1263+
files,
1264+
{},
1265+
'C:\\Test\\Test.vue',
1266+
)
1267+
1268+
expect(props).toStrictEqual({
1269+
foo: ['String'],
1270+
bar: ['Number'],
1271+
baz: ['Number'],
1272+
qux: ['String'],
1273+
})
1274+
expect(deps && [...deps]).toStrictEqual([
1275+
'C:/Test/t1.ts',
1276+
'C:/t2.ts',
1277+
'C:/Test/t3.ts',
1278+
'C:/Test/Other/t4.ts',
1279+
])
1280+
},
1281+
)
1282+
12371283
test('ts module resolve w/ project reference folder', () => {
12381284
const files = {
12391285
'/tsconfig.json': JSON.stringify({

packages/compiler-sfc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"estree-walker": "catalog:",
5151
"magic-string": "catalog:",
5252
"postcss": "^8.5.6",
53+
"resolve.exports": "^2.0.3",
5354
"source-map-js": "catalog:"
5455
},
5556
"devDependencies": {
@@ -62,7 +63,6 @@
6263
"postcss-modules": "^6.0.1",
6364
"postcss-selector-parser": "^7.1.0",
6465
"pug": "^3.0.3",
65-
"resolve.exports": "^2.0.3",
6666
"sass": "^1.90.0"
6767
}
6868
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { parse as babelParse } from '@babel/parser'
3939
import { parse } from '../parse'
4040
import { createCache } from '../cache'
4141
import type TS from 'typescript'
42-
import { dirname, extname, join, resolve } from 'path'
42+
import { dirname, extname, isAbsolute, join } from 'path'
4343
import { minimatch as isMatch } from 'minimatch'
4444
import * as process from 'process'
4545
import { imports as resolveImports } from 'resolve.exports'
@@ -961,7 +961,7 @@ function importSourceToScope(
961961
}
962962
resolved =
963963
resolveWithTS(scope.filename, source, ts, fs) ||
964-
resolveWithNodeSubpathImports(source, fs)
964+
resolveWithNodeSubpathImports(scope.filename, source, fs)
965965
}
966966
if (resolved) {
967967
resolved = scope.resolvedImportSources[source] = normalizePath(resolved)
@@ -1127,13 +1127,14 @@ function loadTSConfig(
11271127
}
11281128

11291129
function resolveWithNodeSubpathImports(
1130+
containingFile: string,
11301131
source: string,
11311132
fs: FS,
11321133
): string | undefined {
11331134
if (!__CJS__) return
11341135

11351136
try {
1136-
const pkgPath = findPackageJsonFile(fs)
1137+
const pkgPath = findPackageJsonFile(containingFile, fs)
11371138
if (!pkgPath) {
11381139
return
11391140
}
@@ -1149,14 +1150,19 @@ function resolveWithNodeSubpathImports(
11491150
return
11501151
}
11511152

1152-
const resolved = resolve(dirname(pkgPath), resolvedImports[0])
1153+
const resolved = isAbsolute(resolvedImports[0])
1154+
? resolvedImports[0]
1155+
: joinPaths(dirname(pkgPath), resolvedImports[0])
11531156

11541157
return fs.realpath ? fs.realpath(resolved) : resolved
11551158
} catch (e) {}
11561159
}
11571160

1158-
function findPackageJsonFile(fs: FS): string | undefined {
1159-
let currDir = process.cwd()
1161+
function findPackageJsonFile(
1162+
searchStartPath: string,
1163+
fs: FS,
1164+
): string | undefined {
1165+
let currDir = searchStartPath
11601166
while (true) {
11611167
const filePath = joinPaths(currDir, 'package.json')
11621168
if (fs.fileExists(filePath)) {

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)