Skip to content

Commit

Permalink
fix: dynamic template path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
fiadone committed Mar 15, 2022
1 parent aa412e2 commit b4c29cb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function viteTwigPlugin(options) {
transformIndexHtml: {
enforce: 'pre',
async transform(content) {
const { template, data } = parseHTML(content)
const { template, data } = parseHTML(content, settings.views)
return template
? await renderTemplate(template, { ...globals, ...data }, settings)
: content
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-twig",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"keywords": [
"vite-plugin",
Expand Down
5 changes: 3 additions & 2 deletions tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ function configureTwig({ functions = {}, filters = {} } = {}) {
/**
* It handles the original html content parsing in order to retrieve the template details
* @param {string} content
* @param {string} basePath
* @returns {object}
*/
function parseHTML(content) {
function parseHTML(content, basePath = '') {
try {
const [_, specs] = content.match(/<script\b[^>]*>([\s\S]+)<\/script>/) || []
const { template, data } = JSON.parse(specs || content)
return { template: path.resolve(process.cwd(), template), data }
return { template: path.join(process.cwd(), basePath, template), data }
} catch (err) {
console.warn(err)
return {}
Expand Down

0 comments on commit b4c29cb

Please sign in to comment.