-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom-routing): Refactor internally to use new template (#182)
- Loading branch information
Showing
9 changed files
with
114 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
node_modules | ||
*.log | ||
src | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'; | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.