From b7fbd4e74e29f13028f537fdc246674a57aa0628 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Fri, 8 Jul 2022 13:06:25 +0200 Subject: [PATCH] Setup app to support prod published flow --- apps/comments-ui/package.json | 12 +++++++++++- apps/comments-ui/scripts/build-combined.js | 7 +++++++ apps/comments-ui/src/App.js | 6 +++--- apps/comments-ui/src/components/ShadowRoot.js | 4 +++- apps/comments-ui/src/index.js | 7 ++++--- apps/comments-ui/src/utils/helpers.js | 8 ++++++++ 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/apps/comments-ui/package.json b/apps/comments-ui/package.json index da1e5757e1..6b9ee2fbb5 100644 --- a/apps/comments-ui/package.json +++ b/apps/comments-ui/package.json @@ -1,9 +1,19 @@ { - "name": "comments-ui", + "name": "@tryghost/comments-ui", "version": "0.1.0", "license": "MIT", "repository": "git@github.com:TryGhost/comments-ui.git", "author": "Ghost Foundation", + "unpkg": "umd/comments-ui.umd.js", + "files": [ + "umd/", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + }, "dependencies": { "@sentry/react": "^7.5.0", "@testing-library/jest-dom": "5.16.2", diff --git a/apps/comments-ui/scripts/build-combined.js b/apps/comments-ui/scripts/build-combined.js index a2a85270b9..f2cf3f6268 100644 --- a/apps/comments-ui/scripts/build-combined.js +++ b/apps/comments-ui/scripts/build-combined.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const rewire = require('rewire'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const defaults = rewire('react-scripts/scripts/build.js'); @@ -27,3 +28,9 @@ config.module.rules[1].oneOf = config.module.rules[1].oneOf.map((rule) => { : options)) }); }); + +fs.copyFile('./public/main.css', './umd/main.css', (err) => { + if (err) { + throw err; + } +}); diff --git a/apps/comments-ui/src/App.js b/apps/comments-ui/src/App.js index d8a810c549..a4408785da 100644 --- a/apps/comments-ui/src/App.js +++ b/apps/comments-ui/src/App.js @@ -23,12 +23,12 @@ function AuthFrame({adminUrl, onLoad}) { ); } -function CommentsBoxContainer({done}) { +function CommentsBoxContainer({done, appVersion}) { if (!done) { return null; } return ( - + ); @@ -301,7 +301,7 @@ export default class App extends React.Component { return ( - + diff --git a/apps/comments-ui/src/components/ShadowRoot.js b/apps/comments-ui/src/components/ShadowRoot.js index b7bbe0b96c..541ec4d526 100644 --- a/apps/comments-ui/src/components/ShadowRoot.js +++ b/apps/comments-ui/src/components/ShadowRoot.js @@ -1,12 +1,14 @@ import React from 'react'; import root from 'react-shadow'; +import {getBundledCssLink} from '../utils/helpers'; const ShadowRoot = ({ children, ...props }) => { + const cssLink = getBundledCssLink({appVersion: props.appVersion}); const head = ( - + ); return ( diff --git a/apps/comments-ui/src/index.js b/apps/comments-ui/src/index.js index c2e109a682..ceb011e8ea 100644 --- a/apps/comments-ui/src/index.js +++ b/apps/comments-ui/src/index.js @@ -37,8 +37,9 @@ function getSiteData() { const colorScheme = scriptTag.dataset.colorScheme; const avatarSaturation = scriptTag.dataset.avatarSaturation; const accentColor = scriptTag.dataset.accentColor; + const appVersion = scriptTag.dataset.appVersion; - return {siteUrl, apiKey, apiUrl, sentryDsn, postId, adminUrl, colorScheme, avatarSaturation, accentColor}; + return {siteUrl, apiKey, apiUrl, sentryDsn, postId, adminUrl, colorScheme, avatarSaturation, accentColor, appVersion}; } return {}; } @@ -58,13 +59,13 @@ function setup({siteUrl}) { function init() { // const customSiteUrl = getSiteUrl(); - const {siteUrl: customSiteUrl, sentryDsn, postId, adminUrl, colorScheme, avatarSaturation, accentColor} = getSiteData(); + const {siteUrl: customSiteUrl, sentryDsn, postId, adminUrl, colorScheme, avatarSaturation, accentColor, appVersion} = getSiteData(); const siteUrl = customSiteUrl || window.location.origin; setup({siteUrl}); ReactDOM.render( - {} + {} , document.getElementById(ROOT_DIV_ID) ); diff --git a/apps/comments-ui/src/utils/helpers.js b/apps/comments-ui/src/utils/helpers.js index 504ea6c852..e4e8835252 100644 --- a/apps/comments-ui/src/utils/helpers.js +++ b/apps/comments-ui/src/utils/helpers.js @@ -104,3 +104,11 @@ export function getInitials(name) { return parts[0].substring(0, 1) + parts[parts.length - 1].substring(0, 1); } + +export function getBundledCssLink({appVersion}) { + if (process.env.NODE_ENV === 'production' && appVersion) { + return `https://unpkg.com/@tryghost/comments-ui@~${appVersion}/umd/main.css`; + } else { + return 'http://localhost:3000/main.css'; + } +}