-
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 all 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,171 @@ | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| /** | ||
| * Retrieves the name of the brand package from the given directory. | ||
| * | ||
| * @param {string} dir - The directory path containing the package.json file. | ||
| * @return {string} The name of the brand package, or an empty string if not found. | ||
| */ | ||
| function getBrandPackageName(dir) { | ||
| const appDependencies = JSON.parse(fs.readFileSync(path.resolve(dir, 'package.json'), 'utf-8')).dependencies; | ||
| return Object.keys(appDependencies).find((key) => key.match(/@(open)?edx\/brand/)) || ''; | ||
| } | ||
|
|
||
| /** | ||
| * 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 ? getBrandPackageName(dir) : '@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} | ||
| */ | ||
| function getParagonThemeCss(dir, { isBrandOverride = false } = {}) { | ||
| const npmPackageName = isBrandOverride ? getBrandPackageName(dir) : '@openedx/paragon'; | ||
| const pathToParagonThemeOutput = path.resolve(dir, 'node_modules', npmPackageName, 'dist', 'theme-urls.json'); | ||
|
|
||
| if (!fs.existsSync(pathToParagonThemeOutput)) { | ||
| return undefined; | ||
| } | ||
| const paragonConfig = JSON.parse(fs.readFileSync(pathToParagonThemeOutput, 'utf-8')); | ||
| const { | ||
| core: themeCore, | ||
| variants: themeVariants, | ||
| defaults, | ||
| } = paragonConfig?.themeUrls || {}; | ||
|
|
||
|
Comment on lines
+65
to
+70
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. should we validate if the values exist before use them?, I mean just to avoid future errors, something like if !themeCore || !themeVariants return undefined.
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. It's not necessary because we are using destructuring if any of them is not defined that will return undefined by default. |
||
| const pathToCoreCss = path.resolve(dir, 'node_modules', npmPackageName, 'dist', themeCore.paths.minified); | ||
| const coreCssExists = fs.existsSync(pathToCoreCss); | ||
|
|
||
| const themeVariantResults = Object.entries(themeVariants || {}).reduce((themeVariantAcc, [themeVariant, value]) => { | ||
| const themeVariantCssDefault = path.resolve(dir, 'node_modules', npmPackageName, 'dist', value.paths.default); | ||
| const themeVariantCssMinified = path.resolve(dir, 'node_modules', npmPackageName, 'dist', value.paths.minified); | ||
|
|
||
| if (!fs.existsSync(themeVariantCssDefault) && !fs.existsSync(themeVariantCssMinified)) { | ||
| return themeVariantAcc; | ||
| } | ||
|
|
||
| return ({ | ||
| ...themeVariantAcc, | ||
| [themeVariant]: { | ||
| filePath: themeVariantCssMinified, | ||
| entryName: isBrandOverride ? `brand.theme.variants.${themeVariant}` : `paragon.theme.variants.${themeVariant}`, | ||
| outputChunkName: isBrandOverride ? `brand-theme-variants-${themeVariant}` : `paragon-theme-variants-${themeVariant}`, | ||
| }, | ||
| }); | ||
| }, {}); | ||
|
|
||
| if (!coreCssExists || themeVariantResults.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const coreResult = { | ||
| filePath: path.resolve(dir, pathToCoreCss), | ||
| entryName: isBrandOverride ? 'brand.theme.core' : 'paragon.theme.core', | ||
| outputChunkName: isBrandOverride ? 'brand-theme-core' : 'paragon-theme-core', | ||
| }; | ||
|
|
||
| return { | ||
| core: fs.existsSync(pathToCoreCss) ? coreResult : undefined, | ||
| variants: themeVariantResults, | ||
| defaults, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @typedef CacheGroup | ||
| * @property {string} type The type of cache group. | ||
| * @property {string|function} name The name of the cache group. | ||
| * @property {function} chunks A function that returns true if the chunk should be included in the cache group. | ||
| * @property {boolean} enforce If true, this cache group will be created even if it conflicts with default cache groups. | ||
| */ | ||
|
|
||
| /** | ||
| * @param {ParagonThemeCss} paragonThemeCss The Paragon theme CSS metadata. | ||
| * @returns {Object.<string, CacheGroup>} The cache groups for the Paragon theme CSS. | ||
| */ | ||
| function getParagonCacheGroups(paragonThemeCss) { | ||
| if (!paragonThemeCss) { | ||
| return {}; | ||
| } | ||
| const cacheGroups = { | ||
| [paragonThemeCss.core.outputChunkName]: { | ||
| type: 'css/mini-extract', | ||
| name: paragonThemeCss.core.outputChunkName, | ||
| chunks: chunk => chunk.name === paragonThemeCss.core.entryName, | ||
| enforce: true, | ||
| }, | ||
| }; | ||
|
|
||
| Object.values(paragonThemeCss.variants).forEach(({ entryName, outputChunkName }) => { | ||
| cacheGroups[outputChunkName] = { | ||
| type: 'css/mini-extract', | ||
| name: outputChunkName, | ||
| chunks: chunk => chunk.name === entryName, | ||
| enforce: true, | ||
| }; | ||
| }); | ||
| return cacheGroups; | ||
| } | ||
|
|
||
| /** | ||
| * @param {ParagonThemeCss} paragonThemeCss The Paragon theme CSS metadata. | ||
| * @returns {Object.<string, string>} 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" | ||
| * } | ||
| * ``` | ||
| */ | ||
| function getParagonEntryPoints(paragonThemeCss) { | ||
| if (!paragonThemeCss) { | ||
| return {}; | ||
| } | ||
|
|
||
| const entryPoints = { [paragonThemeCss.core.entryName]: path.resolve(process.cwd(), paragonThemeCss.core.filePath) }; | ||
| Object.values(paragonThemeCss.variants).forEach(({ filePath, entryName }) => { | ||
| entryPoints[entryName] = path.resolve(process.cwd(), filePath); | ||
| }); | ||
| return entryPoints; | ||
| } | ||
|
|
||
| module.exports = { | ||
| getParagonVersion, | ||
| getParagonThemeCss, | ||
| getParagonCacheGroups, | ||
| getParagonEntryPoints, | ||
| }; | ||
| 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/@(open)edx/brand/dist/core.min.css", | ||
| * "paragon.theme.variants.light": "/path/to/node_modules/@(open)edx/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.