diff --git a/.changeset/hungry-ears-clap.md b/.changeset/hungry-ears-clap.md new file mode 100644 index 000000000..e10b35f58 --- /dev/null +++ b/.changeset/hungry-ears-clap.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow importing .ts files with .js extension diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 790262249..91eadf9e3 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -216,6 +216,13 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu return { code: `${code}${SUFFIX}`, map, + meta: { + vite: { + // Setting this vite metadata to `ts` causes Vite to resolve .js + // extensions to .ts files. + lang: 'ts' + } + } }; } catch (err: any) { // Verify frontmatter: a common reason that this plugin fails is that diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 6a8d59767..6f0410bbb 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -194,6 +194,11 @@ ${tsResult}`; return { code: escapeViteEnvReferences(code), map: null, + meta: { + vite: { + lang: 'ts' + } + } }; } diff --git a/packages/astro/test/fixtures/import-ts-with-js/src/bar.ts b/packages/astro/test/fixtures/import-ts-with-js/src/bar.ts new file mode 100644 index 000000000..98b90b031 --- /dev/null +++ b/packages/astro/test/fixtures/import-ts-with-js/src/bar.ts @@ -0,0 +1,3 @@ +export default function() { + return 'bar'; +} diff --git a/packages/astro/test/fixtures/import-ts-with-js/src/foo.ts b/packages/astro/test/fixtures/import-ts-with-js/src/foo.ts new file mode 100644 index 000000000..95492488d --- /dev/null +++ b/packages/astro/test/fixtures/import-ts-with-js/src/foo.ts @@ -0,0 +1,3 @@ +import bar from './bar.js'; + +export default bar; diff --git a/packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro b/packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro new file mode 100644 index 000000000..36186b21b --- /dev/null +++ b/packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro @@ -0,0 +1,9 @@ +--- +import foo from '../foo.js'; +--- + +