Compare commits

...

10 commits

6 changed files with 1155 additions and 1618 deletions

13
CHANGELOG.md Normal file
View 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

File diff suppressed because it is too large Load diff

View file

@ -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"
}
}

View file

@ -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"}`);
}
},

View file

@ -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;

View file

@ -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"]
}