From ee5a519120cad5bc2e561cf42a42ba3d04b35918 Mon Sep 17 00:00:00 2001 From: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:35:46 +0530 Subject: [PATCH] feat: lazy loading for @formbricks/surveys (#1940) Co-authored-by: Matthias Nannt Co-authored-by: ShubhamPalriwala --- .github/workflows/build-web.yml | 2 +- .github/workflows/e2e.yml | 71 +++++++++++++++++++ .github/workflows/playwright.yml | 40 ----------- .github/workflows/pr.yml | 2 +- apps/web/package.json | 2 + apps/web/playwright/utils/helper.ts | 9 ++- package.json | 2 + packages/api/package.json | 1 + packages/js/index.html | 6 +- packages/js/package.json | 5 +- packages/js/src/index.ts | 10 +++ packages/js/src/lib/actions.ts | 6 +- packages/js/src/lib/widget.ts | 27 ++++++- packages/js/src/vite-env.d.ts | 1 + packages/js/vite.config.js | 44 ++++++++---- packages/lib/actionClass/cache.ts | 2 +- packages/surveys/package.json | 6 +- .../surveys/src/components/general/Survey.tsx | 2 +- .../src/components/general/SurveyInline.tsx | 2 +- .../src/components/general/SurveyModal.tsx | 3 +- packages/surveys/src/index.ts | 19 ++++- .../props.ts => types/formbricksSurveys.ts} | 6 +- playwright.config.ts | 2 + pnpm-lock.yaml | 3 + turbo.json | 12 ++++ 25 files changed, 204 insertions(+), 81 deletions(-) create mode 100644 .github/workflows/e2e.yml delete mode 100644 .github/workflows/playwright.yml create mode 100644 packages/js/src/vite-env.d.ts rename packages/{surveys/src/types/props.ts => types/formbricksSurveys.ts} (82%) diff --git a/.github/workflows/build-web.yml b/.github/workflows/build-web.yml index f8b7d23c3d..2a5bdbd01f 100644 --- a/.github/workflows/build-web.yml +++ b/.github/workflows/build-web.yml @@ -25,7 +25,7 @@ jobs: - name: create .env run: cp .env.example .env - - name: Generate Random NEXTAUTH_SECRET + - name: Generate Random ENCRYPTION_KEY run: | SECRET=$(openssl rand -hex 32) echo "ENCRYPTION_KEY=$SECRET" >> $GITHUB_ENV diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000000..486e176930 --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,71 @@ +name: E2E Tests +on: + workflow_call: + workflow_dispatch: +jobs: + build: + name: Run E2E Tests + runs-on: ubuntu-latest + timeout-minutes: 60 + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 20 + + - name: Install Docker Compose + run: sudo apt-get update && sudo apt-get install -y docker-compose + + - name: Install dependencies + run: npm install -g pnpm && pnpm install + + - name: Install Playwright Browsers + run: pnpm exec playwright install --with-deps + + - name: create .env + run: cp .env.example .env + + - name: Generate ENCRYPTION_KEY + run: | + SECRET=$(openssl rand -hex 32) + echo "ENCRYPTION_KEY=$SECRET" >> $GITHUB_ENV + + - name: Start PostgreSQL + run: | + pnpm db:start + + - name: Build App in dev mode without external dependencies + run: | + pnpm build:dev --filter=web... + + - name: Serve packages for lazy loading + run: | + cd packages/surveys && pnpm serve & + + - name: Run App + run: | + NODE_ENV=test pnpm start --filter=web & + for attempt in {1..20}; do + if [ $(curl -o /dev/null -s -w "%{http_code}" http://localhost:3000/health) -eq 200 ]; then + echo "Ready" + break + fi + echo "Waiting..." + sleep 10 + done + + - name: Test Serve endpoints + run: | + curl -s http://localhost:3003 + + - name: Run E2E Tests + run: | + pnpm test:e2e + + - uses: actions/upload-artifact@v3 + if: always() + with: + name: playwright-report + path: playwright-report/ + retention-days: 30 diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml deleted file mode 100644 index caa270d414..0000000000 --- a/.github/workflows/playwright.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: E2E Tests -on: - workflow_call: -jobs: - build: - name: Run E2E Tests - runs-on: ubuntu-latest - timeout-minutes: 60 - - steps: - - uses: actions/checkout@v3 - - - uses: actions/setup-node@v3 - with: - node-version: 20 - - - name: Install Docker Compose - run: sudo apt-get update && sudo apt-get install -y docker-compose - - - name: Install dependencies - run: npm install -g pnpm && pnpm install - - - name: Build Formricks JS package - run: pnpm build --filter=js - - - name: Build Formbricks Image & Run - run: docker-compose up -d - - - name: Install Playwright Browsers - run: pnpm exec playwright install --with-deps - - - name: Run Playwright tests - run: pnpm test:e2e - - - uses: actions/upload-artifact@v3 - if: always() - with: - name: playwright-report - path: playwright-report/ - retention-days: 30 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 7ef24b7473..d66c769c81 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -29,7 +29,7 @@ jobs: e2e-test: name: Run E2E Tests - uses: ./.github/workflows/playwright.yml + uses: ./.github/workflows/e2e.yml secrets: inherit required: diff --git a/apps/web/package.json b/apps/web/package.json index 2275227183..00c5d960bb 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -7,6 +7,7 @@ "dev": "next dev -p 3000", "go": "next dev -p 3000", "build": "next build", + "build:dev": "next build", "start": "next start", "lint": "next lint" }, @@ -52,6 +53,7 @@ "react-hook-form": "^7.49.3", "react-hot-toast": "^2.4.1", "react-icons": "^5.0.1", + "sharp": "^0.33.1", "ua-parser-js": "^1.0.37", "webpack": "^5.89.0", "xlsx": "^0.18.5" diff --git a/apps/web/playwright/utils/helper.ts b/apps/web/playwright/utils/helper.ts index 8c73065f7e..c12078156c 100644 --- a/apps/web/playwright/utils/helper.ts +++ b/apps/web/playwright/utils/helper.ts @@ -13,10 +13,12 @@ export const signUpAndLogin = async ( await page.getByRole("button", { name: "Continue with Email" }).click(); await page.getByPlaceholder("Full Name").fill(name); await page.getByPlaceholder("Full Name").press("Tab"); + await page.getByPlaceholder("work@email.com").click(); await page.getByPlaceholder("work@email.com").fill(email); await page.getByPlaceholder("work@email.com").press("Tab"); + await page.getByPlaceholder("*******").click(); await page.getByPlaceholder("*******").fill(password); - await page.press('input[name="password"]', "Enter"); + await page.getByRole("button", { name: "Continue with Email" }).click(); await page.getByText("Login").click(); await page.getByRole("button", { name: "Login with Email" }).click(); await page.getByPlaceholder("work@email.com").fill(email); @@ -60,10 +62,11 @@ export const signupUsingInviteToken = async (page: Page, name: string, email: st // the email is already filled in the input field const inputValue = await page.getByPlaceholder("work@email.com").inputValue(); expect(inputValue).toEqual(email); - await page.getByPlaceholder("work@email.com").press("Tab"); + await page.getByPlaceholder("*******").click(); await page.getByPlaceholder("*******").fill(password); - await page.press('input[name="password"]', "Enter"); + await page.waitForTimeout(500); + await page.getByText("Continue with Email").click(); await page.getByText("Login").click(); await page.getByRole("button", { name: "Login with Email" }).click(); await page.getByPlaceholder("work@email.com").fill(email); diff --git a/package.json b/package.json index ac3d60835d..1de6b845c2 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,12 @@ "scripts": { "clean": "turbo run clean && rimraf node_modules .turbo coverage out", "build": "turbo run build", + "build:dev": "turbo run build:dev", "post-install": "turbo run post-install", "db:migrate:dev": "turbo run db:migrate:dev", "db:migrate:deploy": "turbo run db:migrate:deploy", "db:migrate:vercel": "turbo run db:migrate:vercel", + "db:start": "turbo run db:start", "db:push": "turbo run db:push", "go": "turbo run go --concurrency 20", "dev": "turbo run dev --parallel", diff --git a/packages/api/package.json b/packages/api/package.json index 65f74bb45b..ed411c1e4a 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -26,6 +26,7 @@ "scripts": { "dev": "vite build --watch", "build": "tsc && vite build", + "build:dev": "pnpm build", "go": "vite build --watch", "lint": "eslint ./src --fix", "clean": "rimraf .turbo node_modules dist" diff --git a/packages/js/index.html b/packages/js/index.html index ebc735a7a4..5c67a3884b 100644 --- a/packages/js/index.html +++ b/packages/js/index.html @@ -2,14 +2,12 @@