Skip to content

Commit

Permalink
fix: STRF-10768 Stencil bundle fails on some (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored May 12, 2023
1 parent 3e865aa commit 968e5e6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 26 deletions.
61 changes: 41 additions & 20 deletions lib/ScssValidator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require('colors');
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');

const cssCompiler = require('./css/compile');
const { recursiveReadDir } = require('./utils/fsUtils');

/* eslint-disable no-useless-escape */
const STYLESHEET_REGEXP = /{{\s*stylesheet\s*([\/a-zA-Z'"\.-]+)\s*}}/i;
const STYLESHEET_REGEXP = /{{\s*stylesheet\s*([\/a-zA-Z'"\.-]+)\s*}}/gi;

class ScssValidator {
/**
Expand Down Expand Up @@ -36,7 +37,6 @@ class ScssValidator {
);
} catch (e) {
const message = this.parseStencilStylesError(e);
console.log(e.file);
throw new Error(
`${message} while compiling css files from "${stylesPath}/${file}".`.red,
);
Expand All @@ -58,44 +58,65 @@ class ScssValidator {
const cssFiles = [];
for await (const file of files) {
const content = await fs.promises.readFile(file, { encoding: 'utf-8' });
const result = content.match(STYLESHEET_REGEXP);
const result = content.matchAll(STYLESHEET_REGEXP);
if (result) {
// remove quotes
const fileName = result[1].slice(1, -1);

const filePath = this.tryToResolveCssFileLocation(fileName, result);
if (!cssFiles.includes(filePath)) {
// check if file exist
cssFiles.push(filePath);
for (const item of result) {
// remove quotes
const filePath = item[1].slice(1, -1);
const fileName = this.tryToResolveCssFileLocation(filePath);
if (
!this.isStyleSheetAComment(content, filePath) &&
!cssFiles.includes(fileName)
) {
cssFiles.push(fileName);
}
}
}
}

return cssFiles;
}

isStyleSheetAComment(content, cssFilePath) {
const $ = cheerio.load(content);
const comments = $('*')
.contents()
.filter(function () {
return this.nodeType === 8;
});
for (const comment of comments) {
const { data } = comment;
if (data && data.includes('stylesheet') && data.includes(cssFilePath)) {
return true;
}
}

return false;
}

// returns relative path starting from root scss/css folder
tryToResolveCssFileLocation(fileName, result) {
tryToResolveCssFileLocation(filePath) {
const possibleLocations = [
fileName,
fileName.replace('/css/', '/scss/'),
fileName.replace('/scss/', '/css/'),
fileName.replace('/css/', '/scss/').replace('.css', '.scss'),
fileName.replace('/scss/', '/css/').replace('.scss', '.css'),
filePath,
filePath.replace('/css/', '/scss/'),
filePath.replace('/scss/', '/css/'),
filePath.replace('/css/', '/scss/').replace('.css', '.scss'),
filePath.replace('/scss/', '/css/').replace('.scss', '.css'),
];

for (const location of possibleLocations) {
const filePath = path.join(this.themePath, location);
if (fs.existsSync(filePath)) {
const fullFilePath = path.join(this.themePath, location);
if (fs.existsSync(fullFilePath)) {
if (!this.isRootCssFile(location)) {
return this.getCssFileWithoutRootFolder(location);
}
const fileParts = path.parse(filePath);
const fileParts = path.parse(fullFilePath);
return fileParts.name;
}
}

throw new Error(`Couldn't find file for this statement: ${result[0]}`.red);
console.log(`Couldn't validate scss compilation for this file path: ${filePath}`.yellow);
return null;
}

// root folders are /assets/css /assets/scss
Expand Down
7 changes: 5 additions & 2 deletions lib/stencil-bundle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ describe('Stencil Bundle', () => {

it('should assemble CSS files', async () => {
jest.spyOn(async, 'map').mockImplementation((coll, iteratee, cb) =>
cb(null, ['this is dog']),
cb(null, ['this is dog', 'this is not']),
);

const task = bundle.getCssAssembleTask('scss');
const result = await promisify(task.bind(bundle))();

expect(result).toEqual({ 'theme.scss': 'this is dog' });
expect(result).toEqual({
'checkout.scss': 'this is dog',
'theme.scss': 'this is not',
});
});

it('should error on assemble CSS files', async () => {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"async": "^3.2.3",
"axios": "^0.27.2",
"browser-sync": "^2.27.9",
"cheerio": "^1.0.0-rc.10",
"cheerio": "^1.0.0-rc.12",
"colors": "1.4.0",
"commander": "^6.1.0",
"confidence": "^5.0.1",
Expand Down
3 changes: 3 additions & 0 deletions test/_mocks/themes/valid/assets/custom/css/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
color: red;
}
3 changes: 3 additions & 0 deletions test/_mocks/themes/valid/assets/scss/checkout.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2 {
color: #0000AA;
}
6 changes: 6 additions & 0 deletions test/_mocks/themes/valid/templates/components/b.html
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
b


<!-- test
{{{stylesheet '/assets/custom/css/test.css'}}} -->

more text here
2 changes: 2 additions & 0 deletions test/_mocks/themes/valid/templates/pages/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<head>
<title>page.html</title>
{{head.scripts}}
{{{stylesheet '/assets/css/theme.css'}}}
{{{stylesheet '/assets/css/checkout.css'}}}
</head>
<body>
{{#if theme_settings.display_that}}
Expand Down

0 comments on commit 968e5e6

Please sign in to comment.