Skip to content

Commit

Permalink
Merge branch 'master' into ags/2321
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Dec 9, 2023
2 parents 84c6d76 + 5ce301c commit 091b34d
Show file tree
Hide file tree
Showing 18 changed files with 3,850 additions and 1,867 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Nodejs Env
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lockfileversion-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ on:

jobs:
version-check:
uses: openedx/.github/.github/workflows/lockfileversion-check-v3.yml@master
uses: openedx/.github/.github/workflows/lockfile-check.yml@master
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Nodejs Env
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync-master-alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Syncing branches
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v3
with:
Expand Down
99 changes: 88 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
##############
frontend-build
==============
##############

|Build Status| |npm_version| |Codecov| |license|

********
Purpose
********

The purpose of this package is to provide a common sense foundation and
setup for frontend projects including:

Expand All @@ -16,8 +21,29 @@ dev and build dependencies. It aims to provide common sense defaults that
should be good for most edX projects out of the box, but can extended or
overridden where needed.

Cloning and Startup
===================

.. code-block::
1. Clone your new repo:
``git clone https://github.com/openedx/frontend-build.git``
2. Use node v18.x.
The current version of the micro-frontend build scripts support node 18.
Using other major versions of node *may* work, but this is unsupported. For
convenience, this repository includes an .nvmrc file to help in setting the
correct node version via `nvm <https://github.com/nvm-sh/nvm>`_.
3. Install npm dependencies:
``cd frontend-build && npm ci``
Usage
-----
=====

CLI commands are structured: ``fedx-scripts <targetScript> <options>``. Options
are passed on to the target script, so refer to each target script's CLI
Expand All @@ -26,7 +52,7 @@ documentation to learn what options are available. Example package.json::
{
"scripts": {
"build": "fedx-scripts webpack",
"i18n_extract": "BABEL_ENV=i18n fedx-scripts babel src --quiet > /dev/null",
"i18n_extract": "fedx-scripts formatjs extract",
"lint": "fedx-scripts eslint --ext .jsx,.js .",
"precommit": "npm run lint",
"snapshot": "fedx-scripts jest --updateSnapshot",
Expand All @@ -43,7 +69,7 @@ documentation to learn what options are available. Example package.json::
}

Extending or Overriding Config Presets
--------------------------------------
======================================

This package contains a set of configuration presets:

Expand Down Expand Up @@ -104,7 +130,7 @@ location in other configuration files. Please reach out to the FedX team if you
need to do this and are running into problems.

Local module configuration for Webpack
--------------------------------------
======================================

The development webpack configuration allows engineers to create a "module.config.js" file containing local module overrides. This means that if you're developing a new feature in a shared library (@edx/frontend-platform, @edx/paragon, etc.), you can add the local location of that repository to your module.config.js file and the webpack build for your application will automatically pick it up and use it, rather than its node_modules version of the file.

Expand All @@ -131,14 +157,14 @@ An example module.config.js file looks like the following. You can copy this in
};

Steps
~~~~~
=====

#. Copy the ``module.config.js`` into your frontend app repository, modifying it as necessary.
#. Run ``npm install && npm run build`` within any shared NPM package you want to use locally.
#. Restart your app.

Notes
~~~~~
=====

* The "dir" and "dist" keys give you granular control over the shape of your repository's distribution. Paragon, for instance, needs two separate entries to pick up both JS and SCSS imports.
* The directory location ``../src`` (relative to the root of your frontend app repository) is recommended for shared NPM package repositories, since it will work whether or not you are running your frontend via devstack. If you are *not* running your frontend via devstack, then you can place your shared libraries anywhere in your file system, updating the "dir" key accordingly. To learn more, see `this devstack ADR on local packages`_.
Expand All @@ -147,7 +173,7 @@ Notes
.. _this devstack ADR on local packages: https://github.com/openedx/devstack/tree/master/docs/decisions/0005-frontend-package-mounts.rst

Override default .env.development environment variables with .env.private
-------------------------------------------------------------------------
=========================================================================

In some situations, you may want to override development environment variables defined in .env.development
with private environment variables that should never be checked into a repository. For example, a
Expand All @@ -159,7 +185,7 @@ You may create a `.env.private` with any overrides of the environment settings c
**Note: .env.private should be added to your project's .gitignore so it does not get checked in.**

Serving a production Webpack build locally
------------------------------------------
==========================================

In some scenarios, you may want to run a production Webpack build locally. To serve a production build locally:

Expand All @@ -169,7 +195,7 @@ In some scenarios, you may want to run a production Webpack build locally. To se
#. Run ``npm run serve`` to serve your production build assets. It will attempt to run the build on the same port specified in the ``env.config.js`` file.

Development
-----------
===========

This project leverages the command line interface for webpack, jest, eslint, and babel.
Because of this, local development can be tricky. The easiest way to do local
Expand All @@ -185,8 +211,59 @@ or to test with an existing project you can do the following:
3. Install the development version of frontend-build
``npm i --save-dev @edx/frontend-build@file:./frontend-build``.

License
=======

The code in this repository is licensed under the AGPLv3 unless otherwise
noted.

Please see `LICENSE <LICENSE>`_ for details.

Contributing
============

Contributions are very welcome. Please read `How To Contribute`_ for details.

.. _How To Contribute: https://openedx.org/r/how-to-contribute

This project is currently accepting all types of contributions, bug fixes,
security fixes, maintenance work, or new features. However, please make sure
to have a discussion about your new feature idea with the maintainers prior to
beginning development to maximize the chances of your change being accepted.
You can start a conversation by creating a new issue on this repo summarizing
your idea.


Getting Help
===========

If you're having trouble, we have discussion forums at
https://discuss.openedx.org where you can connect with others in the community.

