patternlab-node
patternlab-node copied to clipboard
feat(engine-twig): add custom twing extensions
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
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?
- https://github.com/pattern-lab/patternlab-node/blob/dev/packages/docs/src/docs/advanced-config-options.md
- https://github.com/pattern-lab/patternlab-node/blob/dev/packages/engine-twig/README.md
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?
- https://github.com/pattern-lab/patternlab-node/blob/dev/packages/docs/src/docs/advanced-config-options.md
- 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.
@hadl do you want to add the documentation to this PR or in another one?
docs added as @mfranzke suggested