-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): add ability to specify template loader
fixes #172
- Loading branch information
Showing
6 changed files
with
107 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
const path = require('path'); | ||
const fse = require('fs-extra'); | ||
|
||
/** | ||
* Creates an template loader. | ||
* @param {string[]} roots Root directories for resolution. | ||
*/ | ||
module.exports = function createLoader(roots) { | ||
if (!Array.isArray(roots)) { | ||
// eslint-disable-next-line no-param-reassign | ||
roots = [roots]; | ||
} | ||
|
||
/** | ||
* Resolves the template against the given roots. | ||
* @param {string} baseDir additional root | ||
* @param {string} uri template uri | ||
* @returns {Promise<string>} | ||
*/ | ||
async function resolve(baseDir, uri) { | ||
let bases = [baseDir, ...roots]; | ||
|
||
// if uri starts with '.' or '..', only consider specified bases. | ||
// otherwise also consider apps and libs. | ||
if (!uri.startsWith('./') && !uri.startsWith('../')) { | ||
bases = bases.reduce((prev, root) => { | ||
prev.push(root); | ||
prev.push(path.resolve(root, 'apps')); | ||
prev.push(path.resolve(root, 'libs')); | ||
return prev; | ||
}, []); | ||
} | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const base of bases) { | ||
const templatePath = path.resolve(base, uri); | ||
// eslint-disable-next-line no-await-in-loop | ||
if (await fse.pathExists(templatePath)) { | ||
return templatePath; | ||
} | ||
} | ||
throw Error(`Unable to resolve template: ${uri}. Search Path: ${bases}`); | ||
} | ||
|
||
/** | ||
* Load the template. | ||
* @param {string} baseDir additional root | ||
* @param {string} uri template uri | ||
* @returns {Promise<>} the template source and resolved path | ||
*/ | ||
async function load(baseDir, uri) { | ||
const templatePath = await resolve(baseDir, uri); | ||
return { | ||
data: await fse.readFile(templatePath, 'utf-8'), | ||
path: templatePath, | ||
}; | ||
} | ||
|
||
return load; | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template data-sly-template.tmpl> | ||
<h1>Project ${page.title}</h1> | ||
</template> |