Our real-time conversations are on Slack. You can request a `Slack
invitation`_, then join our `community Slack workspace`_. Because this is a
frontend repository, the best place to discuss it would be in the `#wg-frontend
channel`_.

For anything non-trivial, the best path is to open an issue in this repository
with as many details about the issue you are facing as you can provide.

https://github.com/openedx/frontend-build/issues

For more information about these options, see the `Getting Help`_ page.

.. _Slack invitation: https://openedx.org/slack
.. _community Slack workspace: https://openedx.slack.com/
.. _#wg-frontend channel: https://openedx.slack.com/archives/C04BM6YC7A6
.. _Getting Help: https://openedx.org/community/connect

Reporting Security Issues
=========================

Please do not report security issues in public. Please email [email protected].

Optimization
-----------
============
To increase optimization by reducing unused CSS, you can set ``USE_PURGECSS=true`` in ``.env`` or as ENV var in the corresponding MFE.
However, note that doing this will increase build time by 30%. It's thus not recommended to use this option during development.
On the other hand, enabling PurgeCSS will increase browser performance for the end user by as much as 20% (as measured by `lighthouse`_). Operators are encouraged to do so for production deployments.
Expand Down
12 changes: 12 additions & 0 deletions bin/fedx-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ switch (commandName) {
ensureConfigOption(presets.webpackDevServer);
require('webpack-dev-server/bin/webpack-dev-server');
break;
case 'formatjs': {
const commonArgs = [
'--format', 'node_modules/@edx/frontend-build/lib/formatter.js',
'--ignore', 'src/**/*.json',
'--out-file', './temp/babel-plugin-formatjs/Default.messages.json',
'--', 'src/**/*.js*',
];
process.argv = process.argv.concat(commonArgs);
ensureConfigOption(presets.formatjs);
require('@formatjs/cli/bin/formatjs');
break;
}
case 'serve':
require('../lib/scripts/serve');
break;
Expand Down
6 changes: 1 addition & 5 deletions config/babel-preserve-modules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ module.exports = {
i18n: {
plugins: [
[
'react-intl',
{
messagesDir: './temp/babel-plugin-react-intl',
moduleSourceName: '@edx/frontend-platform/i18n',
},
'formatjs',
],
],
},
Expand Down
6 changes: 1 addition & 5 deletions config/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ module.exports = {
i18n: {
plugins: [
[
'react-intl',
{
messagesDir: './temp/babel-plugin-react-intl',
moduleSourceName: '@edx/frontend-platform/i18n',
},
'formatjs',
],
],
},
Expand Down
5 changes: 1 addition & 4 deletions config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ if (fs.existsSync(appEnvConfigPath)) {
}

module.exports = {
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost/',
},
testURL: 'http://localhost/',
setupFiles: [
path.resolve(__dirname, 'jest/setupTest.js'),
],
Expand Down
18 changes: 18 additions & 0 deletions config/webpack.common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ module.exports = {
new RemoveEmptyScriptsPlugin(),
new ParagonWebpackPlugin(),
],
ignoreWarnings: [
// Ignore warnings raised by source-map-loader.
// some third party packages may ship miss-configured sourcemaps, that interrupts the build
// See: https://github.com/facebook/create-react-app/discussions/11278#discussioncomment-1780169
/**
*
* @param {import('webpack').WebpackError} warning
* @returns {boolean}
*/
function ignoreSourcemapsloaderWarnings(warning) {
return (
warning.module
&& warning.module.resource.includes('node_modules')
&& warning.details
&& warning.details.includes('source-map-loader')
);
},
],
};
2 changes: 1 addition & 1 deletion config/webpack.dev-stage.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = merge(commonConfig, {
// Babel is configured with the .babelrc file at the root of the project.
{
test: /\.(js|jsx)$/,
exclude: /node_modules\/(?!@edx)/,
exclude: /node_modules\/(?!@(open)?edx)/,
use: {
loader: 'babel-loader',
options: {
Expand Down
2 changes: 1 addition & 1 deletion config/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = merge(commonConfig, {
// Babel is configured with the .babelrc file at the root of the project.
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules\/(?!@edx)/,
exclude: /node_modules\/(?!@(open)?edx)/,
use: {
loader: 'babel-loader',
options: {
Expand Down
2 changes: 1 addition & 1 deletion config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = merge(commonConfig, {
// Babel is configured with the .babelrc file at the root of the project.
{
test: /\.(js|jsx)$/,
exclude: /node_modules\/(?!@edx)/,
exclude: /node_modules\/(?!@(open)?edx)/,
use: {
loader: 'babel-loader',
options: {
Expand Down
10 changes: 10 additions & 0 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.format = (messages) => {
const results = Object.entries(messages).map(([id, message]) => (
{
id,
defaultMessage: message.defaultMessage,
description: message.description,
}
));
return results;
};
7 changes: 7 additions & 0 deletions lib/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const babel = new ConfigPreset({
searchFilepaths,
});

const formatjs = new ConfigPreset({
defaultFilename: 'babel.config.js',
searchFilenames: ['.babelrc', '.babelrc.js', 'babel.config.js'],
searchFilepaths,
});

const babelPreserveModules = new ConfigPreset({
defaultFilename: 'babel-preserve-modules.config.js',
searchFilenames: ['.babelrc', '.babelrc.js', 'babel.config.js'],
Expand Down Expand Up @@ -52,6 +58,7 @@ const webpack = new ConfigPreset({

module.exports = {
babel,
formatjs,
babelPreserveModules,
'babel-preserve-modules': babelPreserveModules,
'babel-typescript': babelTypescript,
Expand Down
Loading

0 comments on commit 091b34d

Please sign in to comment.