Skip to content

Commit

Permalink
perf(resolve): avoid isWorkerRequest and clean up .ts imported a .js (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 25, 2023
1 parent 92601db commit 8ab1438
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 72 deletions.
37 changes: 0 additions & 37 deletions packages/vite/src/node/__tests__/utils.spec.ts
Expand Up @@ -5,7 +5,6 @@ import {
asyncFlatten,
getHash,
getLocalhostAddressIfDiffersFromDNS,
getPotentialTsSrcPaths,
injectQuery,
isFileReadable,
isWindows,
Expand Down Expand Up @@ -137,42 +136,6 @@ describe('resolveHostname', () => {
})
})

test('ts import of file with .js extension', () => {
expect(getPotentialTsSrcPaths('test-file.js')).toEqual([
'test-file.ts',
'test-file.tsx',
])
})

test('ts import of file with .jsx extension', () => {
expect(getPotentialTsSrcPaths('test-file.jsx')).toEqual(['test-file.tsx'])
})

test('ts import of file .mjs,.cjs extension', () => {
expect(getPotentialTsSrcPaths('test-file.cjs')).toEqual([
'test-file.cts',
'test-file.ctsx',
])
expect(getPotentialTsSrcPaths('test-file.mjs')).toEqual([
'test-file.mts',
'test-file.mtsx',
])
})

test('ts import of file with .js before extension', () => {
expect(getPotentialTsSrcPaths('test-file.js.js')).toEqual([
'test-file.js.ts',
'test-file.js.tsx',
])
})

test('ts import of file with .js and query param', () => {
expect(getPotentialTsSrcPaths('test-file.js.js?lee=123')).toEqual([
'test-file.js.ts?lee=123',
'test-file.js.tsx?lee=123',
])
})

describe('posToNumber', () => {
test('simple', () => {
const actual = posToNumber('a\nb', { line: 2, column: 0 })
Expand Down
34 changes: 21 additions & 13 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -22,15 +22,13 @@ import {
deepImportRE,
ensureVolumeInPath,
fsPathFromId,
getPotentialTsSrcPaths,
injectQuery,
isBuiltin,
isDataUrl,
isExternalUrl,
isNonDriveRelativeAbsolutePath,
isObject,
isOptimizable,
isPossibleTsOutput,
isTsRequest,
isWindows,
lookupFile,
Expand All @@ -49,7 +47,6 @@ import {
loadPackageData,
resolvePackageData,
} from '../packages'
import { isWorkerRequest } from './worker'

const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
const normalizedEnvEntry = normalizePath(ENV_ENTRY)
Expand Down Expand Up @@ -178,16 +175,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

if (importer) {
const _importer = isWorkerRequest(importer)
? splitFileAndPostfix(importer).file
: importer
if (
isTsRequest(_importer) ||
isTsRequest(importer) ||
resolveOpts.custom?.depScan?.loader?.startsWith('ts')
) {
options.isFromTsImporter = true
} else {
const moduleLang = this.getModuleInfo(_importer)?.meta?.vite?.lang
const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang
options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`)
}
}
Expand Down Expand Up @@ -532,6 +526,9 @@ function tryFsResolve(
if (res) return res + postfix
}

const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/
const isPossibleTsOutput = (url: string): boolean => knownTsOutputRE.test(url)

function tryCleanFsResolve(
file: string,
options: InternalResolveOptions,
Expand All @@ -555,11 +552,22 @@ function tryCleanFsResolve(
const dirStat = tryStatSync(dirPath)
if (dirStat?.isDirectory()) {
if (possibleJsToTs) {
// try resolve .js, .mjs, .mts or .jsx import to typescript file
const tsSrcPaths = getPotentialTsSrcPaths(file)
for (const srcPath of tsSrcPaths) {
if ((res = tryResolveRealFile(srcPath, preserveSymlinks))) return res
}
// try resolve .js, .mjs, .cjs or .jsx import to typescript file
const fileExt = path.extname(file)
const fileName = file.slice(0, -fileExt.length)
if (
(res = tryResolveRealFile(
fileName + fileExt.replace('js', 'ts'),
preserveSymlinks,
))
)
return res
// for .js, also try .tsx
if (
fileExt === '.js' &&
(res = tryResolveRealFile(fileName + '.tsx', preserveSymlinks))
)
return res
}

if (
Expand Down
8 changes: 0 additions & 8 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -32,14 +32,6 @@ export type WorkerType = 'classic' | 'module' | 'ignore'
export const WORKER_FILE_ID = 'worker_file'
const workerCache = new WeakMap<ResolvedConfig, WorkerCache>()

export function isWorkerRequest(id: string): boolean {
const query = parseRequest(id)
if (query && query[WORKER_FILE_ID] != null) {
return true
}
return false
}

function saveEmitWorkerAsset(
config: ResolvedConfig,
asset: EmittedAsset,
Expand Down
15 changes: 1 addition & 14 deletions packages/vite/src/node/utils.ts
Expand Up @@ -283,21 +283,8 @@ export const isJSRequest = (url: string): boolean => {
return false
}

const knownTsRE = /\.(?:ts|mts|cts|tsx)$/
const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/
const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/
export const isTsRequest = (url: string): boolean => knownTsRE.test(url)
export const isPossibleTsOutput = (url: string): boolean =>
knownTsOutputRE.test(cleanUrl(url))

const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/
export function getPotentialTsSrcPaths(filePath: string): string[] {
const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE)
const paths = [name + type.replace('js', 'ts') + query]
if (type[type.length - 1] !== 'x') {
paths.push(name + type.replace('js', 'tsx') + query)
}
return paths
}

const importQueryRE = /(\?|&)import=?(?:&|$)/
const directRequestRE = /(\?|&)direct=?(?:&|$)/
Expand Down

0 comments on commit 8ab1438

Please sign in to comment.