Skip to content

Commit

Permalink
fix(scan): detect import .ts as .js (#8969)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 7, 2022
1 parent 57c6c15 commit 752af6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
27 changes: 22 additions & 5 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -25,6 +25,8 @@ import type { PluginContainer } from '../server/pluginContainer'
import { createPluginContainer } from '../server/pluginContainer'
import { transformGlobImport } from '../plugins/importMetaGlob'

type ResolveIdOptions = Parameters<PluginContainer['resolveId']>[2]

const debug = createDebugger('vite:deps')

const htmlTypesRE = /\.(html|vue|svelte|astro)$/
Expand Down Expand Up @@ -163,7 +165,11 @@ function esbuildScanPlugin(
): Plugin {
const seen = new Map<string, string | undefined>()

const resolve = async (id: string, importer?: string) => {
const resolve = async (
id: string,
importer?: string,
options?: ResolveIdOptions
) => {
const key = id + (importer && path.dirname(importer))
if (seen.has(key)) {
return seen.get(key)
Expand All @@ -172,6 +178,7 @@ function esbuildScanPlugin(
id,
importer && normalizePath(importer),
{
...options,
scan: true
}
)
Expand Down Expand Up @@ -316,12 +323,18 @@ function esbuildScanPlugin(
config.root,
resolve
)
)?.s.toString() || transpiledContents
)?.s.toString() || transpiledContents,
pluginData: {
htmlType: { loader }
}
}
} else {
scripts[key] = {
loader,
contents
contents,
pluginData: {
htmlType: { loader }
}
}
}

Expand Down Expand Up @@ -434,9 +447,13 @@ function esbuildScanPlugin(
{
filter: /.*/
},
async ({ path: id, importer }) => {
async ({ path: id, importer, pluginData }) => {
// use vite resolver to support urls and omitted extensions
const resolved = await resolve(id, importer)
const resolved = await resolve(id, importer, {
custom: {
depScan: { loader: pluginData?.htmlType?.loader }
}
})
if (resolved) {
if (shouldExternalizeDep(resolved, id) || !isScannable(resolved)) {
return externalUnlessEntry({ path: id })
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -132,7 +132,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

if (importer) {
if (isTsRequest(importer)) {
if (
isTsRequest(importer) ||
resolveOpts.custom?.depScan?.loader?.startsWith('ts')
) {
options.isFromTsImporter = true
} else {
const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang
Expand Down
2 changes: 1 addition & 1 deletion playground/vue/TsImport.vue
Expand Up @@ -4,5 +4,5 @@
</template>

<script setup lang="ts">
import { foo } from './TsImportFile.js'
import { foo } from '/@/TsImportFile.js'
</script>

0 comments on commit 752af6c

Please sign in to comment.