Skip to content

Use volto.config.js as dynamic configuration for addons. It adds up… #3008

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

Merged
merged 3 commits into from
Jan 27, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Feature

- Use `volto.config.js` as dynamic configuration for addons. It adds up to the `package.json` `addons` key, allowing dynamic load of addons (eg. via environment variables) @sneridagh

### Bugfix

### Internal
Expand Down
10 changes: 10 additions & 0 deletions __tests__/addon-registry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('AddonConfigurationRegistry', () => {
'test-addon',
'test-released-addon',
'test-released-source-addon',
'my-volto-config-addon',
'test-released-dummy',
'test-released-unmentioned',
]);
Expand Down Expand Up @@ -60,6 +61,13 @@ describe('AddonConfigurationRegistry', () => {
name: 'test-released-unmentioned',
packageJson: `${base}/node_modules/test-released-unmentioned/package.json`,
},
'my-volto-config-addon': {
addons: ['test-released-dummy'],
isPublishedPackage: false,
modulePath: `${base}/addons/my-volto-config-addon/src`,
name: 'my-volto-config-addon',
packageJson: `${base}/addons/my-volto-config-addon/package.json`,
},
'test-released-dummy': {
addons: ['test-released-unmentioned'],
isPublishedPackage: false,
Expand All @@ -74,6 +82,7 @@ describe('AddonConfigurationRegistry', () => {
const base = path.join(__dirname, 'fixtures', 'test-volto-project');
const reg = new AddonConfigurationRegistry(base);
expect(reg.getResolveAliases()).toStrictEqual({
'my-volto-config-addon': `${base}/addons/my-volto-config-addon/src`,
'test-addon': `${base}/addons/test-addon/src`,
'test-released-addon': `${base}/node_modules/test-released-addon`,
'test-released-dummy': `${base}/addons/test-released-dummy`,
Expand All @@ -98,6 +107,7 @@ describe('AddonConfigurationRegistry', () => {
'test-addon',
'test-released-addon',
'test-released-source-addon',
'my-volto-config-addon',
]);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-addon",
"customizationPaths": [
"src/custom-addons"
],
"addons": [
"test-released-dummy"
]
}
3 changes: 2 additions & 1 deletion __tests__/fixtures/test-volto-project/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"paths": {
"test-addon": ["test-addon/src"],
"test-released-dummy": ["test-released-dummy"]
"test-released-dummy": ["test-released-dummy"],
"my-volto-config-addon": ["my-volto-config-addon/src"]
},
"baseUrl": "addons"
}
Expand Down
3 changes: 3 additions & 0 deletions __tests__/fixtures/test-volto-project/volto.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
addons: ['my-volto-config-addon'],
};
17 changes: 15 additions & 2 deletions addon-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,26 @@ class AddonConfigurationRegistry {
projectRootPath,
'package.json',
)));
// Loads the dynamic config, if any
if (fs.existsSync(path.join(projectRootPath, 'volto.config.js'))) {
this.voltoConfigJS = require(path.join(
projectRootPath,
'volto.config.js',
));
} else {
this.voltoConfigJS = [];
}
this.resultantMergedAddons = [
...(packageJson.addons || []),
...(this.voltoConfigJS.addons || []),
];

this.projectRootPath = projectRootPath;
this.voltoPath =
packageJson.name === '@plone/volto'
? `${projectRootPath}`
: `${projectRootPath}/node_modules/@plone/volto`;
this.addonNames = (packageJson.addons || []).map((s) => s.split(':')[0]);
this.addonNames = this.resultantMergedAddons.map((s) => s.split(':')[0]);
this.packages = {};
this.customizations = new Map();

Expand All @@ -114,7 +127,7 @@ class AddonConfigurationRegistry {
this.initTestingPackages();

this.dependencyGraph = buildDependencyGraph(
packageJson.addons || [],
this.resultantMergedAddons,
(name) => {
this.initPublishedPackage(name);
return this.packages[name].addons || [];
Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nav:
- Settings reference guide: 'configuration/settings-reference.md'
- Zero config builds: 'configuration/zero-config-builds.md'
- Internal proxy: 'configuration/internalproxy.md'
- Dynamic Volto Addons Configuration: 'configuration/volto-config-js.md'
- Backend configuration: 'configuration/backend.md'
- Richeditor settings: 'configuration/richeditor-settings.md'
- Multilingual: 'configuration/multilingual.md'
Expand Down
21 changes: 21 additions & 0 deletions docs/source/configuration/volto-config-js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Dynamic Volto Addons Configuration

There are some cases where defining the Volto addons your project is going to use is not enough, and you need more control over it. For example, when you have several builds under the umbrella of the same project that share the core of the code, but each build have special requirements, like other CSS, customizations, shadowing or the features of other addons available.

There is a scapehatch `volto.config.js`. This module exports an object and can have arbitrary code depending on your needs:

```js
let addons = [];
if (process.env.MY_SPECIAL_CUSTOM_BUILD) {
addons = ['volto-custom-addon'];
}

module.exports = {
addons: addons,
};

```

In the case above, we delegate to the presence of an environment variable (MY_SPECIAL_CUSTOM_BUILD) the use of the list of addons specified.

This list, sums up to the one defined in `package.json` (it does not override it), and the addons added are placed at the end of the addons list, so the config in there is applied after the ones in the `package.json`.
2 changes: 1 addition & 1 deletion razzle.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const defaultModify = ({
include.push(fs.realpathSync(`${registry.voltoPath}/src`));
}
// Add babel support external (ie. node_modules npm published packages)
if (packageJson.addons) {
if (registry.addonNames && registry.addonNames.length > 0) {
registry.addonNames.forEach((addon) => {
const p = fs.realpathSync(registry.packages[addon].modulePath);
if (include.indexOf(p) === -1) {
Expand Down
6 changes: 3 additions & 3 deletions volto.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function applyConfig(config) {
return config;
}
module.exports = {
addons: [],
};