Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to include twig files outside project path #48

Open
ilkkave opened this issue Apr 2, 2019 · 9 comments
Open

Unable to include twig files outside project path #48

ilkkave opened this issue Apr 2, 2019 · 9 comments

Comments

@ilkkave
Copy link

ilkkave commented Apr 2, 2019

Problem/motivation

I would like to include twig files outside the project path, but currently it’s not possible, because twig resolves to wrong path with those files.

An example:
Root for project where I use twig-loader is /some/path/my_project.
I can import twig file from /some/path/my_project/templates/template.twig.
I can’t import twig file from /some/path/templates/template.twig and will get an error like:

ERROR in ../templates/template.twig
Module not found: Error: Can't resolve 'twig' in ‘/some/path/my_project’
 @ ../templates/template.twig 3:11-26
 …

Proposed resolution

Use stringifyRequest (https://github.com/webpack/loader-utils#stringifyrequest) from loader-utils to get path to twig in https://github.com/zimmo-be/twig-loader/blob/master/lib/loader.js#L33.

@ben-rogerson
Copy link

@ilkkave Another loader by megahertz takes care of this issue combined with a Webpack alias. Today I integrated it into my craft-storybook-starter if you want to take a look.

@mralexho
Copy link

@ben-rogerson thanks for the suggestion. Thought our team would be able to use this solution but it looks like twigjs-loader doesn't support concat ~ 😕

megahertz/twigjs-loader#4

@ben-rogerson
Copy link

Are there any other loaders you've found that support contact?

@mralexho
Copy link

We're looking for any workarounds. Will keep you posted if we find a solution.

@pjoulot
Copy link

pjoulot commented Jul 23, 2021

Did someone found a workaround? It is pretty annoying to have to keep that file at the project level.

@stfn00
Copy link

stfn00 commented Oct 28, 2021

Hi @mralexho, you have found a solution for concatenation?

We're looking for any workarounds. Will keep you posted if we find a solution.

@ilkkave
Copy link
Author

ilkkave commented Oct 28, 2021

Hi. In our project, where we use version 0.4.1 of https://github.com/AmazeeLabs/twig-loader, which is a fork from this, we have applied this patch to solve the problem:

diff --git a/node_modules/twig-loader/lib/loader.js b/node_modules/twig-loader/lib/loader.js
index d2475dd..e461f6b 100644
--- a/node_modules/twig-loader/lib/loader.js
+++ b/node_modules/twig-loader/lib/loader.js
@@ -1,6 +1,7 @@
 var Twig = require("twig");
 var path = require("path");
 var hashGenerator = require("hasha");
+var loaderUtils = require("loader-utils");
 var mapcache = require("./mapcache");
 var compilerFactory = require("./compiler");
 var getOptions = require("./getOptions");
@@ -30,7 +31,10 @@ module.exports = function(source) {
 
     tpl = tpl.compile({
         module: 'webpack',
-        twig: 'twig'
+        // Use correct path for requiring 'twig' when importing Twig templates
+        // outside project path.
+        // Fixes https://github.com/zimmo-be/twig-loader/issues/48.
+        twig: loaderUtils.stringifyRequest(this, require.resolve('twig'))
     });
 
     this.callback(null, tpl);

@stfn00
Copy link

stfn00 commented Oct 28, 2021

Hi @ilkkave thanks for the reply.
I tried to make the changes but they still don't work. I also tried to use your version directly and keep giving me problems.

@padupuy
Copy link

padupuy commented Nov 2, 2021

hi @ilkkave
Use this fork #48 to solve the include problem, it works for me.
And see this file for the config of the twig namespaces https://github.com/emulsify-ds/emulsify-drupal/blob/master/.storybook/webpack.config.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants