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

fix(storefront): bctheme-1145 enable using external lib for templates under organization #954

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Draft

- fix: bctheme-1145 Enable using of external lib nested into organization for templates ([954](https://github.com/bigcommerce/stencil-cli/pull/954))

### 5.0.1 (2022-13-07)

- feat: paypal-1514 add sort type to schema ([960](https://github.com/bigcommerce/stencil-cli/pull/960))
Expand Down
12 changes: 10 additions & 2 deletions lib/stencil-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,20 @@ class Bundle {
*/
async _getExternalLibs(templatePath) {
const content = await fs.promises.readFile(templatePath, { encoding: 'utf-8' });
const externalPathRegex = /{{2}>\s*(external)[^{]*?}{2}/g;
const externalPathRegex = /{{2}>\s*(['"]external)[^{]*?}{2}/g;
const externalTemplatesImports = content.match(externalPathRegex);

if (!externalTemplatesImports) return [];

return externalTemplatesImports.map((templateImport) => templateImport.split('/')[1]);
return externalTemplatesImports.map((templateImport) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think if it's possible to get some test coverage on this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

const [, importPath] = templateAssembler.partialRegex.exec(templateImport);

templateAssembler.partialRegex.lastIndex = 0;

return importPath
.split('/templates/')[0]
.slice(templateAssembler.packageMarker.length + 1);
});
}

async assembleTemplatesTask(callback) {
Expand Down
24 changes: 24 additions & 0 deletions lib/stencil-bundle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,28 @@ describe('Stencil Bundle', () => {

expect(fs.writeFile).toHaveBeenCalledTimes(0);
});

it('should return array with external library names', async () => {
const componentPathA = path.resolve(
process.cwd(),
'test/_mocks/themes/component-with-external-template/a.html',
);
const componentPathB = path.resolve(
process.cwd(),
'test/_mocks/themes/component-with-external-template/b.html',
);
const externalLib = 'theme-ui-components';
const scopedExternalLib = '@bigcommerce/theme-ui-components';
let mockLib;

// eslint-disable-next-line no-underscore-dangle
mockLib = await bundle._getExternalLibs(componentPathB);

expect(mockLib).toContain(externalLib);

// eslint-disable-next-line no-underscore-dangle
mockLib = await bundle._getExternalLibs(componentPathA);

expect(mockLib).toContain(scopedExternalLib);
});
});
13 changes: 11 additions & 2 deletions lib/template-assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const fs = require('graceful-fs');
const path = require('path');
const upath = require('upath');

const partialRegex = /\{\{>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}}/g;
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 pathStringRegex = /^["'].+["']$/;
const packageMarker = 'external/';

/**
Expand All @@ -28,6 +29,12 @@ const replacePartialNames = (content, partialRoute) => {
(__, partialName) => `{{> ${defineBaseRoute(partialRoute)}/${partialName} }}`,
);
};
const trimPartial = (partial) => {
if (pathStringRegex.test(partial)) {
return partial.slice(1, -1);
}
return partial;
};

/**
* Takes a templates folder and template name. It returns simple path for custom template if it's available
Expand Down Expand Up @@ -127,7 +134,8 @@ function getTemplatePaths(templatesFolder, templates, options, callback) {
return callback(null, templatePaths);
});

function resolvePartials(templateName, cb2) {
function resolvePartials(t, cb2) {
const templateName = trimPartial(t);
const templatePath = getCustomPath(templatesFolder, templateName);

fs.readFile(templatePath, { encoding: 'utf-8' }, (err, content) => {
Expand Down Expand Up @@ -253,6 +261,7 @@ function getTemplateContentSync(templatesFolder, templateFile) {
}

module.exports = {
packageMarker,
partialRegex,
dynamicComponentRegex,
assemble,
Expand Down
3 changes: 3 additions & 0 deletions test/_mocks/themes/component-with-external-template/a.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a

{{> "external/@bigcommerce/theme-ui-components/templates/button"}}
3 changes: 3 additions & 0 deletions test/_mocks/themes/component-with-external-template/b.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
b

{{> "external/theme-ui-components/templates/button"}}