Skip to content

Extendable context references in config files #1055

@mkoe-unitb

Description

@mkoe-unitb

Normal config files with context references looks like:

module.exports = {
    title: 'teaser-list',
    context: {
        items: [
            '@molecules-teaser',
            '@molecules-teaser',
            '@molecules-teaser',
        ]
    }
}

The Problem I have in multiple projects is, that I couldn't extend the referenced object.
This wouldn't work:
const extendedTeaserContext = Object.assign('@molecules-teaser', { newVariable: 'lorem ipsum' })

I have two ideas to solve this problem.

Possible Solution 1: getContext Function
A function to convert a context reference into the object.
const extendedTeaserContext = Object.assign(getContext('@molecules-teaser'), { newVariable: 'lorem ipsum' })

Possible Solution 2: Post Context Generation Hook
A function to adjust the context after all references are resolved.

module.exports = {
    title: 'teaser-list',
    context: {
        items: [
            '@molecules-teaser',
            '@molecules-teaser',
            '@molecules-teaser',
        ]
    },
    postReferenceResolvedHook(context) {
        const extendedItems = [];

        for (const item of context.items) {
            item.newVariable = 'lorem ipsum';
            delete item.globalContextVariable;
            extendedItems.push(item)
        }
        
        context.items = extendedItems;
        return context;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions