radiovisual/metalsmith-rootpath

rootPath returns undefined

Closed this issue · 6 comments

I've followed your instructions but {{rootPath}} returns undefined?

See here: https://github.com/barrymcgee/vanilla-framework/tree/metalsmith-mvp-rootpath

Ok, I will take a look at your metalsmith-mvp-rootpath branch and let you know if I can spot the problem.

Thanks for reporting.

This is not a problem with metalsmith-rootpath, it is simply a matter of you calling the rootpath plugin too late in your build chain. Basically, you are calling .use(rootPath()) AFTER you have built your templates, but you want the rootPath value to be assigned before you render your templates, so that the {{rootPath}} value will be available to them, otherwise, it will return undefined every time.

Simply switching up your build chain so that permalinks, and then rootpath were called before you render your templates solved the problem for me. You can see what I did on my fork of your branch.

Below is the output I get after switching up your build chain, notice how it prints the rootpath, instead of undefined:

❯ gulp build
[22:49:27] Using gulpfile ~/forks/vanilla-framework/gulpfile.js
[22:49:27] Starting 'pattern-library'...
[22:49:27] Finished 'pattern-library' after 3.35 ms
[22:49:27] Starting 'sass-build'...
../
../
../
../
../
../
../
../
../../
../../
../../
../../
../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../
../../

@barrymcgee , Also, just as an FYI, I had to run npm install metalsmith-layouts to avoid this error when trying to run your gulp tasks:

Error: Cannot find module 'metalsmith-layouts'

I am letting you know here, since I can't open an issue or PR on your metalsmith-mvp-rootpath branch.

@radiovisual Thank you for investigating.

Tried your fix but unfortunately it conflicts with permalinks - the side nav no longers works? Have you an example where your plugin works with permalinks?

@barrymcgee , no problem, happy to help.

I have an entire unit test (see the test here) to make sure that rootpath is correctly assigned after permalinks changes the directory structure. That is the only live example I can show you of permalinks working with rootpath, but I have also confirmed in my own usage that permalinks works well with rootpath, however, do you also have other processes in your build chain that affect the directory structure?

Because for me to get it to work, I had to run your plugins in this order:

  • permalinks
  • rootpath
  • collections
  • markdown
  • layout

So I am thinking that if your collections or layout plugin try to change the directory structure, then the rootpath value assigned early in your build chain is no longer valid, thus breaking the relative paths. So do any of these plugins, other than permalinks change your directory structure?

@barrymcgee , did you ever find a resolution on this? I am curious to hear the outcome.

Your build chain has made me think about a possible option to add to this module, that would work 100% of the time, no matter what complicated steps your build chain takes. Check out Issue #8 and respond on that thread if you have anything you would like to mention.

Thanks!