From c6f337745b5b9a640d5d391bbed1e456bd53eb23 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 Jun 2022 11:19:23 -0400 Subject: [PATCH] Allow importing .ts files with .js extension --- packages/astro/src/vite-plugin-astro/index.ts | 7 +++++++ .../fixtures/import-ts-with-js/src/bar.ts | 3 +++ .../fixtures/import-ts-with-js/src/foo.ts | 3 +++ .../import-ts-with-js/src/pages/index.astro | 9 +++++++++ packages/astro/test/import-ts-with-js.test.js | 19 +++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 packages/astro/test/fixtures/import-ts-with-js/src/bar.ts create mode 100644 packages/astro/test/fixtures/import-ts-with-js/src/foo.ts create mode 100644 packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro create mode 100644 packages/astro/test/import-ts-with-js.test.js diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 7902622490..91eadf9e3a 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/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 0000000000..98b90b0310 --- /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 0000000000..95492488da --- /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 0000000000..36186b21b2 --- /dev/null +++ b/packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro @@ -0,0 +1,9 @@ +--- +import foo from '../foo.js'; +--- + + + +

{ foo() }

+ + diff --git a/packages/astro/test/import-ts-with-js.test.js b/packages/astro/test/import-ts-with-js.test.js new file mode 100644 index 0000000000..84ab1ca868 --- /dev/null +++ b/packages/astro/test/import-ts-with-js.test.js @@ -0,0 +1,19 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('Using .js extension on .ts file', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ root: './fixtures/import-ts-with-js/' }); + await fixture.build(); + }); + + it('works', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + expect($('h1').text()).to.equal('bar'); + }); +});