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: reduce build time with cache #406

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/0003-webpack-build-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Webpack Build Configuration

## Summary

We are implementing a caching mechanism in our Webpack configuration to significantly reduce build times.

## Context

Currently, our builds are functioning properly, but any changes made to the MFEs (Micro Frontend Applications) result in the same or even longer build times. Therefore, it is an opportune moment to improve this aspect.

# Decision

We will introduce a caching feature to the `createConfig.js` file in `@edx/frontend-build`. This file is already implemented in most MFEs, making it convenient to enable or disable the feature on a per-MFE basis. Initially, the feature will be implemented in the production environment, but it can also be added to the development environment.


[Webpack cache dock](https://webpack.js.org/configuration/cache/#cachecachedirectory)

# Implementation

To implement this feature, we will utilize the `cache` property in our Webpack configuration. We will use the filesystem type and specify a `cacheDirectory`, which will create a `.cache` directory within each MFE that has the feature enabled. To enable the feature, you need to add a new environment variable to your `.env` file with the following content:

```
ENABLE_WEBPACK_CACHE=''
```

# Follow-on Work

After implementing this feature, remember to add `.cache` to the `.gitignore` file for MFEs that have the caching feature enabled. Additionally, consider extending this feature to the development environment since its scope is currently limited to the production environment.
1 change: 1 addition & 0 deletions lib/ConfigPreset.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function ConfigPreset({
return {
defaultFilename,
getDefault: () => require(require.resolve(`./${defaultFilename}`, { paths: [defaultDir] })),
getMfePath: () => process.cwd(),
get defaultFilepath() {
console.log('getting default filepath', defaultFilename, defaultDir);
return require.resolve(`./${defaultFilename}`, { paths: [defaultDir] });
Expand Down
13 changes: 13 additions & 0 deletions lib/createConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const { merge } = require('webpack-merge');
const path = require('path');
const getBaseConfig = require('./getBaseConfig');
const presets = require('./presets');

module.exports = (commandName, configFragment = {}) => {
const baseConfig = getBaseConfig(commandName);

if (commandName === 'webpack-prod' && process.env.ENABLE_WEBPACK_CACHE !== 'false') {
const cacheFolderPath = presets[commandName].getMfePath();
baseConfig.cache = {
johnvente marked this conversation as resolved.
Show resolved Hide resolved
type: 'filesystem',
cacheDirectory: path.resolve(cacheFolderPath, '.cache'),
johnvente marked this conversation as resolved.
Show resolved Hide resolved
};

return merge(baseConfig, configFragment);
}

return merge(baseConfig, configFragment);
};