Allow importing .ts files with .js extension (#3518)
* Allow importing .ts files with .js extension * Adds a changeset * Make it also work in .md files
This commit is contained in:
parent
d9a67d36dc
commit
df7c43df63
8 changed files with 65 additions and 0 deletions
5
.changeset/hungry-ears-clap.md
Normal file
5
.changeset/hungry-ears-clap.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allow importing .ts files with .js extension
|
|
@ -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
|
||||
|
|
|
@ -194,6 +194,11 @@ ${tsResult}`;
|
|||
return {
|
||||
code: escapeViteEnvReferences(code),
|
||||
map: null,
|
||||
meta: {
|
||||
vite: {
|
||||
lang: 'ts'
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
3
packages/astro/test/fixtures/import-ts-with-js/src/bar.ts
vendored
Normal file
3
packages/astro/test/fixtures/import-ts-with-js/src/bar.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default function() {
|
||||
return 'bar';
|
||||
}
|
3
packages/astro/test/fixtures/import-ts-with-js/src/foo.ts
vendored
Normal file
3
packages/astro/test/fixtures/import-ts-with-js/src/foo.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import bar from './bar.js';
|
||||
|
||||
export default bar;
|
9
packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro
vendored
Normal file
9
packages/astro/test/fixtures/import-ts-with-js/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
import foo from '../foo.js';
|
||||
---
|
||||
<html>
|
||||
<head><title></title></head>
|
||||
<body>
|
||||
<h1>{ foo() }</h1>
|
||||
</body>
|
||||
</html>
|
8
packages/astro/test/fixtures/import-ts-with-js/src/pages/post.md
vendored
Normal file
8
packages/astro/test/fixtures/import-ts-with-js/src/pages/post.md
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
setup: |
|
||||
import foo from '../foo.js'
|
||||
---
|
||||
|
||||
# Testing
|
||||
|
||||
## { foo() }
|
25
packages/astro/test/import-ts-with-js.test.js
Normal file
25
packages/astro/test/import-ts-with-js.test.js
Normal file
|
@ -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');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue