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: add ParagonWebpackPlugin to support design tokens #546

Merged
merged 23 commits into from
Aug 13, 2024

Conversation

dcoa
Copy link
Contributor

@dcoa dcoa commented May 17, 2024

Description:

This PR updates the original one #365 closer to the master branch and adds some extra tests.

Please read the original PR for additional context.

Changes

  1. Delete Runtime Configuration reference: My reason for making this change is the nature of runtime configuration, which can override any configuration defined previously. The plugin runs during the build time and we have env.config.js that can set the variables. (let me know if my reasoning is correct)
  2. Update @edx/paragon to @openedx/paragon and add compatibility for either @edx/brand or @openedx/brand

Finally, I would like to split this PR in 2 one for the main implementation and another one for the example app (due to this one adds frontend-platform and paragon)

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label May 17, 2024
@openedx-webhooks
Copy link

openedx-webhooks commented May 17, 2024

Thanks for the pull request, @dcoa!

What's next?

Please work through the following steps to get your changes ready for engineering review:

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.

🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads

🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

🔘 Let us know that your PR is ready for review:

Who will review my changes?

This repository is currently maintained by @openedx/committers-frontend-build. Tag them in a comment and let them know that your changes are ready for review.

Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@dcoa dcoa changed the title Dcoa/design tokens support feat: expose PARAGON as a global variable May 17, 2024
@dcoa dcoa force-pushed the dcoa/design-tokens-support branch 2 times, most recently from 86312c3 to 9c1a27b Compare May 17, 2024 09:05
@dcoa dcoa marked this pull request as ready for review May 17, 2024 09:55
@itsjeyd itsjeyd added the core contributor PR author is a Core Contributor (who may or may not have write access to this repo). label May 23, 2024
@itsjeyd
Copy link

itsjeyd commented May 23, 2024

Hey @dcoa, thank you for this contribution! Please let us know when the changes are ready for review.

Copy link
Contributor

@PKulkoRaccoonGang PKulkoRaccoonGang left a comment

Choose a reason for hiding this comment

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

@dcoa Thank you for re-opening and improving this PR! Left a few suggestions

@@ -0,0 +1,373 @@
const { sources } = require('webpack');
Copy link
Contributor

Choose a reason for hiding this comment

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

[proposal]: This file is quite large. I suggest dividing it into several files and creating a folder with utilities. I think it would be a good idea to follow this structure:

./utils

htmlUtils.js

  • getDescendantByTag
  • findScriptInsertionPoint
  • findStylesheetInsertionPoint

scriptUtils.js

  • minifyScript
  • insertScriptContentsIntoDocument

stylesheetUtils.js

  • insertStylesheetsIntoDocument

assetUtils.js

  • findCoreCssAsset
  • findThemeVariantCssAssets
  • getCssAssetsFromCompilation

scriptContentUtils.js

  • addToScriptContents
  • generateScriptContents

urlUtils.js

  • handleVersionSubstitution
  • getParagonStylesheetUrls

Copy link
Contributor Author

@dcoa dcoa May 29, 2024

Choose a reason for hiding this comment

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

I agree and I did, not the same order but let me know what you think :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you! This looks great 💯

config/data/paragonUtils.js Show resolved Hide resolved
lib/plugins/paragon-webpack-plugin/utils.js Outdated Show resolved Hide resolved
if (!fs.existsSync(pathToPackageJson)) {
return undefined;
}
return JSON.parse(fs.readFileSync(pathToPackageJson)).version;
Copy link

Choose a reason for hiding this comment

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

should we add the second argument to use utf8?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is optional, however, after reading some posts about it I think it's a good suggestion. 👍

Comment on lines +54 to +59
const {
core: themeCore,
variants: themeVariants,
defaults,
} = paragonConfig?.themeUrls || {};

Copy link

Choose a reason for hiding this comment

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

