-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): Templates can be imported from node_modules/ (#1860)
- Loading branch information
1 parent
1d20ba8
commit 6fa5847
Showing
6 changed files
with
34 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ dist/resources | |
dist/test-resources | ||
lib/ | ||
node_modules/ | ||
src/ | ||
test/ | ||
bundle.*.js | ||
.eslintrc.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ dist/resources | |
dist/test-resources | ||
lib/ | ||
node_modules/ | ||
src/ | ||
test/ | ||
bundle.*.js | ||
.eslintrc.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters