Skip to content
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

Pattern Lab only loads the pattern engines from the config #1256

Merged
merged 26 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5d376a5
PatternLab loads the pattern engines from the config first, before fa…
ringods Sep 2, 2020
66955f8
Merge branch 'dev' into feature/resolver-pattern-engines
JosefBredereck Jan 12, 2021
127a38d
Merge branch 'dev' into feature/resolver-pattern-engines
JosefBredereck Feb 19, 2021
3d8e673
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 24, 2022
0646373
chore: wording and headline structure
mfranzke Dec 24, 2022
297d251
chore: wording
mfranzke Dec 24, 2022
092fd18
docs: further clarification through logging
mfranzke Dec 24, 2022
0bec8bd
fix: configuration
mfranzke Dec 24, 2022
a13c265
docs: let's get rid of that old mustache engine
mfranzke Dec 24, 2022
99177b0
refactor: we should only include the outdated mustache engine interal…
mfranzke Dec 24, 2022
2f44441
chore: adapted that entry from the other places
mfranzke Dec 24, 2022
b8014f9
chore: let's not mention mustache any more
mfranzke Dec 24, 2022
b06ff57
docs: wording
mfranzke Dec 24, 2022
1b8f263
docs: added a link
mfranzke Dec 24, 2022
1799481
chore: added relevant comment
mfranzke Dec 24, 2022
d3662c7
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 27, 2022
3c6f556
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 28, 2022
a6d50eb
Update patternlab-config.json
mfranzke Dec 28, 2022
02a6095
Restore packages/core/test/files/_handlebars-test-patterns/atoms/glob…
mfranzke Dec 28, 2022
5e7d1ee
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 28, 2022
a76bbdd
Merge branch 'dev' into feature/resolver-pattern-engines
JosefBredereck Dec 28, 2022
d9ceff1
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 29, 2022
bd52b43
Merge branch 'dev' into feature/resolver-pattern-engines
mfranzke Dec 29, 2022
a9246db
Update advanced-config-options.md
mfranzke Dec 29, 2022
16ab160
Update pattern_engines.js
mfranzke Dec 30, 2022
d328a40
Update packages/docs/src/docs/advanced-config-options.md
JosefBredereck Dec 30, 2022
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
12 changes: 11 additions & 1 deletion packages/cli/test/fixtures/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,15 @@
"color": "dark",
"density": "compact",
"layout": "horizontal"
}
},
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
}
}
10 changes: 10 additions & 0 deletions packages/core/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@
"density": "compact",
"layout": "horizontal"
},
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
},
"uikits": [
{
"name": "uikit-workshop",
Expand Down
115 changes: 91 additions & 24 deletions packages/core/src/lib/pattern_engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ function findEngineModulesInDirectory(dir) {
return foundEngines;
}

function findEnginesInConfig(config) {
if ('engines' in config) {
return config.engines;
}
logger.warning(
"Scanning the 'node_modules' folder for pattern engines is deprecated and will be removed in v6."
);
logger.warning(
'To configure your engines in patternlab-config.json, see https://patternlab.io/docs/editing-the-configuration-options/#heading-engines'
);
return null;
}

//
// PatternEngines: the main export of this module
//
Expand All @@ -86,52 +99,106 @@ const PatternEngines = Object.create({
loadAllEngines: function (patternLabConfig) {
const self = this;

// Try to load engines! We scan for engines at each path specified above. This
// function is kind of a big deal.
enginesDirectories.forEach(function (engineDirectory) {
const enginesInThisDir = findEngineModulesInDirectory(
engineDirectory.path
);
// Try to load engines! We load the engines configured in patternlab-config.json
const enginesInConfig = findEnginesInConfig(patternLabConfig);

logger.debug(
`Loading engines from ${engineDirectory.displayName}: ${engineDirectory.path} ...`
);
if (enginesInConfig) {
// Quick fix until we've removed @pattern-lab/engine-mustache, starting with https://github.com/pattern-lab/patternlab-node/issues/1239
// @TODO: Remove after removing @pattern-lab/engine-mustache dependency
enginesInConfig.mustache = enginesInConfig.mustache || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach. Do we need to change this to handlebars after you changed the package with #1456?

Copy link
Contributor

@mfranzke mfranzke Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mainly a mitigation, because we still need the mustache engine as we reference mustache templates all over the place, and without it we would get errors like that the header isn't available. By #1455 we should be able to remove this code part again.

enginesInConfig.mustache.package =
enginesInConfig.mustache.package || '@pattern-lab/engine-mustache';
enginesInConfig.mustache.extensions =
enginesInConfig.mustache.extensions || 'mustache';

// find all engine-named things in this directory and try to load them,
// unless it's already been loaded.
enginesInThisDir.forEach(function (engineDiscovery) {
// Try loading each of the configured pattern engines
// eslint-disable-next-line guard-for-in
for (const name in enginesInConfig) {
const engineConfig = enginesInConfig[name];
let errorMessage;
const successMessage = 'good to go';

try {
// Give it a try! load 'er up. But not if we already have,
// of course. Also pass the pattern lab config object into
// of course. Also pass the Pattern Lab config object into
// the engine's closure scope so it can know things about
// things.
if (self[engineDiscovery.name]) {
if (self[name]) {
throw new Error('already loaded, skipping.');
}
self[engineDiscovery.name] = require(engineDiscovery.modulePath);
if (
typeof self[engineDiscovery.name].usePatternLabConfig === 'function'
) {
self[engineDiscovery.name].usePatternLabConfig(patternLabConfig);
}
if (typeof self[engineDiscovery.name].spawnMeta === 'function') {
self[engineDiscovery.name].spawnMeta(patternLabConfig);
if ('package' in engineConfig) {
self[name] = require(engineConfig.package);
if (typeof self[name].usePatternLabConfig === 'function') {
self[name].usePatternLabConfig(patternLabConfig);
}
if (typeof self[name].spawnMeta === 'function') {
self[name].spawnMeta(patternLabConfig);
}
} else {
logger.warning(
`Engine ${name} not configured correctly. Please configure your engines in patternlab-config.json as documented in https://patternlab.io/docs/editing-the-configuration-options/#heading-engines`
);
}
} catch (err) {
errorMessage = err.message;
} finally {
// report on the status of the engine, one way or another!
logger.info(
`Pattern Engine ${engineDiscovery.name}: ${
`Pattern Engine ${name} / package ${engineConfig.package}: ${
errorMessage ? errorMessage : successMessage
}`
);
}
}
} else {
// Try to load engines! We scan for engines at each path specified above. This
// function is kind of a big deal.
enginesDirectories.forEach(function (engineDirectory) {
const enginesInThisDir = findEngineModulesInDirectory(
engineDirectory.path
);

`Loading engines from ${engineDirectory.displayName}: ${engineDirectory.path} ...`;

// find all engine-named things in this directory and try to load them,
// unless it's already been loaded.
enginesInThisDir.forEach(function (engineDiscovery) {
let errorMessage;
const successMessage = 'good to go';

try {
// Give it a try! load 'er up. But not if we already have,
// of course. Also pass the Pattern Lab config object into
// the engine's closure scope so it can know things about
// things.
if (self[engineDiscovery.name]) {
throw new Error('already loaded, skipping.');
}
self[engineDiscovery.name] = require(engineDiscovery.modulePath);
if (
typeof self[engineDiscovery.name].usePatternLabConfig ===
'function'
) {
self[engineDiscovery.name].usePatternLabConfig(patternLabConfig);
}
if (typeof self[engineDiscovery.name].spawnMeta === 'function') {
self[engineDiscovery.name].spawnMeta(patternLabConfig);
}
} catch (err) {
errorMessage = err.message;
} finally {
// report on the status of the engine, one way or another!
logger.info(
`Pattern Engine ${
engineDiscovery.name
} by discovery (deprecated): ${
errorMessage ? errorMessage : successMessage
}`
);
}
});
});
});
}

// Complain if for some reason we haven't loaded any engines.
if (Object.keys(self).length === 0) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/util/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
"density": "compact",
"layout": "horizontal"
},
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
},
"uikits": [
{
"name": "uikit-workshop",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
],
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@
"color": "dark",
"density": "compact",
"layout": "horizontal"
},
"engines": {
"react": {
"package": "@pattern-lab/engine-react",
"fileExtensions": [
"jsx"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
],
"engines": {
"twig": {
"package": "@pattern-lab/engine-twig",
"fileExtensions": [
"twig"
],
"namespaces": {
"atoms": "source/_patterns/atoms/",
"molecules": "source/_patterns/molecules/",
Expand Down
33 changes: 32 additions & 1 deletion packages/docs/src/docs/advanced-config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ Sets the panel name and language for the code tab on the styleguide. Since this

**default**: `mustache`

## engines

An engine is a wrapper around a templating library like Handlebars, Twig or others. An [engine package](docs/template-language-and-patternengines/)
is the bridge between Pattern Lab and the standalone NPM package supporting the templating language.

`engines` accepts an map of Engine objects. The mandatory properties for each Pattern Lab engine are:

- `package`: the NodeJS package name. Add the package of the engine as a dependency in `package.json` before you configure it here.
- `fileExtensions`: list of pattern file extensions which will be handled by this pattern engine.

Other engine specific configuration options can be added and will be passed to the pattern engine at loading time. See the NPM package documentation for the properties each pattern engine supports.

**default**:

```javascript
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"extensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
...
}
}
```

Configuring the engines in the config file was introduced in v5.14. The fallback lookup mode by scanning the
`node_modules` folder is **deprecated** and will be removed in Pattern Lab v6.

## patternStateCascade

See the [Pattern State Documentation](/docs/using-pattern-states/)
Expand Down Expand Up @@ -402,7 +433,7 @@ Important details:
- the [default `paths.source` object paths](https://github.com/pattern-lab/patternlab-node/pull/840/commits/a4961bd5d696a05fb516cdd951163b0f918d5e19) within `patternlab-config.json` are now relative to the current UIKit. See the [structure of uikit-workshop](https://github.com/pattern-lab/patternlab-node/tree/master/packages/uikit-workshop) for more info
- the [default `paths.public` object paths](https://github.com/pattern-lab/patternlab-node/pull/840/commits/812bab3659f504043e8b61b1dc1cdac71f248449) within `patternlab-config.json` are now relative to the current UIKit's `outputDir`. Absolute paths will no longer work. Someone could test putting an absolute path in a UIKit `outputDir` property and see what happens I suppose.
- `dependencyGraph.json` has moved to the project root rather than `public/` as we should only retain one
- The lookup of the uikit by `name` is deprecated and the user will be notified of it. If the `package` property isn't defined, there is a default fallback lookup strategy where the value of `name` is tried as:
- The lookup of the uikit by `name` is **deprecated** and will be removed in v6. The user will be notified of it. If the `package` property isn't defined, there is a default fallback lookup strategy where the value of `name` is tried as:
mfranzke marked this conversation as resolved.
Show resolved Hide resolved
JosefBredereck marked this conversation as resolved.
Show resolved Hide resolved
- `<name>`
- `uikit-<name>`
- `@pattern-lab/<name>`
Expand Down
10 changes: 10 additions & 0 deletions packages/edition-node-gulp/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@
"density": "compact",
"layout": "horizontal"
},
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
},
"uikits": [
{
"name": "uikit-workshop",
Expand Down
5 changes: 5 additions & 0 deletions packages/edition-node/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
],
"engines": {
"handlebars": {
"package": "@pattern-lab/engine-handlebars",
"fileExtensions": [
"handlebars",
"hbs"
],
"extend": "helpers/*.js"
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/edition-twig/patternlab-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"engines": {
"twig": {
"package": "@pattern-lab/engine-twig-php",
"fileExtensions": [
"twig"
],
"namespaces": [
{
"id": "uikit",
Expand Down
4 changes: 4 additions & 0 deletions packages/starterkit-twig-demo/patternlab-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"engines": {
"twig": {
"package": "@pattern-lab/engine-twig-php",
"fileExtensions": [
"twig"
],
"namespaces": [
{
"id": "atoms",
Expand Down