Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): try .tsx extension for .js import from typescript module #7005

Merged
merged 7 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ <h2>
</h2>
<p class="ts-extension">fail</p>

<h2>
A ts module can import another tsx module using its corresponding jsx file
name
</h2>
<p class="jsx-extension">fail</p>

<h2>
A ts module can import another tsx module using its corresponding js file name
</h2>
<p class="tsx-extension">fail</p>

<h2>Resolve file name containing dot</h2>
<p class="dot">fail</p>

Expand Down Expand Up @@ -130,6 +141,12 @@ <h2>resolve package that contains # in path</h2>
import { msg as tsExtensionMsg } from './ts-extension'
text('.ts-extension', tsExtensionMsg)

import { msgJsx as tsJsxExtensionMsg } from './ts-extension'
text('.jsx-extension', tsJsxExtensionMsg)

import { msgTsx as tsTsxExtensionMsg } from './ts-extension'
text('.tsx-extension', tsTsxExtensionMsg)

// filename with dot
import { bar } from './util/bar.util'
text('.dot', bar())
Expand Down
1 change: 1 addition & 0 deletions packages/playground/resolve/ts-extension/hellojsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msgJsx = '[success] use .jsx extension to import a tsx module'
1 change: 1 addition & 0 deletions packages/playground/resolve/ts-extension/hellotsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msgTsx = '[success] use .js extension to import a tsx module'
4 changes: 3 additions & 1 deletion packages/playground/resolve/ts-extension/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { msg } from './hello.js'
import { msgJsx } from './hellojsx.jsx'
import { msgTsx } from './hellotsx.js'

export { msg }
export { msg, msgJsx, msgTsx }
46 changes: 45 additions & 1 deletion packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectQuery, isWindows } from '../utils'
import { getPotentialTsSrcPaths, injectQuery, isWindows } from '../utils'

if (isWindows) {
// this test will work incorrectly on unix systems
Expand Down Expand Up @@ -38,3 +38,47 @@ test('path with unicode, space, and %', () => {
'/usr/vite/東京 %20 hello?direct'
)
})

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'
])
})

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

test('ts import without any extension', () => {
expect(getPotentialTsSrcPaths('test-file')).toEqual(['test-file'])
})
26 changes: 15 additions & 11 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
isFileReadable,
isTsRequest,
isPossibleTsOutput,
getTsSrcPath
getPotentialTsSrcPaths
} from '../utils'
import type { ViteDevServer, SSROptions } from '..'
import type { PartialResolvedId } from 'rollup'
Expand Down Expand Up @@ -436,16 +436,20 @@ function tryResolveFile(

const tryTsExtension = options.isFromTsImporter && isPossibleTsOutput(file)
if (tryTsExtension) {
const tsSrcPath = getTsSrcPath(file)
return tryResolveFile(
tsSrcPath,
postfix,
options,
tryIndex,
targetWeb,
tryPrefix,
skipPackageJson
)
const tsSrcPaths = getPotentialTsSrcPaths(file)
for (const srcPath of tsSrcPaths) {
const res = tryResolveFile(
srcPath,
postfix,
options,
tryIndex,
targetWeb,
tryPrefix,
skipPackageJson
)
if (res) return res
}
return
}

if (tryPrefix) {
Expand Down
18 changes: 16 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,22 @@ 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 getTsSrcPath = (filename: string) =>
filename.replace(/\.([cm])?(js)(x?)(\?|$)/, '.$1ts$3')
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}`
]
}

return [filename.replace(/\.([cm])?(js)(x?)(\?|$)/, '.$1ts$3')]
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice, thanks for improving the implementation I came up with.


const importQueryRE = /(\?|&)import=?(?:&|$)/
const internalPrefixes = [
Expand Down