patternlab-node icon indicating copy to clipboard operation
patternlab-node copied to clipboard

feat(engine-twig): add custom twing extensions

Open hadl opened this issue 2 years ago • 3 comments

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

hadl avatar Apr 10 '22 16:04 hadl

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 avatar Apr 14 '22 07:04 JosefBredereck

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.

mfranzke avatar Apr 23 '22 08:04 mfranzke

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

JosefBredereck avatar Sep 23 '22 17:09 JosefBredereck

docs added as @mfranzke suggested

hadl avatar Sep 25 '22 07:09 hadl