Skip to content

feat(tools): Templates can be imported from node_modules/ #1860

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

Merged
merged 1 commit into from
Jun 24, 2020
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
1 change: 0 additions & 1 deletion packages/fiori/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dist/resources
dist/test-resources
lib/
node_modules/
src/
test/
bundle.*.js
.eslintrc.js
Expand Down
1 change: 0 additions & 1 deletion packages/main/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dist/resources
dist/test-resources
lib/
node_modules/
src/
test/
bundle.*.js
.eslintrc.js
Expand Down
6 changes: 2 additions & 4 deletions packages/tools/lib/hbs2lit/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const compiler = require("./src/compiler");
const hbs2lit = require("./src/compiler");

module.exports = {
compileString: compiler.compileString
};
module.exports = hbs2lit;
9 changes: 3 additions & 6 deletions packages/tools/lib/hbs2lit/src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ const removeWhiteSpaces = (source) => {
.replace(/}}\s+{{/g, "}}{{"); // Remove whitespace between }} and {{
};

const compileString = async (sInput, config) => {
let sPreprocessed = sInput;
const hbs2lit = (file) => {
let sPreprocessed = includesReplacer.replace(file);

sPreprocessed = includesReplacer.replace(sPreprocessed, config);
sPreprocessed = removeWhiteSpaces(sPreprocessed);

const ast = Handlebars.parse(sPreprocessed);
Expand All @@ -38,6 +37,4 @@ const compileString = async (sInput, config) => {
return result;
};

module.exports = {
compileString
};
module.exports = hbs2lit;
29 changes: 20 additions & 9 deletions packages/tools/lib/hbs2lit/src/includesReplacer.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
const path = require("path");
const nativeFs = require("fs");
const fs = require("fs");

function replaceIncludes(hbs, config) {
const fs = config.fs || nativeFs;
const inclRegex = /{{>\s*include\s*["']([a-zA-Z.\/]+)["']}}/g;
function replaceIncludes(file) {
const filePath = path.dirname(file);
let fileContent = fs.readFileSync(file, "utf-8");

const inclRegex = /{{>\s*include\s*["'](.+?)["']}}/g;
let match;

while((match = inclRegex.exec(hbs)) !== null) {
while((match = inclRegex.exec(fileContent)) !== null) {
inclRegex.lastIndex = 0;
const includeContent = fs.readFileSync(path.join(config.templatesPath, match[1]), "utf-8");
hbs = hbs.replace(match[0], includeContent);

let targetFile = match[1];
if (targetFile.startsWith(".")) {
// Relative path, f.e. {{>include "./Popup.hbs"}} or {{>include "../partials/Header.hbs"}}
targetFile = path.join(filePath, targetFile);
} else {
// Node module path, f.e. {{>include "@ui5/webcomponents/src/Popup.hbs"}}
targetFile = require.resolve(targetFile);
}

fileContent = fileContent.replace(match[0], replaceIncludes(targetFile));
}

return hbs;
return fileContent;
}

module.exports = {
replace: replaceIncludes
};
};
28 changes: 9 additions & 19 deletions packages/tools/lib/hbs2ui5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@ const args = getopts(process.argv.slice(2), {

const onError = (place) => {
console.log(`A problem occoured when reading ${place}. Please recheck passed parameters.`);
}
};

const isHandlebars = (fileName) => fileName.indexOf('.hbs') !== -1;
const parseFile = (filePath, inputDir, outputDir) => {
fs.readFile(filePath, 'utf-8', (err, content) => {

if (err) {
onError('file');
}
const processFile = (file, outputDir) => {

hbs2lit.compileString(content, {
templatesPath: inputDir,
compiledTemplatesPath: outputDir
}).then((litCode) => {
const componentNameMatcher = /(\w+)(\.hbs)/gim;
const componentName = componentNameMatcher.exec(filePath)[1];
const litCode = hbs2lit(file);

writeRenderers(outputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
});
});
}
const componentNameMatcher = /(\w+)(\.hbs)/gim;
const componentName = componentNameMatcher.exec(file)[1];
writeRenderers(outputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
};

const wrapDirectory = (directory, outputDir) => {
directory = path.normalize(directory);
Expand All @@ -51,9 +43,7 @@ const wrapDirectory = (directory, outputDir) => {

files.forEach(fileName => {
if (isHandlebars(fileName)) {

// could be refactored a bit
parseFile(directory + fileName, directory, outputDir);
processFile(path.join(directory, fileName), outputDir);
}
});
})
Expand Down