diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index edb3fd3195f455..2a7f7306b5e8f9 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -189,21 +189,13 @@ const knownTsOutputRE = /\.(js|mjs|cjs|jsx)$/ export const isTsRequest = (url: string) => knownTsRE.test(cleanUrl(url)) export const isPossibleTsOutput = (url: string) => knownTsOutputRE.test(cleanUrl(url)) -export const getPotentialTsSrcPaths = (filename: string) => { - const fileParts = filename.split(/(\.[cm]?js\?|\.[cm]?js$)/) - - // If there are three parts, the file uses a .js, .mjs or .cjs extension. - // We replace the 'js' part with 'ts' and 'tsx'. - if (fileParts.length === 3) { - const [namePart, extension, queryPart] = fileParts - - return [ - `${namePart}${extension.replace('js', 'ts')}${queryPart}`, - `${namePart}${extension.replace('js', 'tsx')}${queryPart}` - ] +export function getPotentialTsSrcPaths(filePath: string) { + const [name, type, query = ''] = filePath.split(/(\.(?:[cm]?js|jsx))(\?.*)?$/) + const paths = [name + '.tsx' + query] + if (!type.endsWith('x')) { + paths.push(name + '.ts' + query) } - - return [filename.replace(/\.([cm])?(js)(x?)(\?|$)/, '.$1ts$3')] + return paths } const importQueryRE = /(\?|&)import=?(?:&|$)/