underscore-template-loader icon indicating copy to clipboard operation
underscore-template-loader copied to clipboard

Can I use absolute paths for my `require` statements?

Open seangwright opened this issue 6 years ago • 4 comments

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!

seangwright avatar Mar 07 '18 17:03 seangwright

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.

emaphp avatar Mar 08 '18 15:03 emaphp

Hi @seangwright, have you been able to do this yet? I tried doing it but I'm having some troubles.

jameesjohn avatar Jun 21 '19 23:06 jameesjohn

@jamesjay4199 unfortunately no. For the project I was working on I used the relative paths.

seangwright avatar Jun 22 '19 19:06 seangwright

Okay, I'm working on something that needs relative paths. If I'm able to do it, I'll ping you.

jameesjohn avatar Jun 23 '19 16:06 jameesjohn