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

refactor: enable fileendings separated by more than one dot #1459

Draft
wants to merge 16 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
31 changes: 23 additions & 8 deletions packages/core/src/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,29 @@ const Pattern = function (
*/
const pathObj = path.parse(this.relPath);

// We need to check for templates with a fileextension that contains multiple dots like e.g. .html.twig
if (
patternlab?.config?.patternExtension?.includes('.') &&
this.relPath.endsWith('.' + patternlab.config.patternExtension)
) {
this.fileExtension = '.' + patternlab.config.patternExtension; // e.g. '.html.twig'
this.fileName = path.basename(this.relPath, this.fileExtension); // e.g. 'colors'
} else {
this.fileExtension = pathObj.ext; // e.g. '.hbs'
this.fileName = pathObj.name; // e.g. 'colors'
}
this.subdir = pathObj.dir; // 'atoms/global'

const info = this.getPatternInfo(
pathObj,
patternlab,
isPromoteToFlatPatternRun ||
(patternlab &&
patternlab.config &&
patternlab.config.allPatternsAreDeeplyNested)
patternlab.config.allPatternsAreDeeplyNested),
this.fileName
);

this.fileName = pathObj.name; // 'colors'
this.subdir = pathObj.dir; // 'atoms/global'
this.fileExtension = pathObj.ext; // '.mustache'

// TODO: Remove if block when dropping ordering by prefix and keep else code
// (When we drop the info about the old ordering is deprecated)
if (
Expand Down Expand Up @@ -345,15 +355,20 @@ Pattern.prototype = {
*
* @param pathObj path.parse() object containing useful path information
*/
getPatternInfo: (pathObj, patternlab, isPromoteToFlatPatternRun) => {
getPatternInfo: (
pathObj,
patternlab,
isPromoteToFlatPatternRun,
filename
) => {
const info = {
// colors(.mustache) is deeply nested in atoms-/global/colors
patternlab: patternlab,
patternHasOwnDir: isPromoteToFlatPatternRun
? path.basename(pathObj.dir).replace(prefixMatcher, '') ===
pathObj.name.replace(prefixMatcher, '') ||
filename.replace(prefixMatcher, '') ||
path.basename(pathObj.dir).replace(prefixMatcher, '') ===
pathObj.name.split('~')[0].replace(prefixMatcher, '')
filename.split('~')[0].replace(prefixMatcher, '')
: false,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/docs/advanced-config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Possibility to define whether the initial viewport width on opening pattern lab

Sets default active pattern info code panel by file extension - if unset, uses the value out of _patternExtension_ config value, or instead use value `html` to display the html code initially, or the value defined for the _patternExtension_.

**default**: _patternExtension_ value (`"hbs"` | `"mustache"` | `"twig"` | `"html"`)
**default**: _patternExtension_ value (`"hbs"` | `"mustache"` | `"twig"` | `"html.twig"` | `"html"`)

## ishControlsHide

Expand Down
4 changes: 2 additions & 2 deletions packages/engine-handlebars/lib/engine_handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ const engine_handlebars = {
* assume it's already present
*/
spawnMeta: function (config) {
this.spawnFile(config, '_head.hbs');
this.spawnFile(config, '_foot.hbs');
this.spawnFile(config, '_head.' + config.patternExtension);
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be dangerous.
When pattern lab discovers multiple pattern engines, it will try to spawn the file found in the engine's folder located under _meta/head...
In that case, the file will not be found, and an error will be thrown here

const metaFileContent = fs.readFileSync(

You can either catch the error and ignore it, or you revert it to its former state.

Another issue could be, that by chance the file will be taken, but the output for the twig file will be overwritten by the content of the hbs file. In that case, it could lead to an error when serving pattern lab because it has a different rendering engine.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather look into the engineFileExtensions and when the config.patternExtension is included there, output the required file. (In that case, we need to read the file we deliver with the engine from the _meta folder and spawn it as the configured extension in config.patternExtension.

this.spawnFile(config, '_foot.' + config.patternExtension);
},

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/engine-mustache/lib/engine_mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ const engine_mustache = {
* assume it's already present
*/
spawnMeta: function (config) {
this.spawnFile(config, '_head.mustache');
this.spawnFile(config, '_foot.mustache');
this.spawnFile(config, '_head.' + config.patternExtension);
this.spawnFile(config, '_foot.' + config.patternExtension);
},

// find and return any {{> template-name }} within pattern
Expand Down
7 changes: 5 additions & 2 deletions packages/engine-twig-php/lib/engine_twig_php.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let patternLabConfig = {};
const engine_twig_php = {
engine: TwigRenderer,
engineName: 'twig-php',
engineFileExtension: '.twig',
engineFileExtension: ['.twig', '.html.twig'],
expandPartials: false,
findPartialsRE:
/{%\s*(?:extends|include|embed)\s+('[^']+'|"[^"]+").*?(with|%}|\s*%})/g,
Expand Down Expand Up @@ -120,7 +120,10 @@ const engine_twig_php = {
*/
spawnMeta(config) {
const { paths } = config;
['_head.twig', '_foot.twig'].forEach((fileName) => {
[
'_head.' + config.patternExtension,
'_foot.' + config.patternExtension,
].forEach((fileName) => {
const metaFilePath = path.resolve(paths.source.meta, fileName);
try {
fs.statSync(metaFilePath);
Expand Down
6 changes: 3 additions & 3 deletions packages/engine-twig/lib/engine_twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var metaPath;
var engine_twig = {
engine: twing,
engineName: 'twig',
engineFileExtension: '.twig',
engineFileExtension: ['.twig', '.html.twig'],

// regexes, stored here so they're only compiled once
findPartialsRE:
Expand Down Expand Up @@ -203,8 +203,8 @@ var engine_twig = {
* assume it's already present
*/
spawnMeta: function (config) {
this.spawnFile(config, '_head.twig');
this.spawnFile(config, '_foot.twig');
this.spawnFile(config, '_head.' + config.patternExtension);
this.spawnFile(config, '_foot.' + config.patternExtension);
},

/**
Expand Down