Skip to content

Commit 6fa5847

Browse files
authored
feat(tools): Templates can be imported from node_modules/ (#1860)
1 parent 1d20ba8 commit 6fa5847

File tree

6 files changed

+34
-40
lines changed

6 files changed

+34
-40
lines changed

packages/fiori/.npmignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dist/resources
33
dist/test-resources
44
lib/
55
node_modules/
6-
src/
76
test/
87
bundle.*.js
98
.eslintrc.js

packages/main/.npmignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dist/resources
33
dist/test-resources
44
lib/
55
node_modules/
6-
src/
76
test/
87
bundle.*.js
98
.eslintrc.js

packages/tools/lib/hbs2lit/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const compiler = require("./src/compiler");
1+
const hbs2lit = require("./src/compiler");
22

3-
module.exports = {
4-
compileString: compiler.compileString
5-
};
3+
module.exports = hbs2lit;

packages/tools/lib/hbs2lit/src/compiler.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ const removeWhiteSpaces = (source) => {
1212
.replace(/}}\s+{{/g, "}}{{"); // Remove whitespace between }} and {{
1313
};
1414

15-
const compileString = async (sInput, config) => {
16-
let sPreprocessed = sInput;
15+
const hbs2lit = (file) => {
16+
let sPreprocessed = includesReplacer.replace(file);
1717

18-
sPreprocessed = includesReplacer.replace(sPreprocessed, config);
1918
sPreprocessed = removeWhiteSpaces(sPreprocessed);
2019

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

41-
module.exports = {
42-
compileString
43-
};
40+
module.exports = hbs2lit;
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
const path = require("path");
2-
const nativeFs = require("fs");
2+
const fs = require("fs");
33

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

9-
while((match = inclRegex.exec(hbs)) !== null) {
11+
while((match = inclRegex.exec(fileContent)) !== null) {
1012
inclRegex.lastIndex = 0;
11-
const includeContent = fs.readFileSync(path.join(config.templatesPath, match[1]), "utf-8");
12-
hbs = hbs.replace(match[0], includeContent);
13+
14+
let targetFile = match[1];
15+
if (targetFile.startsWith(".")) {
16+
// Relative path, f.e. {{>include "./Popup.hbs"}} or {{>include "../partials/Header.hbs"}}
17+
targetFile = path.join(filePath, targetFile);
18+
} else {
19+
// Node module path, f.e. {{>include "@ui5/webcomponents/src/Popup.hbs"}}
20+
targetFile = require.resolve(targetFile);
21+
}
22+
23+
fileContent = fileContent.replace(match[0], replaceIncludes(targetFile));
1324
}
1425

15-
return hbs;
26+
return fileContent;
1627
}
1728

1829
module.exports = {
1930
replace: replaceIncludes
20-
};
31+
};

packages/tools/lib/hbs2ui5/index.js

+9-19
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,18 @@ const args = getopts(process.argv.slice(2), {
1818

1919
const onError = (place) => {
2020
console.log(`A problem occoured when reading ${place}. Please recheck passed parameters.`);
21-
}
21+
};
22+
2223
const isHandlebars = (fileName) => fileName.indexOf('.hbs') !== -1;
23-
const parseFile = (filePath, inputDir, outputDir) => {
24-
fs.readFile(filePath, 'utf-8', (err, content) => {
2524

26-
if (err) {
27-
onError('file');
28-
}
25+
const processFile = (file, outputDir) => {
2926

30-
hbs2lit.compileString(content, {
31-
templatesPath: inputDir,
32-
compiledTemplatesPath: outputDir
33-
}).then((litCode) => {
34-
const componentNameMatcher = /(\w+)(\.hbs)/gim;
35-
const componentName = componentNameMatcher.exec(filePath)[1];
27+
const litCode = hbs2lit(file);
3628

37-
writeRenderers(outputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
38-
});
39-
});
40-
}
29+
const componentNameMatcher = /(\w+)(\.hbs)/gim;
30+
const componentName = componentNameMatcher.exec(file)[1];
31+
writeRenderers(outputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
32+
};
4133

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

5244
files.forEach(fileName => {
5345
if (isHandlebars(fileName)) {
54-
55-
// could be refactored a bit
56-
parseFile(directory + fileName, directory, outputDir);
46+
processFile(path.join(directory, fileName), outputDir);
5747
}
5848
});
5949
})

0 commit comments

Comments
 (0)