Skip to content

Commit

Permalink
code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jun 23, 2023
1 parent 392d1ec commit fabe9ab
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .changeset/chilly-pants-fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function integration(): AstroIntegration {
return {
name: "fancy-astro-integration",
hooks: {
'astro:build:done': ({ middlewarePath }) => {
if (middlewarePath) {
'astro:build:done': ({ middlewareEntryPoint }) => {
if (middlewareEntryPoint) {
// do some operations
}
}
Expand All @@ -21,4 +21,4 @@ function integration(): AstroIntegration {
}
```

The `middlewarePath` is only defined if the user has created an Astro middleware.
The `middlewareEntryPoint` is only defined if the user has created an Astro middleware.
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@ export interface AstroIntegration {
pages: { pathname: string }[];
dir: URL;
routes: RouteData[];
middlewarePath: URL | undefined;
middlewareEntryPoint: URL | undefined;
}) => void | Promise<void>;
};
}
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ class AstroBuilder {
});
debug('build', timerMessage('Additional assets copied', this.timer.assetsStart));

let newMiddlewareEntryPoint = undefined;
let middlewareEntryPoint = undefined;
// during the last phase of the build, the emitted code gets copied inside
// `dist/server/` folder, so we need to shred the old URL and make a new one again
if (internals.middlewareEntryPoint && isServerLikeOutput(this.settings.config)) {
const outDir = fileURLToPath(this.settings.config.outDir);
const middlewareRelativePath = fileURLToPath(internals.middlewareEntryPoint).slice(
fileURLToPath(this.settings.config.outDir).length
);
newMiddlewareEntryPoint = pathToFileURL(join(outDir, 'server', middlewareRelativePath));
middlewareEntryPoint = pathToFileURL(join(outDir, 'server', middlewareRelativePath));
}

// You're done! Time to clean up.
Expand All @@ -205,7 +205,7 @@ class AstroBuilder {
pages: pageNames,
routes: Object.values(allPages).map((pd) => pd.route),
logging: this.logging,
middlewarePath: newMiddlewareEntryPoint,
middlewareEntryPoint,
});

if (this.logging.level && levels[this.logging.level] <= levels['info']) {
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ type RunHookBuildDone = {
pages: string[];
routes: RouteData[];
logging: LogOptions;
middlewarePath: URL | undefined;
middlewareEntryPoint: URL | undefined;
};

export async function runHookBuildDone({
config,
pages,
routes,
logging,
middlewarePath,
middlewareEntryPoint,
}: RunHookBuildDone) {
const dir = isServerLikeOutput(config) ? config.build.client : config.outDir;
await fs.promises.mkdir(dir, { recursive: true });
Expand All @@ -369,7 +369,7 @@ export async function runHookBuildDone({
pages: pages.map((p) => ({ pathname: p })),
dir,
routes,
middlewarePath,
middlewareEntryPoint,
}),
logging,
});
Expand Down
1 change: 0 additions & 1 deletion packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ describe('Middleware API in PROD mode, SSR', () => {
expect(middlewarePath).to.not.be.undefined;
try {
const path = fileURLToPath(middlewarePath);
console.log(path);
expect(existsSync(path)).to.be.true;
const content = readFileSync(fileURLToPath(middlewarePath), 'utf-8');
expect(content.length).to.be.greaterThan(0);
Expand Down

0 comments on commit fabe9ab

Please sign in to comment.