From 5e1134b9ce71a1e5110efe36d2a61c7cb5fc8748 Mon Sep 17 00:00:00 2001 From: Michael Salzmann Date: Wed, 18 Sep 2024 09:35:55 +0200 Subject: [PATCH] chore: add cookieconsent-script & full-story / UG integration (#2904) * chore: add cookieconsent-script & full-story integration * chore: fix script tags * chore: fix script type to enable proper autoblocking * chore: add UserGuiding integration * chore: add UserGuiding env variable to code * chore: add comment to each integration * chore: fix type & add comment for type=text/plain exception --- storybook/.storybook/main.ts | 13 ++++++++ storybook/.storybook/manager-head.prod.html | 32 +++++++++++++++++++ .../scripts/prepare-manager-head-file.ts | 20 ++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 storybook/.storybook/manager-head.prod.html create mode 100644 storybook/scripts/prepare-manager-head-file.ts diff --git a/storybook/.storybook/main.ts b/storybook/.storybook/main.ts index 906acfa442..d4f050483b 100644 --- a/storybook/.storybook/main.ts +++ b/storybook/.storybook/main.ts @@ -3,6 +3,14 @@ import react from '@vitejs/plugin-react-swc'; import ViteYaml from '@modyfi/vite-plugin-yaml'; import remarkGfm from 'remark-gfm'; import { join, dirname, resolve } from 'path'; +import { readFileSync } from 'fs'; +import { prepareManagerHeadFile } from './../scripts/prepare-manager-head-file'; + +const isProduction = process.env.NODE_ENV === 'production'; +const prodHead = readFileSync( + join(__dirname, './manager-head.prod.html'), + 'utf8' +); /** * This function is used to resolve the absolute path of a package. @@ -18,6 +26,11 @@ const config: StorybookConfig = { '../../packages/components/**/*.stories.@(js|jsx|mjs|ts|tsx)', '../../packages/components/**/*.mdx', ], + // head = the manager-header.html file contents + managerHead: (head) => ` + ${head} + ${isProduction ? prepareManagerHeadFile(prodHead) : ''} + `, addons: [ //getAbsolutePath('@storybook/addon-webpack5-compiler-swc'), getAbsolutePath('@storybook/addon-links'), diff --git a/storybook/.storybook/manager-head.prod.html b/storybook/.storybook/manager-head.prod.html new file mode 100644 index 0000000000..5d6d9569e9 --- /dev/null +++ b/storybook/.storybook/manager-head.prod.html @@ -0,0 +1,32 @@ + + + + + + + + diff --git a/storybook/scripts/prepare-manager-head-file.ts b/storybook/scripts/prepare-manager-head-file.ts new file mode 100644 index 0000000000..1ed1f57231 --- /dev/null +++ b/storybook/scripts/prepare-manager-head-file.ts @@ -0,0 +1,20 @@ +const envVars: Record = { + /** ENV's needed for CookieConsent script integration */ + COOKIELAW_ORG: process.env.COOKIELAW_ORG, + /** ENV's needed for FullStory integration */ + FS_HOST: process.env.FS_HOST, + FS_SCRIPT: process.env.FS_SCRIPT, + FS_ORG: process.env.FS_ORG, + /** ENV's needed for UserGuiding integration */ + USERGUIDING_ORG: process.env.USERGUIDING_ORG, +}; + +// this function replaces {{VARIABLE_NAME}} variables in the given string +export const prepareManagerHeadFile = (html: string) => { + return html.replace(/{{(.*?)}}/g, (_, varName): string => { + // Trim any extra spaces in the variable name + varName = varName.trim(); + // If the variable exists in the object, replace it. Otherwise, return an empty string + return envVars[varName] || ''; + }); +};