Skip to content

Commit 9974094

Browse files
authored
feat: import ts with .js in vue (#7998)
1 parent 0861ade commit 9974094

File tree

11 files changed

+42
-3
lines changed

11 files changed

+42
-3
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<h2>Ts Import</h2>
3+
<p class="ts-import">{{ foo }}</p>
4+
</template>
5+
6+
<script setup lang="tsx">
7+
import { foo } from './TsImportFile.js'
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'success'

packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test('should render', async () => {
99
expect(await page.textContent('.src-import')).toMatch('5')
1010
expect(await page.textContent('.jsx-with-query')).toMatch('6')
1111
expect(await page.textContent('.other-ext')).toMatch('Other Ext')
12+
expect(await page.textContent('.ts-import')).toMatch('success')
1213
})
1314

1415
test('should update', async () => {

packages/playground/vue-jsx/main.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import JsxSrcImport from './SrcImport.vue'
77
import JsxSetupSyntax from './setup-syntax-jsx.vue'
88
// eslint-disable-next-line
99
import JsxWithQuery from './Query.jsx?query=true'
10+
import TsImport from './TsImport.vue'
1011

1112
function App() {
1213
return (
@@ -20,6 +21,7 @@ function App() {
2021
<JsxSrcImport />
2122
<JsxSetupSyntax />
2223
<JsxWithQuery />
24+
<TsImport />
2325
</>
2426
)
2527
}

packages/playground/vue/Main.vue

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<div class="slotted">this should be red</div>
1616
</Slotted>
1717
<ScanDep />
18+
<TsImport />
1819
<Suspense>
1920
<AsyncComponent />
2021
</Suspense>
@@ -33,6 +34,7 @@ import CustomBlock from './CustomBlock.vue'
3334
import SrcImport from './src-import/SrcImport.vue'
3435
import Slotted from './Slotted.vue'
3536
import ScanDep from './ScanDep.vue'
37+
import TsImport from './TsImport.vue'
3638
import AsyncComponent from './AsyncComponent.vue'
3739
import ReactivityTransform from './ReactivityTransform.vue'
3840
import SetupImportTemplate from './setup-import-template/SetupImportTemplate.vue'

packages/playground/vue/TsImport.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<h2>Ts Import</h2>
3+
<p class="ts-import">{{ foo }}</p>
4+
</template>
5+
6+
<script setup lang="ts">
7+
import { foo } from './TsImportFile.js'
8+
</script>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'success'

packages/playground/vue/__tests__/vue.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ test('template/script latest syntax support', async () => {
1414
expect(await page.textContent('.syntax')).toBe('baz')
1515
})
1616

17+
test('import ts with .js extension with lang="ts"', async () => {
18+
expect(await page.textContent('.ts-import')).toBe('success')
19+
})
20+
1721
test('should remove comments in prod', async () => {
1822
expect(await page.innerHTML('.comments')).toBe(isBuild ? `` : `<!--hello-->`)
1923
})

packages/plugin-vue/src/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ export async function transformMain(
211211
code: resolvedCode,
212212
map: resolvedMap || {
213213
mappings: ''
214+
},
215+
meta: {
216+
vite: {
217+
lang: descriptor.script?.lang || descriptor.scriptSetup?.lang || 'js'
218+
}
214219
}
215220
}
216221
}

packages/vite/src/node/plugins/resolve.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,19 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
128128

129129
const options: InternalResolveOptions = {
130130
isRequire,
131-
132131
...baseOptions,
133-
isFromTsImporter: isTsRequest(importer ?? ''),
134132
scan: resolveOpts?.scan ?? baseOptions.scan
135133
}
136134

135+
if (importer) {
136+
if (isTsRequest(importer)) {
137+
options.isFromTsImporter = true
138+
} else {
139+
const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang
140+
options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`)
141+
}
142+
}
143+
137144
let res: string | PartialResolvedId | undefined
138145

139146
// resolve pre-bundled deps requests, these could be resolved by

packages/vite/src/node/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export const isJSRequest = (url: string): boolean => {
228228

229229
const knownTsRE = /\.(ts|mts|cts|tsx)$/
230230
const knownTsOutputRE = /\.(js|mjs|cjs|jsx)$/
231-
export const isTsRequest = (url: string) => knownTsRE.test(cleanUrl(url))
231+
export const isTsRequest = (url: string) => knownTsRE.test(url)
232232
export const isPossibleTsOutput = (url: string) =>
233233
knownTsOutputRE.test(cleanUrl(url))
234234
export function getPotentialTsSrcPaths(filePath: string) {

0 commit comments

Comments
 (0)