Skip to content

Commit

Permalink
Allow importing .ts files with .js extension (#3518)
Browse files Browse the repository at this point in the history
* Allow importing .ts files with .js extension

* Adds a changeset

* Make it also work in .md files
  • Loading branch information
matthewp authored Jun 3, 2022
1 parent d9a67d3 commit df7c43d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-ears-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow importing .ts files with .js extension
7 changes: 7 additions & 0 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ ${tsResult}`;
return {
code: escapeViteEnvReferences(code),
map: null,
meta: {
vite: {
lang: 'ts'
}
}
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/import-ts-with-js/src/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function() {
return 'bar';
}
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/import-ts-with-js/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import bar from './bar.js';

export default bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import foo from '../foo.js';
---
<html>
<head><title></title></head>
<body>
<h1>{ foo() }</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
setup: |
import foo from '../foo.js'
---

# Testing

## { foo() }
25 changes: 25 additions & 0 deletions packages/astro/test/import-ts-with-js.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 in .astro files', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('h1').text()).to.equal('bar');
});

it('works in .md files', async () => {
const html = await fixture.readFile('/post/index.html');
const $ = cheerio.load(html);
expect($('h2').text()).to.equal('bar');
});
});

0 comments on commit df7c43d

Please sign in to comment.