-
Notifications
You must be signed in to change notification settings - Fork 39
feat: add ParagonWebpackPlugin to support design tokens
#546
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
Changes from 17 commits
452b3e8
eb4fe12
0e3fc56
2e8ee7d
5fa8aff
f48437b
e98a4fb
12007ca
2bf31f8
9343528
afe8880
3d2f0a1
d173b8e
b7fcfe5
73a83f4
dfdcbdb
b1f8364
4ac4d42
347e820
c019cc9
182100f
516c1bf
27d0f75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,160 @@ | ||||
| const path = require('path'); | ||||
| const fs = require('fs'); | ||||
|
|
||||
| /** | ||||
| * Attempts to extract the Paragon version from the `node_modules` of | ||||
| * the consuming application. | ||||
| * | ||||
| * @param {string} dir Path to directory containing `node_modules`. | ||||
| * @returns {string} Paragon dependency version of the consuming application | ||||
| */ | ||||
| function getParagonVersion(dir, { isBrandOverride = false } = {}) { | ||||
| const npmPackageName = isBrandOverride ? '@openedx/brand' : '@openedx/paragon'; | ||||
|
||||
| const pathToPackageJson = `${dir}/node_modules/${npmPackageName}/package.json`; | ||||
| if (!fs.existsSync(pathToPackageJson)) { | ||||
| return undefined; | ||||
| } | ||||
| return JSON.parse(fs.readFileSync(pathToPackageJson, 'utf-8')).version; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * @typedef {Object} ParagonThemeCssAsset | ||||
| * @property {string} filePath | ||||
| * @property {string} entryName | ||||
| * @property {string} outputChunkName | ||||
| */ | ||||
|
Comment on lines
+31
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [unrelated nit, no action needed] now that frontend-build natively supports TypeScript, we might want to consider migrating these JSDoc type definitions to TypeScript. It would help improve the long-term maintainability of these code paths. I filed a follow-up GitHub issue (#581) to backlog the task to migrate these JSDOc type definitions to TypeScript for future consideration/prioritization. |
||||
|
|
||||
| /** | ||||
| * @typedef {Object} ParagonThemeVariantCssAsset | ||||
| * @property {string} filePath | ||||
| * @property {string} entryName | ||||
| * @property {string} outputChunkName | ||||
| */ | ||||
|
|
||||
| /** | ||||
| * @typedef {Object} ParagonThemeCss | ||||
| * @property {ParagonThemeCssAsset} core The metadata about the core Paragon theme CSS | ||||
| * @property {Object.<string, ParagonThemeVariantCssAsset>} variants A collection of theme variants. | ||||
| */ | ||||
|
|
||||
| /** | ||||
| * Attempts to extract the Paragon theme CSS from the locally installed `@openedx/paragon` package. | ||||
| * @param {string} dir Path to directory containing `node_modules`. | ||||
| * @param {boolean} isBrandOverride | ||||
| * @returns {ParagonThemeCss} | ||||
| */ | ||||
dcoa marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| function getParagonThemeCss(dir, { isBrandOverride = false } = {}) { | ||||
| const npmPackageName = isBrandOverride ? '@openedx/brand' : '@openedx/paragon'; | ||||
|
||||
| { moduleName: '@openedx/brand', dir: '../src/brand-openedx' }, // replace with your brand checkout |
I consider that we could be more explicit about this change in the MFE Migration Guide instead. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even the current readme for the repository makes reference to @openedx/brand instead of @edx/brand.
To expand on @brian-smith-tcril's response below regarding keeping parity with the existing @edx/brand scope used today given the non-breaking nature of this release, I believe the README prematurely referenced @openedx/brand as part of this PR given that migration of NPM scope for the brand packages has not yet been prioritized/executed. Looks like the brand package was incorrectly grouped with the scope migrations for other NPM packages (i.e., @openedx/paragon and @openedx/frontend-build).
By having the current README recommend @openedx/brand in the module.config.js within the README would never be resolved as the alias since @openedx/brand is not used in consuming apps, so it's currently incorrect documentation.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,35 @@ | ||
| const path = require('path'); | ||
| const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts'); | ||
|
|
||
| const ParagonWebpackPlugin = require('../lib/plugins/paragon-webpack-plugin/ParagonWebpackPlugin'); | ||
| const { | ||
| getParagonThemeCss, | ||
| getParagonCacheGroups, | ||
| getParagonEntryPoints, | ||
| } = require('./data/paragonUtils'); | ||
|
|
||
| const paragonThemeCss = getParagonThemeCss(process.cwd()); | ||
| const brandThemeCss = getParagonThemeCss(process.cwd(), { isBrandOverride: true }); | ||
|
|
||
| module.exports = { | ||
| entry: { | ||
| app: path.resolve(process.cwd(), './src/index'), | ||
| /** | ||
| * The entry points for the Paragon theme CSS. Example: ``` | ||
| * { | ||
| * "paragon.theme.core": "/path/to/node_modules/@openedx/paragon/dist/core.min.css", | ||
| * "paragon.theme.variants.light": "/path/to/node_modules/@openedx/paragon/dist/light.min.css" | ||
| * } | ||
| */ | ||
| ...getParagonEntryPoints(paragonThemeCss), | ||
| /** | ||
| * The entry points for the brand theme CSS. Example: ``` | ||
| * { | ||
| * "paragon.theme.core": "/path/to/node_modules/@openedx/brand/dist/core.min.css", | ||
|
||
| * "paragon.theme.variants.light": "/path/to/node_modules/@openedx/brand/dist/light.min.css" | ||
|
||
| * } | ||
| */ | ||
| ...getParagonEntryPoints(brandThemeCss), | ||
| }, | ||
| output: { | ||
| path: path.resolve(process.cwd(), './dist'), | ||
|
|
@@ -19,6 +46,23 @@ module.exports = { | |
| }, | ||
| extensions: ['.js', '.jsx', '.ts', '.tsx'], | ||
| }, | ||
| optimization: { | ||
| splitChunks: { | ||
| chunks: 'all', | ||
| cacheGroups: { | ||
| ...getParagonCacheGroups(paragonThemeCss), | ||
| ...getParagonCacheGroups(brandThemeCss), | ||
| }, | ||
| }, | ||
| }, | ||
| plugins: [ | ||
| // RemoveEmptyScriptsPlugin get rid of empty scripts generated by webpack when using mini-css-extract-plugin | ||
| // This helps to clean up the final bundle application | ||
| // See: https://www.npmjs.com/package/webpack-remove-empty-scripts#usage-with-mini-css-extract-plugin | ||
|
|
||
| new RemoveEmptyScriptsPlugin(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be nice to add a comment somewhere explaining why this plugin is being used. The readme on npm says
I assume that is why this is being used, but it'd be nice to confirm/document that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @brian-smith-tcril, thanks for your review, I addressed this comment and rebased the branch. |
||
| new ParagonWebpackPlugin(), | ||
| ], | ||
| ignoreWarnings: [ | ||
| // Ignore warnings raised by source-map-loader. | ||
| // some third party packages may ship miss-configured sourcemaps, that interrupts the build | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.