0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00

Allow importing .ts files with .js extension

This commit is contained in:
unknown 2022-06-03 11:19:23 -04:00
parent d9a67d36dc
commit c6f337745b
5 changed files with 41 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,3 @@
export default function() {
return 'bar';
}

View file

@ -0,0 +1,3 @@
import bar from './bar.js';
export default bar;

View file

@ -0,0 +1,9 @@
---
import foo from '../foo.js';
---
<html>
<head><title></title></head>
<body>
<h1>{ foo() }</h1>
</body>
</html>

View file

@ -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');
});
});