Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tsconfig alias regression #6617

Merged
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ export default function configAliasVitePlugin({
};
}
},
resolveId(id) {
async resolveId(id, importer, options) {
if (id.startsWith('.') || id.startsWith('/')) return;

// Handle baseUrl mapping for non-relative and non-root imports.
// Since TypeScript only applies `baseUrl` autocompletions for files that exist
// in the filesystem only, we can use this heuristic to skip resolve if needed.
const resolved = path.posix.join(resolvedBaseUrl, id);
if (fs.existsSync(resolved)) {
Copy link
Member Author

@MoustaphaDev MoustaphaDev Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When importing TypeScript files without the .ts extension, this condition was always false, as /path/to/ts-file doesn't exist.

Thought about adding a check for .ts files starting with the resolved id to make the change minimal but realized that there might be other file types that could be resolved when not specifying the extension (maybe via vite plugins pushed before the ts/js resolver? Not sure)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call!

return resolved;
}

const resolvedAliasedId = await this.resolve(resolved, importer, {
skipSelf: true,
...options,
});
if (resolvedAliasedId) return resolvedAliasedId;
},
};
}
8 changes: 7 additions & 1 deletion packages/astro/test/alias-tsconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ describe('Aliases with tsconfig.json', () => {

it('works in css @import', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
console.log(html);
// imported css should be bundled
expect(html).to.include('#style-red');
expect(html).to.include('#style-blue');
});

it('can load load typescript files without .ts extension', async () => {
MoustaphaDev marked this conversation as resolved.
Show resolved Hide resolved
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

expect($('#mistery').text()).to.equal("I'm a TypeScript file!");
});
});
});
32 changes: 18 additions & 14 deletions packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
---
import Client from '@components/Client.svelte'
import Client from '@components/Client.svelte';
import Foo from 'src/components/Foo.astro';
import StyleComp from 'src/components/Style.astro';
import '@styles/main.css'
import '@styles/main.css';
import { whoImI } from 'src/ts-file';
const mistery = whoImI();
---

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Aliases using tsconfig</title>
</head>
<body>
<main>
<Client client:load />
<Foo />
<StyleComp />
</main>
</body>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Aliases using tsconfig</title>
</head>
<body>
<main>
<Client client:load />
<Foo />
<StyleComp />
<div id="mistery">{mistery}</div>
</main>
</body>
</html>
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/alias-tsconfig/src/ts-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function whoImI() {
return "I'm a TypeScript file!";
}