Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose PARAGON as a global variable #365

Closed
wants to merge 25 commits into from
Closed

Conversation

adamstankiewicz
Copy link
Member

@adamstankiewicz adamstankiewicz commented May 20, 2023

openedx/paragon#2321

In this post, @xitij2000 had a great suggestion to support version substitution on the Paragon theme urls such that if the theme url specified in configuration looks something like, https://cdn.jsdelivr.net/npm/@edx/paragon@$paragonVersion/dist/core.css, we could replace the $paragonVersion with the version of the locally installed Paragon in the application itself.

This approach should help mitigate the concern around the coupling between the Paragon version installed in package.json in consuming MFEs (related to this PR) as, if the theme URLs contain $paragonVersion, consumers won't need to worry about updating Paragon versions in two places, or using an incompatible version of the Paragon theme between the version specified in configuration and the locally installed Paragon version.

Output

If there is no compatible version of @edx/paragon installed (i.e., does not include a paragon-theme.json file in its dist directory in node_modules), falls back to create an undefined global variable, which prevents consuming MFE runtimes from breaking due to usage of an undefined PARAGON global variable:

const PARAGON_THEME = undefined;

Consuming libraries (e.g., @edx/frontend-platform) should not expect PARAGON_THEME to always be defined.

Development build (npm start)

image

Production build (npm run build)

Note: includes the [contentHash] due to options in webpack.config.prod.js. When the Paragon version changes, we will invalidate the cached file paragon-theme-{url}.{hash}.js due to the {hash} changing when the files themselves change.

image

@adamstankiewicz adamstankiewicz changed the title feat: expose PARAGON_VERSION as a global variable feat: expose PARAGON as a global variable May 27, 2023
@adamstankiewicz adamstankiewicz force-pushed the ags/2321 branch 2 times, most recently from 7ec92fc to 56a285e Compare May 28, 2023 15:54
example/package.json Outdated Show resolved Hide resolved
@@ -19,4 +30,16 @@ module.exports = {
},
extensions: ['.js', '.jsx'],
},
optimization: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the various chunks: ['app'] below, and this optimization block here too. What's the net effect of how we've changed the chunking? What do the cacheGroups do for us?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put another way, it may be worth a comment in the config here describing what we're doing to the optimization and how this differs from the default (I think you're leaving the chunking the same and just adding this caching thing, but that's only cause I've stared at a lot of these in my day 😉)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the chunking is staying the same as is it today, with the addition of splitting out the "locally" installed already-compiled CSS files from @edx/paragon and @edx/brand. I will add a comment :)

That said, we probably should find some ways to optimize our MFE Webpack bundles (e.g., test hypothesis that many small JS files for the node_modules is better than 1 larger JS file, recommend/implement a strategy for code splitting for MFEs): openedx/wg-frontend#173

ghassanmas added a commit to ghassanmas/frontend-app-learning that referenced this pull request Oct 6, 2023
  This PR purpose is to test/demo parago design tokens simliar
  to this one for the profile openedx/frontend-app-profile/pull/764

  it override the following depns as seen in package.json

  - paragon alpha
  - openedx/frontend-build/pull/365
  - openedx/frontend-platform/pull/440
  - openedx/frontend-component-header/pull/351
  - openedx/frontend-component-footer/pull/303

 Conclousion so far:

 - There is an extra library that learning depends on which
  needs to support paragon; `frontend-lib-learning-assistant`
  and also `frontend-lib-special-exams`

 - It would be great to have a gudie or a document to look at,
 while doing the conversion that would **map variable from
 who it was used/named before to the name in design tokens**

 - I was stuck in the end with compliation error, that
 wepack couldn't find `Modal` exported from paragon.
@Mashal-m
Copy link
Contributor

Hey @adamstankiewicz, What is the current status of this PR, is it ready to review and merge?
Could you please resolve conflicts?

if (fs.existsSync(envConfigFile)) {
envConfig = require(envConfigFile);
}
// const paragonThemeUrls = mfeRuntimeApiConfig?.PARAGON_THEME_URLS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I would suggest removing these comments if they are not intended to be used in the future.

Comment on lines +14 to +15
const response = await axios.get(url);
return response.data;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we consider incorporating a try/catch block for this operation? It would be beneficial to anticipate and handle potential failures in the request.

