underscore-template-loader
underscore-template-loader copied to clipboard
Can I use absolute paths for my `require` statements?
Hello!
I'm trying to use this loader to import partials into other partials or html files and I would like to use paths from the root of my src/
in my @require()
imports.
I have a directory structure like this
src/
+-- footer.html
+-- header.html
+-- features/
| +-- users/
| | +-- users.html
+-- pages/
| +-- about-us/
| | +-- about-us.html
I currently have src/features/users/users.html
looking like this
<div class="users">
<i class="fas fa-plus"></i>
<h1>Users</h1>
<span>Users</span>
<span>Users</span>
<span>Users</span>
<span>Users</span>
<span>Users</span>
</div>
And my src/pages/about-us/about-us.html
looks like this
<!-- header -->
@require('../../header.html')
<h1>About Us</h1>
<!-- users -->
@require('../../features/users/users.html')
@require('../../features/users/users.html')
@require('../../features/users/users.html')
<!-- footer -->
@require('../../footer.html')
I would like to get rid of the ../../
in the @require()
and instead path from the root like @require('features/users/users.html')
and @require('header.html')
Is there a way through syntax, conventions or configuration to do this?
Thanks!
Hi. This loader doesn't have any option on that regard. Still, we do provide a mechanism for overriding the require macro, which is explained in the docs. If you take a look to the lib/macros.js file you'll find the following:
require: function (resourcePath, args) {
var argsExpr = args ? '(' + objExtend + ')' + '(arguments, ' + JSON.stringify(args) + ')' : 'arguments';
return "require(" + JSON.stringify(loaderUtils.urlToRequest(resourcePath)) + ").apply(null," + argsExpr + ")";
}
This code shows that we basically let loader-utils resolve how the resource path should be handled.
Hi @seangwright, have you been able to do this yet? I tried doing it but I'm having some troubles.
@jamesjay4199 unfortunately no. For the project I was working on I used the relative paths.
Okay, I'm working on something that needs relative paths. If I'm able to do it, I'll ping you.