This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utility to generate "_redirects" file (#677)
- Loading branch information
1 parent
b52b9cd
commit 3b6b9a9
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'guild-docs': minor | ||
--- | ||
|
||
Added utility to generate "\_redirects" file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
}) | ||
); | ||
} |
3b6b9a9
There was a problem hiding this comment.
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