Skip to content

Commit

Permalink
feat(custom-routing): Refactor internally to use new template (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Dec 10, 2024
1 parent 59025b5 commit f2e76e2
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-kids-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@inox-tools/custom-routing': patch
---

Refactor internally to use new Inox Tools template
6 changes: 0 additions & 6 deletions packages/custom-routing/.eslintrc.cjs

This file was deleted.

3 changes: 0 additions & 3 deletions packages/custom-routing/gitignore

This file was deleted.

62 changes: 0 additions & 62 deletions packages/custom-routing/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/custom-routing/npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
*.log
src
src
21 changes: 15 additions & 6 deletions packages/custom-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@
"author": "Luiz Ferraz <[email protected]>",
"type": "module",
"exports": {
".": "./index.ts"
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"files": [
"README.md",
"*.ts"
"dist",
"src"
],
"scripts": {
"build": "echo 'no build'",
"test": "echo 'no test'"
"build": "tsup",
"dev": "tsup --watch",
"prepublish": "pnpm run build",
"test": "echo 'no tests'"
},
"devDependencies": {
"@types/node": "catalog:",
"astro": "catalog:",
"typescript": "catalog:"
"tsup": "catalog:",
"typescript": "catalog:",
"vite": "catalog:"
},
"peerDependency": {
"peerDependencies": {
"astro": ">=3"
}
}
56 changes: 56 additions & 0 deletions packages/custom-routing/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { AstroIntegration } from 'astro';
import { AstroError } from 'astro/errors';
import { resolve, sep as PATH_SEP } from 'node:path';

// TODO: Add support for inline API route once Astro core accepts virtual modules
// as injected routes.
type CustomRoute = string; // | APIRoute;

type Options = {
strict: boolean;
routes: Record<string, CustomRoute>;
};

function customRoutingInner({ strict, routes }: Options): AstroIntegration {
return {
name: '@inox-tools/custom-routing',
hooks: {
'astro:config:setup': ({ injectRoute }) => {
for (const [route, handle] of Object.entries(routes)) {
injectRoute({
entrypoint: handle,
pattern: route,
});
}
},
...(strict && {
'astro:build:setup': ({ vite, pages }) => {
const pagesFolder = resolve(vite.root ?? '', './src/pages') + PATH_SEP;
for (const page of pages.values()) {
const componentPath = resolve(vite.root ?? '', page.component);
if (componentPath.startsWith(pagesFolder)) {
throw new AstroError(
'Custom routing used alongside pages route.',
'Either use disable strict mode of custom routing or remove any file-based routes.'
);
}
}
},
}),
},
};
}

export function customRouting(routes: Options['routes']): AstroIntegration {
return customRoutingInner({
strict: false,
routes: routes,
});
}

export function strictCustomRouting(routes: Options['routes']): AstroIntegration {
return customRoutingInner({
strict: true,
routes: routes,
});
}
28 changes: 28 additions & 0 deletions packages/custom-routing/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from 'tsup';
import { readFileSync } from 'node:fs';

const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'));
const dependencies = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {}),
];
const devDependencies = [...Object.keys(packageJson.devDependencies || {})];

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm'],
target: 'node18',
bundle: true,
dts: true,
sourcemap: true,
clean: true,
splitting: true,
minify: false,
external: dependencies,
noExternal: devDependencies,
treeshake: 'smallest',
tsconfig: 'tsconfig.json',
esbuildOptions: (options) => {
options.chunkNames = 'chunks/[name]-[hash]';
},
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2e76e2

Please sign in to comment.