// Add process env vars. Currently used only for setting the
// server port and the publicPath
dotenv.config({
path: path.resolve(process.cwd(), '.env.development'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes that the build(npm run build) process uses env.development setttings instead of the env

@arbrandes arbrandes self-requested a review March 6, 2024 16:39
example/env.config.js Outdated Show resolved Hide resolved
@arbrandes
Copy link
Contributor

I'd like to put this (and openedx/frontend-platform#440) back on our radar, now that Redwood is cut and OEP-65 has been merged. @davidjoy, interested in your take in particular: do you feel this (external CSS stylesheets) is something we should push before, in parallel, as part of, or after OEP-65?

@davidjoy
Copy link
Contributor

@adamstankiewicz may have a more comprehensive view of this than I do, as I'm trying to page this paragon work back in to my brain. But my sense of it (including openedx/frontend-platform#440) is that:

  1. Sharing a stylesheet between MFEs becomes our default behavior once we have a 'shell' app which loads all the other MFEs, so nothing special will be necessary for that.
  2. Dark mode / themes is still something we want, though we don't actually have a 'dark' theme yet, right? So this is about groundwork for that, which may be easier to reason when the dust starts settling on OEP-65.

@adamstankiewicz
Copy link
Member Author

  1. Sharing a stylesheet between MFEs becomes our default behavior once we have a 'shell' app which loads all the other MFEs, so nothing special will be necessary for that.

@davidjoy When you say "nothing special will be necessary" for sharing a stylesheet between MFEs as it will become the default behavior once we have a "shell" app, are you referring to the changes in the openedx/frontend-platform#440 no longer being necessary? That is, the frontend-platform PR enables support for using the Paragon CSS from a CDN URL instead of importing directly from the @openedx/paragon NPM package (i.e., better supports multi-tenancy use cases). If we want to continue relying on external CDN URLs (versus importing from node_modules), then I imagine there would work to be done in the shell to embed the Paragon CSS CDN url.

Dark mode / themes is still something we want, though we don't actually have a 'dark' theme yet, right? So this is about groundwork for that, which may be easier to reason when the dust starts settling on OEP-65.

Correct, there is no official dark mode (yet). The design tokens work in Paragon sets up the foundational ground work to support a dark mode in the future (and maybe a high-contrast mode even longer term) and the frontend-build/platform PRs try to support those future use cases. The actual theme switching capabilities is not a strict requirement for the design tokens initial release, though, but would like to ensure the technical design for consuming the updated Paragon CSS from design tokens can be readily extended to support themes in the future.

do you feel this (external CSS stylesheets) is something we should push before, in parallel, as part of, or after OEP-65?

@arbrandes @davidjoy Supporting the external CSS stylesheets (i.e., loading Paragon's CSS via CDN url vs. importing from node_modules) proposed by this PR and the associated frontend-platform PR is probably not as relevant to OEP-65. In the short term, the "shell" app could still rely on importing the Paragon CSS via node_modules; however, I believe Open Craft / eduNext / etc. does desire the ability to load the Paragon CSS from external CDN URL for multi-tenancy support (i.e., not requiring apps to rebuild & redeploy whenever the theme changes).

Likely the biggest overlap with OEP-65 and Paragon design tokens is not so much whether we adopt the external CSS stylesheets proposed by these 2 PRs, but more so Paragon migrating from SCSS -> CSS variables. That is, specifically, around the current styles challenge where custom SCSS files in MFEs using Paragon's SCSS variables need to either be imported by the MFE's styles entry point (e.g., src/index.scss) or the custom SCSS files would need to re-import Paragon styles (risking duplicate Paragon styles).

This current challenge around needing to import Paragon SCSS variables before using the variables in custom SCSS files will be a non-issue after the SCSS -> CSS migration as part of Paragon design tokens. That is, custom SCSS will be able to reference Paragon's CSS variables without needing to import anything from Paragon into the custom SCSS file(s) first.

When a "guest" MFE has custom styles relying on Paragon's variables, but Paragon's CSS is provided by the "shell", we would want to be sure we do not risk introducing duplicate Paragon styles, as some MFEs are doing today. As it relates to OEP-65 and its shared CSS in the "shell", we could probably start by importing Paragon's CSS from node_modules in the "shell" but may likely need the CSS variables support from design tokens to prevent duplicate Paragon CSS introduced by "guest" apps due to the existing SCSS import issue described above.

Hope this helps!

@davidjoy
Copy link
Contributor

Thanks Adam!

When you say "nothing special will be necessary" for sharing a stylesheet between MFEs as it will become the default behavior once we have a "shell" app, are you referring to the changes in the openedx/frontend-platform#440 no longer being necessary?

I'm just saying that if the shell loads the brand and paragon CSS, then all the other MFEs loaded into it don't need to... assuming they want a compatible version. This was under the assumption that the external CDN URL was only about downloading the stylesheet once; sounds like there's more to it than that.

@adamstankiewicz
Copy link
Member Author

adamstankiewicz commented Aug 30, 2024

Closed in favor of #546, which picked up where this PR left off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants