-
Notifications
You must be signed in to change notification settings - Fork 5
Importing assets from templates #3
Comments
Hey there. I'm really sorry, but I actually have no idea 😖 I've never actually used image loaders or anything similar to really know how they work. I'll probably need to have a look through some other templating language loaders and see what they're doing. I imagine that part of the problem is how sketchy this nunjucks loader actually is. Unfortunately, nunjucks really is not tailored towards being turned into a loader; I've had to do a whole lot of work arounds to get this working. It's possible that because of this webpack is failing to find the potential dependencies within the templates. I've got quite a busy work day today, so I doubt I'll get a chance to look into this until later this evening. If you're interested in trying to solve it, that'd be amazing. I'd be happy to support you if you have any questions about the code or if there are any other ways I can help! |
Thank you very much! I'll throw myself into the Webpack docs and hopefully will be able to help you out with this. Have a nice day! |
Awesome, good luck! :D |
Hiya, I think I know how to do this (thanks to the docs) but first, can you explain a bit how's the code working? In fact, I only use it with the HtmlWebpackPlugin so, for now, I only test this context but if you tell me more about the loader, I'll be more prepared to start coding this feature. (Sorry, I'm quite new to Webpack but I'm motivated :D ) Thanks in advance! |
Sure thing! Right, so the code I have written only handles the HtmlWebpackPlugin situation. If you're targetting web, then it instead loads the other package, Now then, as for what my code actually does. It's been some time since I wrote this, so apologies if I'm a little shaky it on it. It's a little convoluted due to some of the requirements of loaders and Nunjucks, Nunjucks really did not want to be used like this, so there's been quite a lot of workarounds to glue this all together. To understand the loader, first you need to understand the requirements, and the issues. In order for Unfortunately, that's not so easy in Nunjucks. Other templating languages, such as EJS and Pug, have very simple functionality, and require no knowledge of the current context in order to compile. This means they can both use very simplified portable compile functions to return the string. Nunjucks, on the other hand, has support for things like Luckily, Nunjucks has a partial solution to this; precompiled templates. Nunjucks allows you to "precompile" the templates, essentially working out all of the complicated bits first, then outputting a simplified version of the templates for real-time compilation at a later point. This is how Nunjucks is usually used for web. In order to precompile the templates, you need to use the nunjucks node environment loader (https://github.com/mozilla/nunjucks/blob/master/src/node-loaders.js). However, when I tried use this inside of a webpack loader, I encountered a host of recursive dependency errors. This turned out to be because of the require statement on line 35 of the nunjucks node loader. Because of this, I decided to clone the node-loader, and remove the undesirable code which was creating the problems. The result is the fs-loader.js (https://github.com/SudoCat/Nunjucks-Isomorphic-Loader/blob/master/src/fs-loader.js). This creates a loader suitable for precompiling templates from within a webpack loader. You can see this being used when creating the nunjucks environment at https://github.com/SudoCat/Nunjucks-Isomorphic-Loader/blob/master/src/node-loader.js#L46 The next problem I encountered was how precompiled templates work. They are intended to be used live on the front end of a website, compiling them with javascript when needed. Because of this, all of the precompiled templates get automatically stored in the global With the precompiled templates safely stored in the loader's export module, the module then creates a new nunjucks slim environment. This provides us with the function we need to finalise the compilation, which gets done at https://github.com/SudoCat/Nunjucks-Isomorphic-Loader/blob/master/src/node-loader.js#L74 Hopefully this makes sense. Please let me know if there's anything you want me to clarify! In short the loader:
|
Wow, thank you very much for taking the time to explain this to me! Will read this and start coding this weekend. Oh time, if only we could get more of that stuff :') |
That's no problem, I should probably have it written down anyway, just so I don't forget 😆 Haha, you can say that again! |
So, I gave this a try and for now I'm stuck with paths from HtmlWebpackPlugin. I chose the following syntax as it is valid nunjucks: In the loader, at line 50 I just changed this:
So basically, I just changed the Here's the error I get
I'm not sure about how to generate the right require path and I'm stuck with it. I think, this could work outside of the HtmlWebpackPlugin but didn't tried it out. I'll try to check this out again next week but for now, if you have any advice, let me know :) |
Well, my bad. Here's the right way to require the needed asset:
This gets the right path to the wanted asset but I still got a problem as it seems the
And get the following error (looks like it tries to load the binary as a JS module):
So now I get the asset but it's not loader by the
|
The issue should really be about using nunjucks import functionality. I have the same error while trying to import macro from one .njk file into another: |
Okay, I found the source of the error form this issue. You can't use a relative path inside nunjucks imports (probably a related bug in nunjucks repo). Instead you need to use an absolute path relative to The other problem though is that webpack doesn't watch changes in these imported assets (probably because they are not in a "webpack land"/not handled by webpack |
Hi @SudoCat . It's me again (yeah still using this loader :D ) Is there a way to use import statements or anything parsed by other webpack loaders ? (like, for example the
url (asset.png)
in css)I tried the following with no success:
with the following error message:
Sorry if I look dumb with this but my knowledge of loaders is quite limited. Will try to take the time to read the docs about writing loaders so I could help you support this one!
The text was updated successfully, but these errors were encountered: