From fa1f7e56250e13a58badd908e7ff46973ac18d16 Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Mon, 15 Jul 2024 01:55:50 +0200 Subject: [PATCH] feat(cdk-sanity-site): experimental release --- .changeset/selfish-steaks-applaud.md | 5 +++ packages/cdk-sanity-site/.npmignore | 5 +++ packages/cdk-sanity-site/LICENSE.md | 9 +++++ packages/cdk-sanity-site/README.md | 11 ++++++ packages/cdk-sanity-site/package.json | 38 +++++++++++++++++++ .../src/constructs/SanitySite.ts | 31 +++++++++++++++ packages/cdk-sanity-site/tsconfig.json | 3 ++ 7 files changed, 102 insertions(+) create mode 100644 .changeset/selfish-steaks-applaud.md create mode 100644 packages/cdk-sanity-site/.npmignore create mode 100644 packages/cdk-sanity-site/LICENSE.md create mode 100644 packages/cdk-sanity-site/README.md create mode 100644 packages/cdk-sanity-site/package.json create mode 100644 packages/cdk-sanity-site/src/constructs/SanitySite.ts create mode 100644 packages/cdk-sanity-site/tsconfig.json diff --git a/.changeset/selfish-steaks-applaud.md b/.changeset/selfish-steaks-applaud.md new file mode 100644 index 00000000..a7bd4790 --- /dev/null +++ b/.changeset/selfish-steaks-applaud.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-sanity-site": minor +--- + +Experimental release. diff --git a/packages/cdk-sanity-site/.npmignore b/packages/cdk-sanity-site/.npmignore new file mode 100644 index 00000000..37eddcc3 --- /dev/null +++ b/packages/cdk-sanity-site/.npmignore @@ -0,0 +1,5 @@ +# Source Code +src + +# Turbo Artifacts +.turbo diff --git a/packages/cdk-sanity-site/LICENSE.md b/packages/cdk-sanity-site/LICENSE.md new file mode 100644 index 00000000..4cd5ff85 --- /dev/null +++ b/packages/cdk-sanity-site/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2023 code d'azur Interactive B.V. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/cdk-sanity-site/README.md b/packages/cdk-sanity-site/README.md new file mode 100644 index 00000000..62c124c8 --- /dev/null +++ b/packages/cdk-sanity-site/README.md @@ -0,0 +1,11 @@ +# SanitySite + +This construct is simply a [StaticSite](https://github.com/codedazur/toolkit/tree/main/packages/cdk-static-site#readme) with the addition of a CloudFront ViewerRequest Function to rewrite requests to the `index.html` file, since Sanity is compiled to a single route. + +```ts +new SanitySite(this, "Sanity", { + src: "../sanity/dist", +}); +``` + +For more information on usage and configuration, please refer to the `StaticSite` readme. diff --git a/packages/cdk-sanity-site/package.json b/packages/cdk-sanity-site/package.json new file mode 100644 index 00000000..97a5bb50 --- /dev/null +++ b/packages/cdk-sanity-site/package.json @@ -0,0 +1,38 @@ +{ + "name": "@codedazur/cdk-sanity-site", + "version": "0.0.0", + "main": ".dist/index.js", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + } + }, + "publishConfig": { + "access": "public" + }, + "license": "MIT", + "scripts": { + "develop": "tsup src/index.ts --format esm,cjs --dts --watch", + "build": "tsup src/index.ts --format esm,cjs --dts", + "audit": "npm audit --omit dev", + "lint": "TIMING=1 eslint \"**/*.ts*\"", + "types": "tsc --noEmit", + "test": "vitest run --passWithNoTests" + }, + "peerDependencies": { + "aws-cdk-lib": ">=2", + "constructs": ">=10" + }, + "dependencies": { + "@codedazur/cdk-static-site": "^2.0.5" + }, + "devDependencies": { + "@types/node": "^20.14.9", + "aws-cdk-lib": "^2.147.2", + "constructs": "^10.3.0", + "esbuild": "^0.21.5" + } +} diff --git a/packages/cdk-sanity-site/src/constructs/SanitySite.ts b/packages/cdk-sanity-site/src/constructs/SanitySite.ts new file mode 100644 index 00000000..25cc2f9a --- /dev/null +++ b/packages/cdk-sanity-site/src/constructs/SanitySite.ts @@ -0,0 +1,31 @@ +import { StaticSite, StaticSiteProps } from "@codedazur/cdk-static-site"; +import { FunctionCode } from "aws-cdk-lib/aws-cloudfront"; +import { Construct } from "constructs"; + +export interface SanitySiteProps extends StaticSiteProps {} + +export class SanitySite extends StaticSite { + constructor(scope: Construct, id: string, props: SanitySiteProps) { + super(scope, id, { + ...props, + distribution: { + ...props.distribution, + functions: { + ...props.distribution?.functions, + viewerRequest: [ + ...(props.distribution?.functions?.viewerRequest ?? []), + FunctionCode.fromInline(/* js */ ` + function rewriteToIndex(event, next) { + if (!event.request.uri.match(/.*.[^?]*$/)) { + event.request.uri = "/index.html"; + } + + return next(event); + } + `), + ], + }, + }, + }); + } +} diff --git a/packages/cdk-sanity-site/tsconfig.json b/packages/cdk-sanity-site/tsconfig.json new file mode 100644 index 00000000..5c40eb77 --- /dev/null +++ b/packages/cdk-sanity-site/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@codedazur/tsconfig/cdk.json" +}