From 5f7c312fc3db1cf5e8ab55de7d8f7928f2bd24aa Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 24 Mar 2023 23:31:14 +0100 Subject: [PATCH 1/3] perf(resolve): avoid isWorkerRequest --- packages/vite/src/node/plugins/resolve.ts | 8 ++------ packages/vite/src/node/plugins/worker.ts | 8 -------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 581b1a3368c4c8..f1655e0084a8eb 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -48,7 +48,6 @@ import { loadPackageData, resolvePackageData, } from '../packages' -import { isWorkerRequest } from './worker' const normalizedClientEntry = normalizePath(CLIENT_ENTRY) const normalizedEnvEntry = normalizePath(ENV_ENTRY) @@ -177,16 +176,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { } if (importer) { - const _importer = isWorkerRequest(importer) - ? splitFileAndPostfix(importer).file - : importer if ( - isTsRequest(_importer) || + isTsRequest(cleanUrl(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}`) } } 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, From 9060f7fc2d76f6bf6732c0a7125f2ffa58f71f30 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 09:26:33 +0100 Subject: [PATCH 2/3] chore: refactor --- .../vite/src/node/__tests__/utils.spec.ts | 37 ------------------- packages/vite/src/node/plugins/resolve.ts | 26 +++++++++---- packages/vite/src/node/utils.ts | 15 +------- 3 files changed, 20 insertions(+), 58 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 f1655e0084a8eb..69966da6c9894b 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, @@ -177,7 +175,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { if (importer) { if ( - isTsRequest(cleanUrl(importer)) || + isTsRequest(importer) || resolveOpts.custom?.depScan?.loader?.startsWith('ts') ) { options.isFromTsImporter = true @@ -527,6 +525,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, @@ -551,10 +552,21 @@ function tryCleanFsResolve( 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 - } + const type = path.extname(file) + const fileName = file.slice(0, -type.length) + if ( + (res = tryResolveRealFile( + fileName + type.replace('js', 'ts'), + preserveSymlinks, + )) + ) + return res + // for .js, also try .tsx + if ( + type === '.js' && + (res = tryResolveRealFile(fileName + '.tsx', preserveSymlinks)) + ) + return res } if ( diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 4eb5bef7ae87f3..a65e96dccdd835 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -282,21 +282,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=?(?:&|$)/ From cbdf6bcab3ffb7deb84083bd64ee3bd630de018a Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 25 Mar 2023 10:22:55 +0100 Subject: [PATCH 3/3] chore: update --- packages/vite/src/node/plugins/resolve.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 69966da6c9894b..375040b37eb80e 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -551,19 +551,19 @@ function tryCleanFsResolve( const dirStat = tryStatSync(dirPath) if (dirStat?.isDirectory()) { if (possibleJsToTs) { - // try resolve .js, .mjs, .mts or .jsx import to typescript file - const type = path.extname(file) - const fileName = file.slice(0, -type.length) + // 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 + type.replace('js', 'ts'), + fileName + fileExt.replace('js', 'ts'), preserveSymlinks, )) ) return res // for .js, also try .tsx if ( - type === '.js' && + fileExt === '.js' && (res = tryResolveRealFile(fileName + '.tsx', preserveSymlinks)) ) return res