Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Added utility to generate "_redirects" file (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha authored Sep 4, 2022
1 parent b52b9cd commit 3b6b9a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-rats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'guild-docs': minor
---

Added utility to generate "\_redirects" file
43 changes: 43 additions & 0 deletions packages/docs/src/underscore-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { writeFile } from 'fs/promises';
import { join } from 'path';

class RunPromiseWebpackPlugin {
asyncHook;

constructor(asyncHook: () => Promise<void>) {
this.asyncHook = asyncHook;
}

apply(compiler: any) {
compiler.hooks.beforeCompile.tapPromise('RunPromiseWebpackPlugin', this.asyncHook);
}
}

export function applyUnderscoreRedirects(config: any, meta: any) {
config.plugins.push(
new RunPromiseWebpackPlugin(async () => {
const outDir = meta.dir;
const outFile = join(outDir, './public/_redirects');

try {
const redirects: any[] = meta.config.redirects
? Array.isArray(typeof meta.config.redirects)
? typeof meta.config.redirects
: await meta.config.redirects()
: [];

if (redirects.length > 0) {
const redirectsTxt = redirects
.map(r => `${r.source} ${r.destination} ${r.permanent ? '301' : '302'}`)
.join('\n');
await writeFile(outFile, redirectsTxt);
} else {
console.warn(`No redirects defined, no "_redirect" file is created!`);
}
} catch (e) {
console.error('Error while generating redirects file: ', e);
throw new Error(`Failed to generate "_redirects" file during build: ${(e as Error).message}`);
}
})
);
}

1 comment on commit 3b6b9a9

@vercel
Copy link

@vercel vercel bot commented on 3b6b9a9 Sep 4, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

the-guild-docs – ./

the-guild-docs.vercel.app
the-guild-docs-theguild.vercel.app
the-guild-docs-git-main-theguild.vercel.app

Please sign in to comment.