Compare commits

..

No commits in common. "381b22221065446f9ae4d3407af625d1e602e4ca" and "64354e10e709662e5ecded2d4000c6dd723788a0" have entirely different histories.

6 changed files with 1618 additions and 1155 deletions

View file

@ -1,13 +0,0 @@
# 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.2.3",
"version": "0.1.0",
"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 < 6"
"astro": "^4.0.0"
},
"devDependencies": {
"@astrojs/check": "^0.9.4",
"@astrojs/check": "^0.9.3",
"@types/node": "^22.5.1",
"astro": "^5.0.1",
"astro": "^4.15.1",
"typescript": "^5.5.4"
}
}

View file

@ -1,5 +1,4 @@
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";
@ -51,8 +50,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.");
@ -66,7 +65,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") {
} else if (config.adapter?.name === "@astrojs/node" && config.output === "hybrid") {
assetsDir = fileURLToPath(config.build.client!);
} else {
assetsDir = fileURLToPath(config.outDir);
@ -78,8 +77,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.");
@ -114,16 +113,11 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
// Automatic error pages and Astro redirects
() =>
(!error &&
routes.reduce((acc, { type, route, redirect }) => {
routes.reduce((acc, { type, route, redirectRoute }) => {
if (!error) {
switch (type) {
case "redirect":
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.`);
}
acc.push(`RedirectMatch 301 ^${route}(/(index.html)?)?$ ${redirectRoute!.route}`);
break;
case "page":
if (errorPages === undefined) {
@ -177,8 +171,7 @@ export const integration = ({ generateHtaccessFile, errorPages, redirects, custo
.map((fn) => fn())
.flat();
if (!error) {
const htaccessPath = path.join(assetsDir, ".htaccess");
await writeFile(htaccessPath, [existsSync(htaccessPath) ? "\n" : "", htaccess.join("\n")], { flag: 'a' });
await writeFile(path.join(assetsDir, ".htaccess"), htaccess.join("\n"));
logger.info(`Generated .htaccess with ${htaccess.length} ${htaccess.length === 1 ? "rule" : "rules"}`);
}
},

View file

@ -1,4 +1,4 @@
import { integration as htaccessIntegration } from "./htaccess.js";
export type { Config as HtaccessConfig, RedirectCode, ErrorCode } from "./htaccess.d.ts";
import { integration as htaccessIntegration } from "./htaccess";
export type { Config as HtaccessConfig, RedirectCode, ErrorCode } from "./htaccess";
export { htaccessIntegration };
export default htaccessIntegration;

View file

@ -3,7 +3,6 @@
"compilerOptions": {
"outDir": "./dist",
"target": "ES2022",
"moduleResolution": "bundler",
"declaration": true,
"stripInternal": true,
"skipLibCheck": true,
@ -11,5 +10,5 @@
"noEmit": false,
"allowImportingTsExtensions": false
},
"include": ["src", ".astro/types.d.ts"]
"include": ["src"]
}