|
| 1 | +# frontend-build |
| 2 | + |
| 3 | +[](https://travis-ci.com/edx/frontend-build) |
| 5 | + |
| 6 | +[](https://codecov.io/gh/edx/frontend-build) |
| 7 | +[](https://github.com/edx-unsupported/frontend-base/blob/master/LICENSE) |
| 8 | + |
| 9 | +## Purpose |
| 10 | + |
| 11 | +The purpose of this package is to provide a common sense foundation and |
| 12 | +setup for frontend projects including: |
| 13 | + |
| 14 | +- linting (eslint) |
| 15 | +- testing (jest) |
| 16 | +- development server (webpack-dev-server) |
| 17 | +- build (webpack) |
| 18 | + |
| 19 | +This package can serve as a single dev dependency replacing a large |
| 20 | +number of dev and build dependencies. It aims to provide common sense |
| 21 | +defaults that should be good for most edX projects out of the box, but |
| 22 | +can extended or overridden where needed. |
| 23 | + |
| 24 | +## Cloning and Startup |
| 25 | + |
| 26 | +``` {.} |
| 27 | +1. Clone your new repo: |
| 28 | +
|
| 29 | + ``git clone https://github.com/openedx/frontend-build.git`` |
| 30 | +
|
| 31 | +2. Use node v18.x. |
| 32 | +
|
| 33 | + The current version of the micro-frontend build scripts support node 18. |
| 34 | + Using other major versions of node *may* work, but this is unsupported. For |
| 35 | + convenience, this repository includes an .nvmrc file to help in setting the |
| 36 | + correct node version via `nvm <https://github.com/nvm-sh/nvm>`_. |
| 37 | +
|
| 38 | +3. Install npm dependencies: |
| 39 | +
|
| 40 | + ``cd frontend-build && npm ci`` |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +CLI commands are structured: `fedx-scripts <targetScript> <options>`. |
| 46 | +Options are passed on to the target script, so refer to each target |
| 47 | +script\'s CLI documentation to learn what options are available. Example |
| 48 | +package.json: |
| 49 | + |
| 50 | + { |
| 51 | + "scripts": { |
| 52 | + "build": "fedx-scripts webpack", |
| 53 | + "i18n_extract": "fedx-scripts formatjs extract", |
| 54 | + "lint": "fedx-scripts eslint --ext .jsx,.js .", |
| 55 | + "precommit": "npm run lint", |
| 56 | + "snapshot": "fedx-scripts jest --updateSnapshot", |
| 57 | + "start": "fedx-scripts webpack-dev-server --progress", |
| 58 | + "test": "fedx-scripts jest --coverage --passWithNoTests", |
| 59 | + "serve": "fedx-scripts serve" |
| 60 | + }, |
| 61 | + "dependencies": { |
| 62 | + ... |
| 63 | + }, |
| 64 | + "devDependencies": { |
| 65 | + "@openedx/frontend-build": "1.0.0" |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +## Extending or Overriding Config Presets |
| 70 | + |
| 71 | +This package contains a set of configuration presets: |
| 72 | + |
| 73 | +- webpack-prod (or webpack) |
| 74 | +- webpack-dev (or webpack-dev-server) |
| 75 | +- webpack-dev-stage (for running development apps against stage apis) |
| 76 | +- babel |
| 77 | +- babel-preserve-modules |
| 78 | +- jest |
| 79 | +- eslint |
| 80 | + |
| 81 | +If you need to extend or modify a configuration you can add your own |
| 82 | +configuration files, either by extending frontend-build\'s configuration |
| 83 | +files or supplying your own wholesale. |
| 84 | + |
| 85 | +Method 1: Extend base config (babel.config.js): |
| 86 | + |
| 87 | + const { createConfig } = require('@openedx/frontend-build'); |
| 88 | + module.exports = createConfig('babel', { |
| 89 | + /* option overrides or extensions */ |
| 90 | + }); |
| 91 | + |
| 92 | +Method 2: Custom manipulations (babel.config.js): |
| 93 | + |
| 94 | + const { getBaseConfig } = require('@openedx/frontend-build'); |
| 95 | + const config = getBaseConfig('babel'); |
| 96 | + |
| 97 | + /* Custom config manipulations */ |
| 98 | + |
| 99 | + module.exports = config; |
| 100 | + |
| 101 | +Frontend build will look in the following locations for configuration |
| 102 | +files in your project. |
| 103 | + |
| 104 | +- eslint: `<project_root>/.eslintrc.js` |
| 105 | +- jest: `<project_root>/jest.config.js` |
| 106 | +- babel: `<project_root>/babel.config.js` |
| 107 | +- webpack-prod: `<project_root>/webpack.prod.config.js` |
| 108 | +- webpack-dev-server: `<project_root>/webpack.dev.config.js` |
| 109 | + |
| 110 | +You may specify custom config file locations via the command line if you |
| 111 | +prefer a different location. Example package.json: |
| 112 | + |
| 113 | + { |
| 114 | + "scripts": { |
| 115 | + "build": "fedx-scripts webpack --config ./config/webpack.config.js", |
| 116 | + "start:stage": "fedx-scripts webpack-dev-server --config webpack.dev-stage.config.js", |
| 117 | + ... |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +Note, specifying a custom config location for babel may cause issues |
| 122 | +with other tools in frontend-build. eslint, jest, webpack, and |
| 123 | +webpack-dev-server configuration presets rely upon the babel config and |
| 124 | +resolve the location of the config file according to the default |
| 125 | +locations described above. If you need to move the babel config file to |
| 126 | +a custom location, you may also need to customize references to its |
| 127 | +location in other configuration files. Please reach out to the FedX team |
| 128 | +if you need to do this and are running into problems. |
| 129 | + |
| 130 | +## Local module configuration for Webpack |
| 131 | + |
| 132 | + |
| 133 | +The development webpack configuration allows engineers to create a |
| 134 | +\"module.config.js\" file containing local module overrides. This means |
| 135 | +that if you\'re developing a new feature in a shared library |
| 136 | +(\@edx/frontend-platform, \@openedx/paragon, etc.), you can add the local |
| 137 | +location of that repository to your module.config.js file and the |
| 138 | +webpack build for your application will automatically pick it up and use |
| 139 | +it, rather than its node\_modules version of the file. |
| 140 | + |
| 141 | +**NOTE: This module.config.js file should be added to your** |
| 142 | +[.gitignore]{.title-ref}. |
| 143 | + |
| 144 | +An example module.config.js file looks like the following. You can copy |
| 145 | +this into your application to use local versions of paragon and |
| 146 | +frontend-platform: |
| 147 | + |
| 148 | + module.exports = { |
| 149 | + /* |
| 150 | + Modules you want to use from local source code. Adding a module here means that when this app |
| 151 | + runs its build, it'll resolve the source from peer directories of this app. |
| 152 | + |
| 153 | + moduleName: the name you use to import code from the module. |
| 154 | + dir: The relative path to the module's source code. |
| 155 | + dist: The sub-directory of the source code where it puts its build artifact. Often "dist". |
| 156 | + */ |
| 157 | + localModules: [ |
| 158 | + { moduleName: '@openedx/brand', dir: '../src/brand-openedx' }, // replace with your brand checkout |
| 159 | + { moduleName: '@openedx/paragon/scss/core', dir: '../src/paragon', dist: 'scss/core' }, |
| 160 | + { moduleName: '@openedx/paragon/icons', dir: '../src/paragon', dist: 'icons' }, |
| 161 | + { moduleName: '@openedx/paragon', dir: '../src/paragon', dist: 'dist' }, |
| 162 | + { moduleName: '@edx/frontend-platform', dir: '../src/frontend-platform', dist: 'dist' }, |
| 163 | + ], |
| 164 | + }; |
| 165 | + |
| 166 | +## Steps |
| 167 | + |
| 168 | +1. Copy the `module.config.js` into your frontend app repository, |
| 169 | + modifying it as necessary. |
| 170 | +2. Run `npm install && npm run build` within any shared NPM package you |
| 171 | + want to use locally. |
| 172 | +3. Restart your app. |
| 173 | + |
| 174 | +## Notes |
| 175 | + |
| 176 | +- The \"dir\" and \"dist\" keys give you granular control over the |
| 177 | + shape of your repository\'s distribution. Paragon, for instance, |
| 178 | + needs two separate entries to pick up both JS and SCSS imports. |
| 179 | +- The directory location `../src` (relative to the root of your |
| 180 | + frontend app repository) is recommended for shared NPM package |
| 181 | + repositories, since it will work whether or not you are running your |
| 182 | + frontend via devstack. If you are *not* running your frontend via |
| 183 | + devstack, then you can place your shared libraries anywhere in your |
| 184 | + file system, updating the \"dir\" key accordingly. To learn more, |
| 185 | + see [this devstack ADR on local |
| 186 | + packages](https://github.com/openedx/devstack/tree/master/docs/decisions/0005-frontend-package-mounts.rst). |
| 187 | +- This mechanism uses Webpack resolve aliases, as documented here: |
| 188 | + <https://webpack.js.org/configuration/resolve/#resolvealias> |
| 189 | + |
| 190 | +## Override default .env.development environment variables with .env.private |
| 191 | + |
| 192 | +In some situations, you may want to override development environment |
| 193 | +variables defined in .env.development with private environment variables |
| 194 | +that should never be checked into a repository. For example, a |
| 195 | +.env.development file may contain secrets for a third-party service |
| 196 | +(e.g., Algolia) that you\'d like to use during development but want to |
| 197 | +ensure these secrets are not checked into Git. |
| 198 | + |
| 199 | +You may create a [.env.private]{.title-ref} with any overrides of the |
| 200 | +environment settings configured in [.env.development]{.title-ref}. |
| 201 | + |
| 202 | +**Note: .env.private should be added to your project\'s .gitignore so it |
| 203 | +does not get checked in.** |
| 204 | + |
| 205 | +## Serving a production Webpack build locally |
| 206 | + |
| 207 | +In some scenarios, you may want to run a production Webpack build |
| 208 | +locally. To serve a production build locally: |
| 209 | + |
| 210 | +1. Create an `env.config.js` file containing the configuration for |
| 211 | + local development, with the exception of `NODE_ENV='production'`. |
| 212 | +2. Run `npm run build` to build the production assets. The output |
| 213 | + assets will rely on the local development configuration specified in |
| 214 | + the prior step. |
| 215 | +3. Add an NPM script `serve` to your application\'s `package.json` |
| 216 | + (i.e., `"serve": "fedx-scripts serve"`). |
| 217 | +4. Run `npm run serve` to serve your production build assets. It will |
| 218 | + attempt to run the build on the same port specified in the |
| 219 | + `env.config.js` file. |
| 220 | + |
| 221 | +## Local module configuration for TypeScript |
| 222 | + |
| 223 | +#. Create file in repository `tsconfig.json`, with a clause `"extends": "@openedx/frontend-build"` |
| 224 | +#. Set "rootDir" to the root of the source code folders |
| 225 | +#. Set "include" to wildcard patterns specifying the subdirectories/files under rootDir where source code can be found |
| 226 | +#. Include any wildcards under rootDir that should be excluded using "exclude" |
| 227 | + |
| 228 | +```Sample json |
| 229 | +{ |
| 230 | + "extends": "@openedx/frontend-build", |
| 231 | + "compilerOptions": { |
| 232 | + "rootDir": ".", |
| 233 | + "outDir": "dist" |
| 234 | + }, |
| 235 | + "include": ["src/**/*"], |
| 236 | + "exclude": ["dist", "node_modules"] |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +## Development |
| 241 | + |
| 242 | +This project leverages the command line interface for webpack, jest, |
| 243 | +eslint, and babel. Because of this, local development can be tricky. The |
| 244 | +easiest way to do local development on this project is to either run |
| 245 | +scripts inside the project in example or to test with an existing |
| 246 | +project you can do the following: |
| 247 | + |
| 248 | +1. Delete the node\_modules directories in the host project: |
| 249 | + `rm -rf node_modules/` |
| 250 | +2. Move frontend-build inside the host project and delete its node |
| 251 | + modules folder |
| 252 | + `mv ../frontend-build ./ && rm -rf frontend-build/node_modules` |
| 253 | +3. Install the development version of frontend-build |
| 254 | + `npm i --save-dev @openedx/frontend-build@file:./frontend-build`. |
| 255 | + |
| 256 | +## License |
| 257 | + |
| 258 | +The code in this repository is licensed under the AGPLv3 unless |
| 259 | +otherwise noted. |
| 260 | + |
| 261 | +Please see [LICENSE](LICENSE) for details. |
| 262 | + |
| 263 | +## Contributing |
| 264 | + |
| 265 | +Contributions are very welcome. Please read [How To |
| 266 | +Contribute](https://openedx.org/r/how-to-contribute) for details. |
| 267 | + |
| 268 | +This project is currently accepting all types of contributions, bug |
| 269 | +fixes, security fixes, maintenance work, or new features. However, |
| 270 | +please make sure to have a discussion about your new feature idea with |
| 271 | +the maintainers prior to beginning development to maximize the chances |
| 272 | +of your change being accepted. You can start a conversation by creating |
| 273 | +a new issue on this repo summarizing your idea. |
| 274 | + |
| 275 | +## Getting Help |
| 276 | + |
| 277 | +If you\'re having trouble, we have discussion forums at |
| 278 | +<https://discuss.openedx.org> where you can connect with others in the |
| 279 | +community. |
| 280 | + |
| 281 | +Our real-time conversations are on Slack. You can request a [Slack |
| 282 | +invitation](https://openedx.org/slack), then join our [community Slack |
| 283 | +workspace](https://openedx.slack.com/). Because this is a frontend |
| 284 | +repository, the best place to discuss it would be in the [\#wg-frontend |
| 285 | +channel](https://openedx.slack.com/archives/C04BM6YC7A6). |
| 286 | + |
| 287 | +For anything non-trivial, the best path is to open an issue in this |
| 288 | +repository with as many details about the issue you are facing as you |
| 289 | +can provide. |
| 290 | + |
| 291 | +<https://github.com/openedx/frontend-build/issues> |
| 292 | + |
| 293 | +For more information about these options, see the [Getting |
| 294 | +Help](https://openedx.org/community/connect) page. |
| 295 | + |
| 296 | +## Reporting Security Issues |
| 297 | + |
| 298 | +Please do not report security issues in public. Please email |
| 299 | +<security@openedx.org>. |
| 300 | + |
| 301 | +## Optimization |
| 302 | + |
| 303 | +To increase optimization by reducing unused CSS, you can set |
| 304 | +`USE_PURGECSS=true` in `.env` or as ENV var in the corresponding MFE. |
| 305 | +However, note that doing this will increase build time by 30%. It\'s |
| 306 | +thus not recommended to use this option during development. On the other |
| 307 | +hand, enabling PurgeCSS will increase browser performance for the end |
| 308 | +user by as much as 20% (as measured by |
| 309 | +[lighthouse](https://developer.chrome.com/docs/lighthouse/overview/)). |
| 310 | +Operators are encouraged to do so for production deployments. |
| 311 | + |
| 312 | +For more information about optimizing MFEs, refer to the [issue |
| 313 | +\#138](https://github.com/openedx/wg-frontend/issues/138) in the |
| 314 | +wg-frontend repository. |
0 commit comments