diff --git a/.changeset/gentle-kids-worry.md b/.changeset/gentle-kids-worry.md new file mode 100644 index 00000000..48da2c75 --- /dev/null +++ b/.changeset/gentle-kids-worry.md @@ -0,0 +1,5 @@ +--- +'@inox-tools/custom-routing': patch +--- + +Refactor internally to use new Inox Tools template diff --git a/packages/custom-routing/.eslintrc.cjs b/packages/custom-routing/.eslintrc.cjs deleted file mode 100644 index c529ce8a..00000000 --- a/packages/custom-routing/.eslintrc.cjs +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - rules: { - '@typescript-eslint/ban-types': 'off', - '@typescript-eslint/no-namespace': 'off', - }, -}; diff --git a/packages/custom-routing/gitignore b/packages/custom-routing/gitignore deleted file mode 100644 index 45f72223..00000000 --- a/packages/custom-routing/gitignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules -*.log -lib diff --git a/packages/custom-routing/index.ts b/packages/custom-routing/index.ts deleted file mode 100644 index 805c3aa3..00000000 --- a/packages/custom-routing/index.ts +++ /dev/null @@ -1,62 +0,0 @@ -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; -}; - -function customRoutingInner({ strict, routes }: Options): AstroIntegration { - return { - name: '@inox-tools/custom-routing', - hooks: { - 'astro:config:setup': ({ injectRoute, updateConfig }) => { - for (const [route, handle] of Object.entries(routes)) { - injectRoute({ - entrypoint: handle, - pattern: route, - }); - } - - updateConfig({ - experimental: { - globalRoutePriority: true, - }, - }); - }, - ...(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, - }); -} diff --git a/packages/custom-routing/npmignore b/packages/custom-routing/npmignore index cace0d6d..d433a553 100644 --- a/packages/custom-routing/npmignore +++ b/packages/custom-routing/npmignore @@ -1,3 +1,3 @@ node_modules *.log -src +src \ No newline at end of file diff --git a/packages/custom-routing/package.json b/packages/custom-routing/package.json index f0d2595e..babd03e3 100644 --- a/packages/custom-routing/package.json +++ b/packages/custom-routing/package.json @@ -12,21 +12,30 @@ "author": "Luiz Ferraz ", "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" } } diff --git a/packages/custom-routing/src/index.ts b/packages/custom-routing/src/index.ts new file mode 100644 index 00000000..6f0f36c7 --- /dev/null +++ b/packages/custom-routing/src/index.ts @@ -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; +}; + +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, + }); +} diff --git a/packages/custom-routing/tsup.config.ts b/packages/custom-routing/tsup.config.ts new file mode 100644 index 00000000..db1befee --- /dev/null +++ b/packages/custom-routing/tsup.config.ts @@ -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]'; + }, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e98d46e4..bad4c588 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -624,12 +624,21 @@ importers: packages/custom-routing: devDependencies: + '@types/node': + specifier: 'catalog:' + version: 22.10.1 astro: specifier: 'catalog:' version: 5.0.1(@types/node@22.10.1)(jiti@2.0.0)(rollup@4.28.0)(typescript@5.7.2)(yaml@2.6.0) + tsup: + specifier: 'catalog:' + version: 8.3.5(jiti@2.0.0)(postcss@8.4.49)(typescript@5.7.2)(yaml@2.6.0) typescript: specifier: 'catalog:' version: 5.7.2 + vite: + specifier: 'catalog:' + version: 6.0.2(@types/node@22.10.1)(jiti@2.0.0)(yaml@2.6.0) packages/cut-short: dependencies: