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

feat(storefront): bctheme-1063 modify stencil to start locally with components ui library #895

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Changes from all commits
Commits
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
51 changes: 48 additions & 3 deletions lib/template-assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ const upath = require('upath');

const partialRegex = /\{\{>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}}/g;
const dynamicComponentRegex = /\{\{\s*?dynamicComponent\s*(?:'|")([_|\-|a-zA-Z0-9/]+)(?:'|").*?}}/g;
const includeRegex = /{{2}>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}{2}/g;
const packageMarker = 'external/';

const isExternalTemplate = (templateName) => {
return templateName.startsWith(packageMarker);
};
const defineBaseRoute = (route) => route.split('/').slice(0, 3).join('/');
const applyExternalPath = (templateName, templates) => {
return templates.map((t) => `${defineBaseRoute(templateName)}/${t}`);
};
const replacePartialNames = (content, partialRoute) => {
return content.replace(
includeRegex,
(__, partialName) => `{{> ${defineBaseRoute(partialRoute)}/${partialName} }}`,
);
};

/**
* Takes a templates folder and template name. It returns simple path for custom template if it's available
* or use default folder instead
*
* @param {string} templatesFolder
* @param {string} templateName
*/
function getCustomPath(templatesFolder, templateName) {
let customTemplatesDir;
let customTemplateName;

if (templateName.startsWith(packageMarker)) {
customTemplatesDir = templatesFolder.replace('templates', 'node_modules');
customTemplateName = templateName.slice(packageMarker.length);
} else {
customTemplatesDir = templatesFolder;
customTemplateName = templateName;
}

return path.join(customTemplatesDir, `${customTemplateName}.html`);
}

/**
* Takes a list of templates and grabs their content. It returns simple key/val pair
Expand All @@ -20,14 +58,17 @@ function getContent(templatesFolder, templatePaths, callback) {
templatePaths,
{},
(acc, templatePath, reduceCallback) => {
const file = path.join(templatesFolder, `${templatePath}.html`);
const file = getCustomPath(templatesFolder, templatePath);

fs.readFile(file, { encoding: 'utf-8' }, (err, content) => {
if (err) {
reduceCallback(err);
return;
}

acc[templatePath] = content;
acc[templatePath] = templatePath.startsWith(packageMarker)
? replacePartialNames(content, templatePath)
: content;

reduceCallback(null, acc);
});
Expand Down Expand Up @@ -81,7 +122,7 @@ function getTemplatePaths(templatesFolder, templates, options, callback) {
});

function resolvePartials(templateName, cb2) {
const templatePath = path.join(templatesFolder, `${templateName}.html`);
const templatePath = getCustomPath(templatesFolder, templateName);

fs.readFile(templatePath, { encoding: 'utf-8' }, (err, content) => {
const componentPaths = [];
Expand Down Expand Up @@ -147,6 +188,10 @@ function getTemplatePaths(templatesFolder, templates, options, callback) {
return;
}

if (isExternalTemplate(templateName)) {
matches = applyExternalPath(templateName, matches);
}

async.each(matches, resolvePartials, cb2);
},
);
Expand Down