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(scan): detect import .ts as .js #8969

Merged
merged 1 commit into from Jul 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }
Copy link
Member

Choose a reason for hiding this comment

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

We already have a scan flag that is available also to the resolver. But I agree that using the convention for custom here feels better. So looks good to me 👍🏼

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