Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(engine-twig): add custom twing extensions #1435

Merged

Conversation

hadl
Copy link
Contributor

@hadl hadl commented Apr 10, 2022

Closes: #1230

Summary of changes:

  • patternlab-config.json new config key possible engines -> twig -> loadExtensionsFile
  • create JS file in PatternLab root directory (e.g twingExtensions.js) and "loadExtensionsFile": "twingExtensions.js"
  • this JS file must export a Map for TwingEnvironment.addExtensions(extensions: Map<string, TwingExtensionInterface>)
  • this Map will be added to the TwingEnvironment on startup

Example

// twingExtensions.js
const { TwingExtension, TwingFunction } = require('twing');

const extensionsMap = new Map();

class TestTwingExtension extends TwingExtension {
  getFunctions() {
    return [
      new TwingFunction('foobar', function (foo) {
        return Promise.resolve(`function foobar called with param "${foo}"`);
      }),
    ];
  }
}
extensionsMap.set('TestTwingExtension', new TestTwingExtension());

module.exports = extensionsMap;

See https://nightlycommit.github.io/twing/advanced.html#creating-an-extension for more details on how to create extensions

@mfranzke mfranzke requested a review from ringods April 13, 2022 19:25
@JosefBredereck
Copy link
Contributor

That is an exciting extension of the pattern lab workspace. I want to request additional documentation for the parameter and what the extension file must contain.

@mfranzke, what do you think? Where is the best place to put this documentation?

  1. https://github.com/pattern-lab/patternlab-node/blob/dev/packages/docs/src/docs/advanced-config-options.md
  2. https://github.com/pattern-lab/patternlab-node/blob/dev/packages/engine-twig/README.md

@mfranzke
Copy link
Contributor

That is an exciting extension of the pattern lab workspace. I want to request additional documentation for the parameter and what the extension file must contain.

@mfranzke, what do you think? Where is the best place to put this documentation?

  1. https://github.com/pattern-lab/patternlab-node/blob/dev/packages/docs/src/docs/advanced-config-options.md
  2. https://github.com/pattern-lab/patternlab-node/blob/dev/packages/engine-twig/README.md

@JosefBredereck Valid question. I would say that it's important to mention engines -> twig -> loadExtensionsFile within the advanced config options documentation, to keep a general overview in there regarding the configurations. But put the rest into https://github.com/pattern-lab/patternlab-node/blob/dev/packages/engine-twig/README.md to keep it all in one place there regarding "installation" itself most likely with a hyperlink to advanced config options documentation as well.

@JosefBredereck
Copy link
Contributor

@hadl do you want to add the documentation to this PR or in another one?

@hadl
Copy link
Contributor Author

hadl commented Sep 25, 2022

docs added as @mfranzke suggested

@JosefBredereck JosefBredereck merged commit c32a45c into pattern-lab:dev Sep 25, 2022
@hadl hadl deleted the feature/1230-add-custom-twing-extensions branch January 15, 2023 16:13
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding custom TwingExtension functions and filters in engine-twig.
3 participants