@openedx/frontend-base 1.0.0-alpha.6 → 1.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/config/eslint/base.eslint.config.js +0 -1
  2. package/config/webpack/common-config/all/getStylesheetRule.js +1 -1
  3. package/config/webpack/webpack.config.build.js +0 -11
  4. package/config/webpack/webpack.config.dev.js +0 -11
  5. package/config/webpack/webpack.config.dev.shell.js +0 -11
  6. package/package.json +4 -3
  7. package/runtime/config/index.ts +2 -3
  8. package/runtime/react/SiteProvider.tsx +26 -3
  9. package/runtime/react/constants.ts +3 -0
  10. package/runtime/react/hooks/index.ts +8 -0
  11. package/runtime/react/hooks/theme/index.ts +2 -0
  12. package/runtime/react/hooks/theme/useTheme.test.ts +221 -0
  13. package/runtime/react/hooks/theme/useTheme.ts +179 -0
  14. package/runtime/react/hooks/theme/useThemeConfig.test.ts +107 -0
  15. package/runtime/react/hooks/theme/useThemeConfig.ts +34 -0
  16. package/runtime/react/hooks/theme/useThemeCore.test.ts +65 -0
  17. package/runtime/react/hooks/theme/useThemeCore.ts +52 -0
  18. package/runtime/react/hooks/theme/useThemeVariants.test.ts +97 -0
  19. package/runtime/react/hooks/theme/useThemeVariants.ts +116 -0
  20. package/runtime/react/hooks/theme/useTrackColorSchemeChoice.test.ts +54 -0
  21. package/runtime/react/hooks/theme/useTrackColorSchemeChoice.ts +30 -0
  22. package/runtime/react/hooks/theme/utils.ts +11 -0
  23. package/runtime/react/hooks/useActiveRoles.ts +15 -0
  24. package/runtime/react/hooks/useActiveRouteRoleWatcher.ts +31 -0
  25. package/runtime/react/hooks/useAppConfig.ts +9 -0
  26. package/runtime/react/hooks/useAuthenticatedUser.test.tsx +41 -0
  27. package/runtime/react/hooks/useAuthenticatedUser.ts +9 -0
  28. package/runtime/react/hooks/useSiteConfig.test.tsx +13 -0
  29. package/runtime/react/hooks/useSiteConfig.ts +9 -0
  30. package/runtime/react/hooks/useSiteEvent.ts +24 -0
  31. package/runtime/react/reducers.ts +40 -0
  32. package/runtime/setupTest.js +0 -35
  33. package/runtime/testing/initializeMockApp.ts +5 -0
  34. package/shell/app.scss +2 -1
  35. package/shell/setupTest.js +0 -35
  36. package/tools/dist/eslint/base.eslint.config.js +0 -1
  37. package/tools/dist/webpack/common-config/all/getStylesheetRule.js +1 -1
  38. package/tools/dist/webpack/webpack.config.build.js +0 -11
  39. package/tools/dist/webpack/webpack.config.dev.js +0 -11
  40. package/tools/dist/webpack/webpack.config.dev.shell.js +0 -11
  41. package/types.ts +20 -0
  42. package/config/webpack/plugins/paragon-webpack-plugin/ParagonWebpackPlugin.js +0 -108
  43. package/config/webpack/plugins/paragon-webpack-plugin/index.js +0 -7
  44. package/config/webpack/plugins/paragon-webpack-plugin/utils/assetUtils.js +0 -64
  45. package/config/webpack/plugins/paragon-webpack-plugin/utils/htmlUtils.js +0 -53
  46. package/config/webpack/plugins/paragon-webpack-plugin/utils/index.js +0 -9
  47. package/config/webpack/plugins/paragon-webpack-plugin/utils/paragonStylesheetUtils.js +0 -114
  48. package/config/webpack/plugins/paragon-webpack-plugin/utils/scriptUtils.js +0 -146
  49. package/config/webpack/plugins/paragon-webpack-plugin/utils/stylesheetUtils.js +0 -126
  50. package/config/webpack/plugins/paragon-webpack-plugin/utils/tagUtils.js +0 -57
  51. package/config/webpack/types.js +0 -2
  52. package/config/webpack/utils/paragonUtils.js +0 -138
  53. package/runtime/react/hooks.test.jsx +0 -104
  54. package/runtime/react/hooks.ts +0 -106
  55. package/tools/dist/webpack/plugins/paragon-webpack-plugin/ParagonWebpackPlugin.js +0 -108
  56. package/tools/dist/webpack/plugins/paragon-webpack-plugin/index.js +0 -7
  57. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/assetUtils.js +0 -64
  58. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/htmlUtils.js +0 -53
  59. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/index.js +0 -9
  60. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/paragonStylesheetUtils.js +0 -114
  61. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/scriptUtils.js +0 -146
  62. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/stylesheetUtils.js +0 -126
  63. package/tools/dist/webpack/plugins/paragon-webpack-plugin/utils/tagUtils.js +0 -57
  64. package/tools/dist/webpack/types.js +0 -2
  65. package/tools/dist/webpack/utils/paragonUtils.js +0 -138
@@ -1,104 +0,0 @@
1
- import { act, renderHook } from '@testing-library/react';
2
- import siteConfig from 'site.config';
3
- import { EnvironmentTypes } from '../../types';
4
- import { sendTrackEvent } from '../analytics';
5
- import { setAuthenticatedUser } from '../auth';
6
- import { initializeMockApp } from '../testing';
7
- import SiteProvider from './SiteProvider';
8
- import { useAuthenticatedUser, useSiteConfig, useTrackColorSchemeChoice } from './hooks';
9
-
10
- jest.mock('../analytics');
11
-
12
- const mockAddEventListener = jest.fn();
13
- const mockRemoveEventListener = jest.fn();
14
- let matchesMock;
15
-
16
- Object.defineProperty(window, 'matchMedia', {
17
- value: jest.fn(() => ({
18
- addEventListener: mockAddEventListener,
19
- removeEventListener: mockRemoveEventListener,
20
- matches: matchesMock,
21
- })),
22
- });
23
-
24
- describe('useTrackColorSchemeChoice hook', () => {
25
- afterEach(() => {
26
- mockAddEventListener.mockClear();
27
- mockRemoveEventListener.mockClear();
28
- sendTrackEvent.mockClear();
29
- });
30
-
31
- it('sends dark preferred color schema event if query matches', async () => {
32
- matchesMock = true;
33
- renderHook(() => useTrackColorSchemeChoice());
34
-
35
- expect(sendTrackEvent).toHaveBeenCalledTimes(1);
36
- expect(sendTrackEvent).toHaveBeenCalledWith(
37
- 'openedx.ui.frontend-base.prefers-color-scheme.selected',
38
- { preferredColorScheme: 'dark' },
39
- );
40
- });
41
-
42
- it('sends light preferred color schema event if query does not match', async () => {
43
- matchesMock = false;
44
- renderHook(() => useTrackColorSchemeChoice());
45
-
46
- expect(sendTrackEvent).toHaveBeenCalledTimes(1);
47
- expect(sendTrackEvent).toHaveBeenCalledWith(
48
- 'openedx.ui.frontend-base.prefers-color-scheme.selected',
49
- { preferredColorScheme: 'light' },
50
- );
51
- });
52
-
53
- it('adds change event listener to matchMedia query', async () => {
54
- renderHook(() => useTrackColorSchemeChoice());
55
-
56
- expect(mockAddEventListener).toHaveBeenCalledTimes(1);
57
- expect(mockAddEventListener).toHaveBeenCalledWith('change', expect.any(Function));
58
- });
59
- });
60
-
61
- describe('useAuthenticatedUser', () => {
62
- it('returns null when the user is anonymous', () => {
63
- const { result } = renderHook(() => useAuthenticatedUser());
64
- expect(result.current).toBeNull();
65
- });
66
-
67
- describe('with a user', () => {
68
- const user = {
69
- administrator: true,
70
- email: 'admin@example.com',
71
- name: 'Admin',
72
- roles: ['admin'],
73
- userId: 1,
74
- username: 'admin-user',
75
- avatar: 'http://localhost/admin.png',
76
- };
77
-
78
- beforeEach(() => {
79
- initializeMockApp({
80
- authenticatedUser: user,
81
- });
82
- });
83
-
84
- afterEach(() => {
85
- act(() => {
86
- setAuthenticatedUser(null);
87
- });
88
- });
89
-
90
- it('returns a User when the user exists', () => {
91
- const { result } = renderHook(() => useAuthenticatedUser(), { wrapper: SiteProvider });
92
- expect(result.current).toBe(user);
93
- });
94
- });
95
- });
96
-
97
- describe('useSiteConfig', () => {
98
- it('returns the site config', () => {
99
- const { result } = renderHook(() => useSiteConfig());
100
- expect(result.current).toHaveProperty('apps', siteConfig.apps);
101
- expect(result.current).toHaveProperty('environment', EnvironmentTypes.TEST);
102
- expect(result.current).toHaveProperty('baseUrl', 'http://localhost:8080');
103
- });
104
- });
@@ -1,106 +0,0 @@
1
- import { useCallback, useContext, useEffect, useState } from 'react';
2
- import { useMatches } from 'react-router';
3
-
4
- import { sendTrackEvent } from '../analytics';
5
- import { getActiveRoles, setActiveRouteRoles } from '../config';
6
- import { ACTIVE_ROLES_CHANGED } from '../constants';
7
- import { isRoleRouteObject } from '../routing';
8
- import { subscribe, unsubscribe } from '../subscriptions';
9
-
10
- import SiteContext from './SiteContext';
11
- import CurrentAppContext from './CurrentAppContext';
12
-
13
- /**
14
- * A React hook that allows functional components to subscribe to application events. This should
15
- * be used sparingly - for the most part, Context should be used higher-up in the application to
16
- * provide necessary data to a given component, rather than utilizing a non-React-like Pub/Sub
17
- * mechanism.
18
- *
19
- * @memberof module:React
20
- * @param {string} type
21
- * @param {function} callback
22
- */
23
- export const useSiteEvent = (type, callback) => {
24
- useEffect(() => {
25
- subscribe(type, callback);
26
-
27
- return () => {
28
- unsubscribe(type, callback);
29
- };
30
- }, [callback, type]);
31
- };
32
-
33
- /**
34
- * A React hook that tracks user's preferred color scheme (light or dark) and sends respective
35
- * event to the tracking service.
36
- *
37
- * @memberof module:React
38
- */
39
- export const useTrackColorSchemeChoice = () => {
40
- useEffect(() => {
41
- const trackColorSchemeChoice = ({ matches }) => {
42
- const preferredColorScheme = matches ? 'dark' : 'light';
43
- sendTrackEvent('openedx.ui.frontend-base.prefers-color-scheme.selected', { preferredColorScheme });
44
- };
45
- const colorSchemeQuery = window.matchMedia?.('(prefers-color-scheme: dark)');
46
- if (colorSchemeQuery) {
47
- // send user's initial choice
48
- trackColorSchemeChoice(colorSchemeQuery);
49
- colorSchemeQuery.addEventListener('change', trackColorSchemeChoice);
50
- }
51
- return () => {
52
- if (colorSchemeQuery) {
53
- colorSchemeQuery.removeEventListener('change', trackColorSchemeChoice);
54
- }
55
- };
56
- }, []);
57
- };
58
-
59
- export function useAuthenticatedUser() {
60
- const { authenticatedUser } = useContext(SiteContext);
61
- return authenticatedUser;
62
- }
63
-
64
- export function useSiteConfig() {
65
- const { siteConfig } = useContext(SiteContext);
66
- return siteConfig;
67
- }
68
-
69
- export function useAppConfig() {
70
- const { appConfig } = useContext(CurrentAppContext);
71
- return appConfig;
72
- }
73
-
74
- export function useActiveRouteRoleWatcher() {
75
- const matches = useMatches();
76
-
77
- // We create this callback so we can use it right away to populate the default state value.
78
- const findActiveRouteRoles = useCallback(() => {
79
- // Starts with the widget roles and adds the others in.
80
- const roles: string[] = [];
81
-
82
- // Route roles
83
- for (const match of matches) {
84
- if (isRoleRouteObject(match)) {
85
- if (!roles.includes(match.handle.role)) {
86
- roles.push(match.handle.role);
87
- }
88
- }
89
- }
90
-
91
- return roles;
92
- }, [matches]);
93
-
94
- useEffect(() => {
95
- setActiveRouteRoles(findActiveRouteRoles());
96
- }, [matches, findActiveRouteRoles]);
97
- }
98
-
99
- export function useActiveRoles() {
100
- const [roles, setRoles] = useState<string[]>(getActiveRoles());
101
- useSiteEvent(ACTIVE_ROLES_CHANGED, () => {
102
- setRoles(getActiveRoles());
103
- });
104
-
105
- return roles;
106
- }
@@ -1,108 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const webpack_1 = require("webpack");
4
- const paragonUtils_1 = require("../../utils/paragonUtils");
5
- const utils_1 = require("./utils");
6
- // Get Paragon and brand versions / CSS files from disk.
7
- const paragonVersion = (0, paragonUtils_1.getParagonVersion)(process.cwd());
8
- const paragonThemeCss = (0, paragonUtils_1.getParagonThemeCss)(process.cwd());
9
- const brandVersion = (0, paragonUtils_1.getParagonVersion)(process.cwd(), { isBrandOverride: true });
10
- const brandThemeCss = (0, paragonUtils_1.getParagonThemeCss)(process.cwd(), { isBrandOverride: true });
11
- /**
12
- * 1. Injects `PARAGON_THEME` global variable into the HTML document during the Webpack compilation process.
13
- * 2. Injects `<link rel="preload" as="style">` element(s) for the Paragon and brand CSS into the HTML document.
14
- */
15
- class ParagonWebpackPlugin {
16
- pluginName;
17
- paragonThemeUrlsConfig;
18
- processAssetsHandlers;
19
- paragonMetadata;
20
- constructor({ processAssetsHandlers = [] } = {}) {
21
- this.pluginName = 'ParagonWebpackPlugin';
22
- this.paragonThemeUrlsConfig = {};
23
- this.paragonMetadata = {};
24
- // List of handlers to be executed after processing assets during the Webpack compilation.
25
- this.processAssetsHandlers = [
26
- this.resolveParagonThemeUrlsFromConfig,
27
- this.injectParagonMetadataIntoDocument,
28
- this.injectParagonStylesheetsIntoDocument,
29
- ...processAssetsHandlers,
30
- ].map(handler => handler.bind(this));
31
- }
32
- /**
33
- * Resolves the MFE configuration from ``PARAGON_THEME_URLS`` in the environment variables. `
34
- *
35
- * @returns {Object} Metadata about the Paragon and brand theme URLs from configuration.
36
- */
37
- async resolveParagonThemeUrlsFromConfig() {
38
- try {
39
- this.paragonThemeUrlsConfig = JSON.parse(process.env.PARAGON_THEME_URLS ?? '{}');
40
- }
41
- catch (error) {
42
- console.info('Paragon Plugin cannot load PARAGON_THEME_URLS env variable, skipping.');
43
- }
44
- }
45
- /**
46
- * Generates `PARAGON_THEME` global variable in HTML document.
47
- * @param {Object} compilation Webpack compilation object.
48
- */
49
- injectParagonMetadataIntoDocument(compilation) {
50
- const paragonMetadata = (0, utils_1.injectMetadataIntoDocument)(compilation, {
51
- paragonThemeCss,
52
- paragonVersion,
53
- brandThemeCss,
54
- brandVersion,
55
- });
56
- if (paragonMetadata) {
57
- this.paragonMetadata = paragonMetadata;
58
- }
59
- }
60
- injectParagonStylesheetsIntoDocument(compilation) {
61
- const file = compilation.getAsset('index.html');
62
- // If the `index.html` hasn't loaded yet, or there are no Paragon theme URLs, then there is nothing to do yet.
63
- if (!file || Object.keys(this.paragonThemeUrlsConfig || {}).length === 0) {
64
- return;
65
- }
66
- // Generates `<link rel="preload" as="style">` element(s) for the Paragon and brand CSS files.
67
- const paragonStylesheetUrls = (0, utils_1.getParagonStylesheetUrls)({
68
- paragonThemeUrls: this.paragonThemeUrlsConfig,
69
- paragonVersion,
70
- brandVersion,
71
- });
72
- const { core: paragonCoreCss, variants: paragonThemeVariantCss, } = paragonStylesheetUrls;
73
- // We do not expect to have a Buffer here, ever.
74
- const originalSource = file.source.source();
75
- // Inject core CSS
76
- let newSource = (0, utils_1.injectParagonCoreStylesheets)({
77
- source: originalSource,
78
- paragonCoreCss,
79
- paragonThemeCss,
80
- brandThemeCss,
81
- });
82
- // Inject theme variant CSS
83
- newSource = (0, utils_1.injectParagonThemeVariantStylesheets)({
84
- // @ts-expect-error newSource is possibly undefined here.
85
- source: newSource.source(),
86
- paragonThemeVariantCss,
87
- paragonThemeCss,
88
- brandThemeCss,
89
- });
90
- // @ts-expect-error newSource is possibly undefined here.
91
- compilation.updateAsset('index.html', new webpack_1.sources.RawSource(newSource.source()));
92
- }
93
- apply(compiler) {
94
- compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
95
- compilation.hooks.processAssets.tap({
96
- name: this.pluginName,
97
- stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
98
- additionalAssets: true,
99
- }, () => {
100
- // Iterate through each configured handler, passing the compilation to each.
101
- this.processAssetsHandlers.forEach(async (handler) => {
102
- await handler(compilation);
103
- });
104
- });
105
- });
106
- }
107
- }
108
- exports.default = ParagonWebpackPlugin;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const ParagonWebpackPlugin_1 = __importDefault(require("./ParagonWebpackPlugin"));
7
- exports.default = ParagonWebpackPlugin_1.default;
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findCoreCssAsset = findCoreCssAsset;
4
- exports.findThemeVariantCssAssets = findThemeVariantCssAssets;
5
- exports.getCssAssetsFromCompilation = getCssAssetsFromCompilation;
6
- /**
7
- * Finds the core CSS asset from the given array of Paragon assets.
8
- *
9
- * @param {Array} paragonAssets - An array of Paragon assets.
10
- * @return {Object|undefined} The core CSS asset, or undefined if not found.
11
- */
12
- function findCoreCssAsset(paragonAssets) {
13
- return paragonAssets?.find((asset) => asset.name.includes('core') && asset.name.endsWith('.css'));
14
- }
15
- /**
16
- * Finds the theme variant CSS assets from the given Paragon assets based on the provided options.
17
- *
18
- * @param {Array} paragonAssets - An array of Paragon assets.
19
- * @param {Object} options - The options for finding the theme variant CSS assets.
20
- * @param {boolean} [options.isBrandOverride=false] - Indicates if the theme variant is a brand override.
21
- * @param {Object} [options.brandThemeCss] - The brand theme CSS object.
22
- * @param {Object} [options.paragonThemeCss] - The Paragon theme CSS object.
23
- * @return {Object} - The theme variant CSS assets.
24
- */
25
- function findThemeVariantCssAssets(paragonAssets, { isBrandOverride = false, brandThemeCss, paragonThemeCss, }) {
26
- const themeVariantsSource = isBrandOverride ? brandThemeCss?.variants : paragonThemeCss?.variants;
27
- const themeVariantCssAssets = {};
28
- Object.entries(themeVariantsSource || {}).forEach(([themeVariant, value]) => {
29
- const foundThemeVariantAsset = paragonAssets.find((asset) => asset.name.includes(value.outputChunkName));
30
- if (!foundThemeVariantAsset) {
31
- return;
32
- }
33
- themeVariantCssAssets[themeVariant] = {
34
- fileName: foundThemeVariantAsset.name,
35
- };
36
- });
37
- return themeVariantCssAssets;
38
- }
39
- /**
40
- * Retrieves the CSS assets from the compilation based on the provided options.
41
- *
42
- * @param {Object} compilation - The compilation object.
43
- * @param {Object} options - The options for retrieving the CSS assets.
44
- * @param {boolean} [options.isBrandOverride=false] - Indicates if the assets are for a brand override.
45
- * @param {Object} [options.brandThemeCss] - The brand theme CSS object.
46
- * @param {Object} [options.paragonThemeCss] - The Paragon theme CSS object.
47
- * @return {Object} - The CSS assets, including the core CSS asset and theme variant CSS assets.
48
- */
49
- function getCssAssetsFromCompilation(compilation, { isBrandOverride = false, brandThemeCss, paragonThemeCss, }) {
50
- const assetSubstring = isBrandOverride ? 'brand' : 'paragon';
51
- const paragonAssets = compilation.getAssets().filter(asset => asset.name.includes(assetSubstring) && asset.name.endsWith('.css'));
52
- const coreCssAsset = findCoreCssAsset(paragonAssets);
53
- const themeVariantCssAssets = findThemeVariantCssAssets(paragonAssets, {
54
- isBrandOverride,
55
- paragonThemeCss,
56
- brandThemeCss,
57
- });
58
- return {
59
- coreCssAsset: {
60
- fileName: coreCssAsset?.name,
61
- },
62
- themeVariantCssAssets,
63
- };
64
- }
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.injectMetadataIntoDocument = injectMetadataIntoDocument;
4
- const webpack_1 = require("webpack");
5
- const assetUtils_1 = require("./assetUtils");
6
- const scriptUtils_1 = require("./scriptUtils");
7
- /**
8
- * Injects metadata into the HTML document by modifying the 'index.html' asset in the compilation.
9
- *
10
- * @param {Object} compilation - The Webpack compilation object.
11
- * @param {Object} options - The options object.
12
- * @param {Object} options.paragonThemeCss - The Paragon theme CSS object.
13
- * @param {string} options.paragonVersion - The version of the Paragon theme.
14
- * @param {Object} options.brandThemeCss - The brand theme CSS object.
15
- * @param {string} options.brandVersion - The version of the brand theme.
16
- * @return {Object|undefined} The script contents object if the 'index.html' asset exists, otherwise undefined.
17
- */
18
- function injectMetadataIntoDocument(compilation, { paragonThemeCss, paragonVersion, brandThemeCss, brandVersion, }) {
19
- const file = compilation.getAsset('index.html');
20
- if (!file) {
21
- return undefined;
22
- }
23
- const { coreCssAsset: paragonCoreCssAsset, themeVariantCssAssets: paragonThemeVariantCssAssets, } = (0, assetUtils_1.getCssAssetsFromCompilation)(compilation, {
24
- brandThemeCss,
25
- paragonThemeCss,
26
- });
27
- const { coreCssAsset: brandCoreCssAsset, themeVariantCssAssets: brandThemeVariantCssAssets, } = (0, assetUtils_1.getCssAssetsFromCompilation)(compilation, {
28
- isBrandOverride: true,
29
- brandThemeCss,
30
- paragonThemeCss,
31
- });
32
- const scriptContents = (0, scriptUtils_1.generateScriptContents)({
33
- paragonCoreCssAsset,
34
- paragonThemeVariantCssAssets,
35
- brandCoreCssAsset,
36
- brandThemeVariantCssAssets,
37
- paragonThemeCss,
38
- paragonVersion,
39
- brandThemeCss,
40
- brandVersion,
41
- });
42
- // We expect this to be a string at all times.
43
- const originalSource = file.source.source();
44
- const newSource = (0, scriptUtils_1.insertScriptContentsIntoDocument)({
45
- originalSource,
46
- // @ts-expect-error This parameter doesn't exist in the function.
47
- coreCssAsset: paragonCoreCssAsset,
48
- themeVariantCssAssets: paragonThemeVariantCssAssets,
49
- scriptContents,
50
- });
51
- compilation.updateAsset('index.html', new webpack_1.sources.RawSource(newSource.source()));
52
- return scriptContents;
53
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.injectParagonThemeVariantStylesheets = exports.injectParagonCoreStylesheets = exports.getParagonStylesheetUrls = exports.injectMetadataIntoDocument = void 0;
4
- var htmlUtils_1 = require("./htmlUtils");
5
- Object.defineProperty(exports, "injectMetadataIntoDocument", { enumerable: true, get: function () { return htmlUtils_1.injectMetadataIntoDocument; } });
6
- var paragonStylesheetUtils_1 = require("./paragonStylesheetUtils");
7
- Object.defineProperty(exports, "getParagonStylesheetUrls", { enumerable: true, get: function () { return paragonStylesheetUtils_1.getParagonStylesheetUrls; } });
8
- Object.defineProperty(exports, "injectParagonCoreStylesheets", { enumerable: true, get: function () { return paragonStylesheetUtils_1.injectParagonCoreStylesheets; } });
9
- Object.defineProperty(exports, "injectParagonThemeVariantStylesheets", { enumerable: true, get: function () { return paragonStylesheetUtils_1.injectParagonThemeVariantStylesheets; } });
@@ -1,114 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.injectParagonCoreStylesheets = injectParagonCoreStylesheets;
4
- exports.injectParagonThemeVariantStylesheets = injectParagonThemeVariantStylesheets;
5
- exports.getParagonStylesheetUrls = getParagonStylesheetUrls;
6
- const stylesheetUtils_1 = require("./stylesheetUtils");
7
- const tagUtils_1 = require("./tagUtils");
8
- /**
9
- * Injects Paragon core stylesheets into the document.
10
- *
11
- * @param {Object} options - The options object.
12
- * @param {string|object} options.source - The source HTML document.
13
- * @param {Object} options.paragonCoreCss - The Paragon core CSS object.
14
- * @param {Object} options.paragonThemeCss - The Paragon theme CSS object.
15
- * @param {Object} options.brandThemeCss - The brand theme CSS object.
16
- * @return {string|object} The modified HTML document with Paragon core stylesheets injected.
17
- */
18
- function injectParagonCoreStylesheets({ source, paragonCoreCss, paragonThemeCss, brandThemeCss, }) {
19
- return (0, stylesheetUtils_1.insertStylesheetsIntoDocument)({
20
- source,
21
- urls: paragonCoreCss.urls,
22
- // @ts-expect-error These two parameters don't exist on insertStylesheetsIntoDocument
23
- paragonThemeCss,
24
- brandThemeCss,
25
- });
26
- }
27
- /**
28
- * Injects Paragon theme variant stylesheets into the document.
29
- *
30
- * @param {Object} options - The options object.
31
- * @param {string|object} options.source - The source HTML document.
32
- * @param {Object} options.paragonThemeVariantCss - The Paragon theme variant CSS object.
33
- * @param {Object} options.paragonThemeCss - The Paragon theme CSS object.
34
- * @param {Object} options.brandThemeCss - The brand theme CSS object.
35
- * @return {string|object} The modified HTML document with Paragon theme variant stylesheets injected.
36
- */
37
- function injectParagonThemeVariantStylesheets({ source, paragonThemeVariantCss, paragonThemeCss, brandThemeCss, }) {
38
- let newSource = source;
39
- Object.values(paragonThemeVariantCss).forEach(({ urls }) => {
40
- newSource = (0, stylesheetUtils_1.insertStylesheetsIntoDocument)({
41
- source: (typeof newSource === 'object' ? newSource.source() : newSource),
42
- urls,
43
- // @ts-expect-error These two parameters don't exist on insertStylesheetsIntoDocument
44
- paragonThemeCss,
45
- brandThemeCss,
46
- });
47
- });
48
- return newSource;
49
- }
50
- /**
51
- * Retrieves the URLs of the Paragon stylesheets based on the provided theme URLs, Paragon version, and brand version.
52
- *
53
- * @param {Object} options - The options object.
54
- * @param {Object} options.paragonThemeUrls - The URLs of the Paragon theme.
55
- * @param {string} options.paragonVersion - The version of the Paragon theme.
56
- * @param {string} options.brandVersion - The version of the brand theme.
57
- * @return {Object} An object containing the URLs of the Paragon stylesheets.
58
- */
59
- function getParagonStylesheetUrls({ paragonThemeUrls, paragonVersion, brandVersion }) {
60
- const paragonCoreCssUrl = 'urls' in paragonThemeUrls.core && paragonThemeUrls.core.urls !== undefined ? paragonThemeUrls.core.urls.default : paragonThemeUrls.core.url;
61
- const brandCoreCssUrl = 'urls' in paragonThemeUrls.core && paragonThemeUrls.core.urls !== undefined ? paragonThemeUrls.core.urls.brandOverride : undefined;
62
- const defaultThemeVariants = paragonThemeUrls.defaults || {};
63
- const coreCss = {
64
- urls: {
65
- default: (0, tagUtils_1.handleVersionSubstitution)({
66
- url: paragonCoreCssUrl,
67
- wildcardKeyword: '$paragonVersion',
68
- localVersion: paragonVersion
69
- }),
70
- brandOverride: (0, tagUtils_1.handleVersionSubstitution)({
71
- url: brandCoreCssUrl,
72
- wildcardKeyword: '$brandVersion',
73
- localVersion: brandVersion
74
- }),
75
- },
76
- };
77
- const themeVariantsCss = {};
78
- const themeVariantsEntries = Object.entries(paragonThemeUrls.variants || {});
79
- themeVariantsEntries.forEach(([themeVariant, { url, urls }]) => {
80
- const themeVariantMetadata = { urls: {} };
81
- if (url) {
82
- themeVariantMetadata.urls = {
83
- default: (0, tagUtils_1.handleVersionSubstitution)({
84
- url,
85
- wildcardKeyword: '$paragonVersion',
86
- localVersion: paragonVersion,
87
- }),
88
- // If there is no brand override URL, then we don't need to do any version substitution
89
- // but we still need to return the property.
90
- brandOverride: undefined,
91
- };
92
- }
93
- else if (urls) {
94
- themeVariantMetadata.urls = {
95
- default: (0, tagUtils_1.handleVersionSubstitution)({
96
- url: urls.default,
97
- wildcardKeyword: '$paragonVersion',
98
- localVersion: paragonVersion,
99
- }),
100
- brandOverride: (0, tagUtils_1.handleVersionSubstitution)({
101
- url: urls.brandOverride,
102
- wildcardKeyword: '$brandVersion',
103
- localVersion: brandVersion,
104
- }),
105
- };
106
- }
107
- themeVariantsCss[themeVariant] = themeVariantMetadata;
108
- });
109
- return {
110
- core: coreCss,
111
- variants: themeVariantsCss,
112
- defaults: defaultThemeVariants,
113
- };
114
- }