From 8ab1438e7b0e5be29d256e2fc1466a23725af2c6 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 11:09:42 +0100 Subject: [PATCH] perf(resolve): avoid isWorkerRequest and clean up .ts imported a .js (#12571) --- .../vite/src/node/__tests__/utils.spec.ts | 37 ------------------- packages/vite/src/node/plugins/resolve.ts | 34 ++++++++++------- packages/vite/src/node/plugins/worker.ts | 8 ---- packages/vite/src/node/utils.ts | 15 +------- 4 files changed, 22 insertions(+), 72 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 65a88ed708230b..2ee69665c49ae7 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -5,7 +5,6 @@ import { asyncFlatten, getHash, getLocalhostAddressIfDiffersFromDNS, - getPotentialTsSrcPaths, injectQuery, isFileReadable, isWindows, @@ -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 }) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 3616a839c1098b..4d535c8659f834 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -22,7 +22,6 @@ import { deepImportRE, ensureVolumeInPath, fsPathFromId, - getPotentialTsSrcPaths, injectQuery, isBuiltin, isDataUrl, @@ -30,7 +29,6 @@ import { isNonDriveRelativeAbsolutePath, isObject, isOptimizable, - isPossibleTsOutput, isTsRequest, isWindows, lookupFile, @@ -49,7 +47,6 @@ import { loadPackageData, resolvePackageData, } from '../packages' -import { isWorkerRequest } from './worker' const normalizedClientEntry = normalizePath(CLIENT_ENTRY) const normalizedEnvEntry = normalizePath(ENV_ENTRY) @@ -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}`) } } @@ -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, @@ -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 ( diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 197b3feeabe0bc..7fd7e328d6c301 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -32,14 +32,6 @@ export type WorkerType = 'classic' | 'module' | 'ignore' export const WORKER_FILE_ID = 'worker_file' const workerCache = new WeakMap() -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, diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 078aa8ed99e4f3..5e9a73f111f3b8 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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=?(?:&|$)/