From b741556eaab908204ab868dcc7b0dbfcab262b27 Mon Sep 17 00:00:00 2001 From: Jarrod Date: Wed, 18 Sep 2024 10:55:19 +0100 Subject: [PATCH 1/3] Added normalizePath Added normalizePath method from vite so the import part for the templates work on windows machines. Changes e.g. c:\path\to\file.twig => c:/path/to/file.twig --- src/index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index eb0a6ef..46fe919 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ import Twig from "twig" import { resolve, dirname } from "node:path" import { existsSync } from "node:fs" +import { normalizePath } from "vite" const { twig } = Twig @@ -26,17 +27,23 @@ const includeTokenTypes = [ const resolveFile = (directory, file) => { const filesToTry = [file, `${file}.twig`, `${file}.html.twig`] + + let resolvedFile = "" + for (const ix in filesToTry) { const path = resolve(filesToTry[ix]) if (existsSync(path)) { - return path + resolvedFile = path + break } const withDir = resolve(directory, filesToTry[ix]) if (existsSync(withDir)) { - return withDir + resolvedFile = withDir + break } } - return resolve(directory, file) + + return normalizePath(resolvedFile || resolve(directory, file)) } const pluckIncludes = (tokens) => { From 12f963306afe08567bdd822a1fa336cae7eb7b25 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Thu, 19 Sep 2024 07:06:19 +1000 Subject: [PATCH 2/3] Early returns ftw --- src/index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 46fe919..b83616f 100644 --- a/src/index.js +++ b/src/index.js @@ -27,23 +27,18 @@ const includeTokenTypes = [ const resolveFile = (directory, file) => { const filesToTry = [file, `${file}.twig`, `${file}.html.twig`] - - let resolvedFile = "" - for (const ix in filesToTry) { const path = resolve(filesToTry[ix]) if (existsSync(path)) { - resolvedFile = path - break + return normalizePath(path); } const withDir = resolve(directory, filesToTry[ix]) if (existsSync(withDir)) { - resolvedFile = withDir - break + return normalizePath(withDir); } } - return normalizePath(resolvedFile || resolve(directory, file)) + return normalizePath(resolve(directory, file)) } const pluckIncludes = (tokens) => { From 4cf5fb6018946fc876c13d4d6deb639976c608ae Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Thu, 19 Sep 2024 07:07:53 +1000 Subject: [PATCH 3/3] Lint --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index b83616f..247b8df 100644 --- a/src/index.js +++ b/src/index.js @@ -30,11 +30,11 @@ const resolveFile = (directory, file) => { for (const ix in filesToTry) { const path = resolve(filesToTry[ix]) if (existsSync(path)) { - return normalizePath(path); + return normalizePath(path) } const withDir = resolve(directory, filesToTry[ix]) if (existsSync(withDir)) { - return normalizePath(withDir); + return normalizePath(withDir) } }