should we validate if the values exist before use them?, I mean just to avoid future errors, something like if !themeCore || !themeVariants return undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not necessary because we are using destructuring if any of them is not defined that will return undefined by default.

Comment on lines 78 to 85
const themeVariantResults = {};
validThemeVariantPaths.forEach(([themeVariant, value]) => {
themeVariantResults[themeVariant] = {
filePath: path.resolve(dir, 'node_modules', npmPackageName, 'dist', value.paths.minified),
entryName: isBrandOverride ? `brand.theme.variants.${themeVariant}` : `paragon.theme.variants.${themeVariant}`,
outputChunkName: isBrandOverride ? `brand-theme-variants-${themeVariant}` : `paragon-theme-variants-${themeVariant}`,
};
});
Copy link

Choose a reason for hiding this comment

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

I think we could use .reduce instead of filtering and iterate this, it might be more efficient but I think this is an easier way to read the code.

Should we worry about efficiency?

if (!paragonThemeCss) {
return cacheGroups;
}
cacheGroups[paragonThemeCss.core.outputChunkName] = {
Copy link

Choose a reason for hiding this comment

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

We could remove the line 107 and just define the constant from here, right?

@itsjeyd
Copy link

itsjeyd commented Jul 18, 2024

@dcoa We've got a green build here and it's been a while since the last update. So I'm assuming that the changes are ready for engineering review. Let me know if that's not the case.

@itsjeyd itsjeyd added the waiting for eng review PR is ready for review. Review and merge it, or suggest changes. label Jul 18, 2024
@dcoa
Copy link
Contributor Author

dcoa commented Jul 21, 2024

Hi @itsjeyd yes, it is ready for review and has been tested alongside other design tokens PRs here openedx/frontend-app-discussions#726

@itsjeyd
Copy link

itsjeyd commented Jul 25, 2024

Thanks for confirming @dcoa.

@brian-smith-tcril @adamstankiewicz Would you be able to have a look at this PR?

@dcoa dcoa force-pushed the dcoa/design-tokens-support branch from 2733312 to 347e820 Compare August 2, 2024 04:48
@itsjeyd itsjeyd added waiting on author PR author needs to resolve review requests, answer questions, fix tests, etc. and removed waiting for eng review PR is ready for review. Review and merge it, or suggest changes. labels Aug 2, 2024
@brian-smith-tcril
Copy link
Contributor

@dcoa in https://openedx.slack.com/archives/C071U9E4VNV/p1715966767660459?thread_ts=1715940913.033459&cid=C071U9E4VNV I mentioned

I'd prefer to move over to using @openedx/brand-openedx everywhere but I don't remember all of the gotchas/blockers around that. I think moving forward we should try to use @openedx but if we run into conflicts because of it we can alias back to @edx and note why that was required in any given instance.

When I wrote that I wasn't thinking about landing this PR as a non breaking change. Since the plan now is to land this as a non breaking change I think that fully justifies keeping the @edx namespace for now.

I would still like to make the move to the @openedx namespace, but since doing so is a breaking change I feel it would be better off as a separate PR so as to not delay the merge of this one.

@adamstankiewicz
Copy link
Member

Thanks @brian-smith-tcril. Only thing I'd add regarding the brand package scope (@edx/brand vs. @openedx/brand) is that I think it might make sense to handle either scope so we are compatible with the current state of MFEs' usage with @edx/brand, but also be forward-looking knowing we will migrate to @openedx/brand eventually. Once we're confident consumers/instances have fully migrated to @openedx/brand, we could come back to remove the @edx/brand at that point. Supporting both scopes now would eliminate the need for any coordinated effort in adding support for @openedx/brand down the road.

@dcoa dcoa force-pushed the dcoa/design-tokens-support branch from 05b69a2 to 7c65744 Compare August 4, 2024 10:02
@dcoa dcoa force-pushed the dcoa/design-tokens-support branch from 7c65744 to 182100f Compare August 4, 2024 10:18
@itsjeyd itsjeyd added waiting for eng review PR is ready for review. Review and merge it, or suggest changes. and removed waiting on author PR author needs to resolve review requests, answer questions, fix tests, etc. labels Aug 8, 2024
Comment on lines 46 to 47
* 1. ``envConfig.PARAGON_THEME_URLS`` (JS based)
* 3. ``process.env.PARAGON_THEME_URLS`` (JSON based)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like having PARAGON_THEME_URLS work in an env.config.js file but not an env.config.jsx file is more confusing than just saying PARAGON_THEME_URLS must be set as an environment variable.

I'd love to hear what others think about this!

Copy link
Contributor

Choose a reason for hiding this comment

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

To be clear - this file cannot import or use env.config with any file extension, and we must use process.env to get this value. The env.config file is runtime configuration and is expected to import runtime files that will require transpilation (i.e., jsx/tsx) and depend on their own runtime dependencies. That code can't meaningfully pollute the build-time configuration, and having multiple files named env.config with different extensions would be seriously confusing for folks.

At a later date we may decide to create a new type of build-only configuration file, but we're just not there at the moment. For now, we just need to use process.env either via command line environment variables or the .env files, and it'll come in as a string we need to JSON.parse.

Copy link
Contributor

Choose a reason for hiding this comment

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

Furthermore, worse, would be for build-time code and build-time dependencies to find their way into env.config... that would mean build-time code gets bundled with the app for a browser environment which is a huge no-no and would definitely break.

Copy link
Member

Choose a reason for hiding this comment

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

+1. Agreed, we should only be relying on process.env.PARAGON_THEME_URLS at this point, with JSON.parse for the build-time vs. runtime configuration that @davidjoy is alluding to above (across both this frontend-build PR and within frontend-platform).

Copy link
Contributor

Choose a reason for hiding this comment

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

On the runtime side for frontend-platform's use, by all means copy PARAGON_THEME_URLS into env.config and format it nicely as an actual object instead of a string. But for now it does need to be duplicated, unfortunately, because the two types of config can't mingle.

Copy link
Contributor

Choose a reason for hiding this comment

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

I gave a couple of thumbs up above, but better to be explicit: I agree that for now, process.env.PARAGON_THEME_URLS should be what is used, and only that.

I also like the idea of eventually having a separate build-only config file, thus doing away with any potential for confusion or breakage. (While at the same time finally removing support for the .env files, hopefully.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I also like the idea of eventually having a separate build-only config file, thus doing away with any potential for confusion or breakage. (While at the same time finally removing support for the .env files, hopefully.)

💯

That lines up perfectly with the feelings expressed in the slack thread.

Copy link
Contributor Author

@dcoa dcoa Aug 10, 2024

Choose a reason for hiding this comment

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

Copy link
Member

@adamstankiewicz adamstankiewicz left a comment

Choose a reason for hiding this comment

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

LGTM :shipit: Great job! I also closed the original POC implementation's PR.

Copy link
Contributor

@brian-smith-tcril brian-smith-tcril left a comment

Choose a reason for hiding this comment

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

Thank you so much for this! I know a long review process full of in-depth discussions around architectural decisions isn't always the most fun when trying to land a PR, but this came out great!

LGTM!

@brian-smith-tcril brian-smith-tcril changed the title feat: expose PARAGON as a global variable feat: add ParagonWebpackPlugin to support design tokens Aug 13, 2024
@brian-smith-tcril brian-smith-tcril merged commit 031f51f into openedx:master Aug 13, 2024
5 checks passed
@openedx-webhooks
Copy link

@dcoa 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@openedx-semantic-release-bot

🎉 This PR is included in version 14.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@openedx-semantic-release-bot

🎉 This PR is included in version 15.0.0-alpha.15 🎉

The release is available on:

Your semantic-release bot 📦🚀

@davidjoy
Copy link
Contributor

I had a comment here about using default as a key name, but I think it's actually fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U released on @alpha released
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

10 participants