Skip to content

Commit 0a31d99

Browse files
fix: actions typegen path (#13976)
1 parent b1ec32a commit 0a31d99

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

.changeset/stale-donkeys-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a case where Astro Actions types would be broken when using a `tsconfig.json` with `"moduleResolution": "nodenext"`

packages/astro/src/actions/integration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import { ACTIONS_TYPES_FILE, ACTION_RPC_ROUTE_PATTERN, VIRTUAL_MODULE_ID } from
1111
*/
1212
export default function astroIntegrationActionsRouteHandler({
1313
settings,
14+
filename,
1415
}: {
1516
settings: AstroSettings;
17+
filename: string;
1618
}): AstroIntegration {
1719
return {
1820
name: VIRTUAL_MODULE_ID,
@@ -33,7 +35,7 @@ export default function astroIntegrationActionsRouteHandler({
3335
}
3436

3537
const stringifiedActionsImport = JSON.stringify(
36-
viteID(new URL('./actions', params.config.srcDir)),
38+
viteID(new URL(`./${filename}`, params.config.srcDir)),
3739
);
3840
settings.injectedTypes.push({
3941
filename: ACTIONS_TYPES_FILE,

packages/astro/src/actions/utils.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function isActionsFilePresent(fs: typeof fsMod, srcDir: URL) {
4141

4242
let contents: string;
4343
try {
44-
contents = fs.readFileSync(actionsFile, 'utf-8');
44+
contents = fs.readFileSync(actionsFile.url, 'utf-8');
4545
} catch {
4646
return false;
4747
}
@@ -50,17 +50,17 @@ export async function isActionsFilePresent(fs: typeof fsMod, srcDir: URL) {
5050
// If not, the user may have an empty `actions` file,
5151
// or may be using the `actions` file for another purpose
5252
// (possible since actions are non-breaking for v4.X).
53-
const [, exports] = eslexer.parse(contents, actionsFile.pathname);
53+
const [, exports] = eslexer.parse(contents, actionsFile.url.pathname);
5454
for (const exp of exports) {
5555
if (exp.n === 'server') {
56-
return true;
56+
return actionsFile.filename;
5757
}
5858
}
5959
return false;
6060
}
6161

6262
function search(fs: typeof fsMod, srcDir: URL) {
63-
const paths = [
63+
const filenames = [
6464
'actions.mjs',
6565
'actions.js',
6666
'actions.mts',
@@ -69,10 +69,11 @@ function search(fs: typeof fsMod, srcDir: URL) {
6969
'actions/index.js',
7070
'actions/index.mts',
7171
'actions/index.ts',
72-
].map((p) => new URL(p, srcDir));
73-
for (const file of paths) {
74-
if (fs.existsSync(file)) {
75-
return file;
72+
];
73+
for (const filename of filenames) {
74+
const url = new URL(filename, srcDir);
75+
if (fs.existsSync(url)) {
76+
return { filename, url };
7677
}
7778
}
7879
return undefined;

packages/astro/src/integrations/hooks.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,11 @@ export async function runHookConfigSetup({
182182
if (settings.config.adapter) {
183183
settings.config.integrations.unshift(settings.config.adapter);
184184
}
185-
if (await isActionsFilePresent(fs, settings.config.srcDir)) {
186-
settings.config.integrations.push(astroIntegrationActionsRouteHandler({ settings }));
185+
const actionsFilename = await isActionsFilePresent(fs, settings.config.srcDir)
186+
if (actionsFilename) {
187+
settings.config.integrations.push(
188+
astroIntegrationActionsRouteHandler({ settings, filename: actionsFilename }),
189+
);
187190
}
188191

189192
let updatedConfig: AstroConfig = { ...settings.config };

0 commit comments

Comments
 (0)