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

feat: import ts with .js in vue #7998

Merged
merged 6 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions packages/playground/vue-jsx/TsImport.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<h2>Ts Import</h2>
<p class="ts-import">{{ foo }}</p>
</template>

<script setup lang="tsx">
import { foo } from './TsImportFile.js'
</script>
1 change: 1 addition & 0 deletions packages/playground/vue-jsx/TsImportFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'success'
1 change: 1 addition & 0 deletions packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('should render', async () => {
expect(await page.textContent('.src-import')).toMatch('5')
expect(await page.textContent('.jsx-with-query')).toMatch('6')
expect(await page.textContent('.other-ext')).toMatch('Other Ext')
expect(await page.textContent('.ts-import')).toMatch('success')
})

test('should update', async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/vue-jsx/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import JsxSrcImport from './SrcImport.vue'
import JsxSetupSyntax from './setup-syntax-jsx.vue'
// eslint-disable-next-line
import JsxWithQuery from './Query.jsx?query=true'
import TsImport from './TsImport.vue'

function App() {
return (
Expand All @@ -20,6 +21,7 @@ function App() {
<JsxSrcImport />
<JsxSetupSyntax />
<JsxWithQuery />
<TsImport />
</>
)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/vue/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<div class="slotted">this should be red</div>
</Slotted>
<ScanDep />
<TsImport />
<Suspense>
<AsyncComponent />
</Suspense>
Expand All @@ -33,6 +34,7 @@ import CustomBlock from './CustomBlock.vue'
import SrcImport from './src-import/SrcImport.vue'
import Slotted from './Slotted.vue'
import ScanDep from './ScanDep.vue'
import TsImport from './TsImport.vue'
import AsyncComponent from './AsyncComponent.vue'
import ReactivityTransform from './ReactivityTransform.vue'
import SetupImportTemplate from './setup-import-template/SetupImportTemplate.vue'
Expand Down
8 changes: 8 additions & 0 deletions packages/playground/vue/TsImport.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<h2>Ts Import</h2>
<p class="ts-import">{{ foo }}</p>
</template>

<script setup lang="ts">
import { foo } from './TsImportFile.js'
</script>
1 change: 1 addition & 0 deletions packages/playground/vue/TsImportFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'success'
4 changes: 4 additions & 0 deletions packages/playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test('template/script latest syntax support', async () => {
expect(await page.textContent('.syntax')).toBe('baz')
})

test('import ts with .js extension with lang="ts"', async () => {
expect(await page.textContent('.ts-import')).toBe('success')
})

test('should remove comments in prod', async () => {
expect(await page.innerHTML('.comments')).toBe(isBuild ? `` : `<!--hello-->`)
})
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ export async function transformMain(
code: resolvedCode,
map: resolvedMap || {
mappings: ''
},
meta: {
vite: {
lang: descriptor.script?.lang || descriptor.scriptSetup?.lang || 'js'
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {

const options: InternalResolveOptions = {
isRequire,

...baseOptions,
isFromTsImporter: isTsRequest(importer ?? ''),
scan: resolveOpts?.scan ?? baseOptions.scan
}

if (importer) {
if (isTsRequest(importer)) {
options.isFromTsImporter = true
} else {
const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang
options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`)
}
}

let res: string | PartialResolvedId | undefined

// resolve pre-bundled deps requests, these could be resolved by
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export const isJSRequest = (url: string): boolean => {

const knownTsRE = /\.(ts|mts|cts|tsx)$/
const knownTsOutputRE = /\.(js|mjs|cjs|jsx)$/
export const isTsRequest = (url: string) => knownTsRE.test(cleanUrl(url))
export const isTsRequest = (url: string) => knownTsRE.test(url)
export const isPossibleTsOutput = (url: string) =>
knownTsOutputRE.test(cleanUrl(url))
export function getPotentialTsSrcPaths(filePath: string) {
Expand Down