Compare commits
10 commits
64354e10e7
...
381b222210
| Author | SHA1 | Date | |
|---|---|---|---|
| 381b222210 | |||
| 07495463e6 | |||
| a914343ca1 | |||
| dc2fb254d6 | |||
| 6757c0b0e6 | |||
| 4b56fd20ae | |||
| 5ac64c5db6 | |||
| 994969ba54 | |||
| f87f962c41 | |||
| 8c989402c2 |
6 changed files with 1155 additions and 1618 deletions
13
CHANGELOG.md
Normal file
13
CHANGELOG.md
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# Changelog
|
||||
|
||||
## 0.2.0
|
||||
|
||||
Append new rules to existing `.htaccess` file instead of overwriting it.
|
||||
|
||||
## 0.1.2
|
||||
|
||||
Fix redirects by checking the `redirect` field.
|
||||
|
||||
## 0.1.1
|
||||
|
||||
Initial release.
|
||||
2722
package-lock.json
generated
2722
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "astro-htaccess",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.3",
|
||||
"description": "Astro integration to generate an Apache .htaccess file",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
|
|
@ -33,12 +33,12 @@
|
|||
"author": "Bad Manners <npm@badmanners.xyz>",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"astro": "^4.0.0"
|
||||
"astro": ">= 4.0.0 < 6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@astrojs/check": "^0.9.3",
|
||||
"@astrojs/check": "^0.9.4",
|
||||
"@types/node": "^22.5.1",
|
||||
"astro": "^4.15.1",
|
||||
"astro": "^5.0.1",
|
||||
"typescript": "^5.5.4"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import type { AstroIntegration } from "astro";
|
||||
import { existsSync } from "node:fs";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path, { posix as pathPosix } from "node:path";
|
||||
import { fileURLToPath, URL } from "node:url";
|
||||
|
|
@ -50,8 +51,8 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
|
|||
generateHtaccessFile === undefined
|
||||
? true
|
||||
: typeof generateHtaccessFile === "function"
|
||||
? !(await generateHtaccessFile())
|
||||
: !generateHtaccessFile;
|
||||
? (await generateHtaccessFile())
|
||||
: generateHtaccessFile;
|
||||
}
|
||||
if (!enabled) {
|
||||
logger.debug("generateHtaccessFile evaluated to false; skipping integration config.");
|
||||
|
|
@ -65,7 +66,7 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
|
|||
assetsDir = fileURLToPath(new URL(".vercel/output/static/", config.root));
|
||||
} else if (config.adapter?.name === "@astrojs/cloudflare") {
|
||||
assetsDir = fileURLToPath(new URL(config.base?.replace(/^\//, ""), config.outDir));
|
||||
} else if (config.adapter?.name === "@astrojs/node" && config.output === "hybrid") {
|
||||
} else if (config.adapter?.name === "@astrojs/node") {
|
||||
assetsDir = fileURLToPath(config.build.client!);
|
||||
} else {
|
||||
assetsDir = fileURLToPath(config.outDir);
|
||||
|
|
@ -77,8 +78,8 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
|
|||
generateHtaccessFile === undefined
|
||||
? true
|
||||
: typeof generateHtaccessFile === "function"
|
||||
? !(await generateHtaccessFile())
|
||||
: !generateHtaccessFile;
|
||||
? (await generateHtaccessFile())
|
||||
: generateHtaccessFile;
|
||||
}
|
||||
if (!enabled) {
|
||||
logger.debug("generateHtaccessFile evaluated to false; skipping .htaccess generation.");
|
||||
|
|
@ -113,11 +114,16 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
|
|||
// Automatic error pages and Astro redirects
|
||||
() =>
|
||||
(!error &&
|
||||
routes.reduce((acc, { type, route, redirectRoute }) => {
|
||||
routes.reduce((acc, { type, route, redirect }) => {
|
||||
if (!error) {
|
||||
switch (type) {
|
||||
case "redirect":
|
||||
acc.push(`RedirectMatch 301 ^${route}(/(index.html)?)?$ ${redirectRoute!.route}`);
|
||||
const destination = typeof redirect === "string" ? redirect : redirect?.destination;
|
||||
if (destination) {
|
||||
acc.push(`RedirectMatch 301 ^${route}(/(index.html)?)?$ ${destination}`);
|
||||
} else {
|
||||
logger.warn(`No destination found for redirect route "${route}"! Skipping.`);
|
||||
}
|
||||
break;
|
||||
case "page":
|
||||
if (errorPages === undefined) {
|
||||
|
|
@ -171,7 +177,8 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
|
|||
.map((fn) => fn())
|
||||
.flat();
|
||||
if (!error) {
|
||||
await writeFile(path.join(assetsDir, ".htaccess"), htaccess.join("\n"));
|
||||
const htaccessPath = path.join(assetsDir, ".htaccess");
|
||||
await writeFile(htaccessPath, [existsSync(htaccessPath) ? "\n" : "", htaccess.join("\n")], { flag: 'a' });
|
||||
logger.info(`Generated .htaccess with ${htaccess.length} ${htaccess.length === 1 ? "rule" : "rules"}`);
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { integration as htaccessIntegration } from "./htaccess";
|
||||
export type { Config as HtaccessConfig, RedirectCode, ErrorCode } from "./htaccess";
|
||||
import { integration as htaccessIntegration } from "./htaccess.js";
|
||||
export type { Config as HtaccessConfig, RedirectCode, ErrorCode } from "./htaccess.d.ts";
|
||||
export { htaccessIntegration };
|
||||
export default htaccessIntegration;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"target": "ES2022",
|
||||
"moduleResolution": "bundler",
|
||||
"declaration": true,
|
||||
"stripInternal": true,
|
||||
"skipLibCheck": true,
|
||||
|
|
@ -10,5 +11,5 @@
|
|||
"noEmit": false,
|
||||
"allowImportingTsExtensions": false
|
||||
},
|
||||
"include": ["src"]
|
||||
"include": ["src", ".astro/types.d.ts"]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue