nunjucks-loader icon indicating copy to clipboard operation
nunjucks-loader copied to clipboard

Create 4x branch

Open at0g opened this issue 7 years ago • 8 comments

Disclaimer: While I maintain this repo, I don't use nunjucks and have not done so for several years now. It does not suit my style of work, but I can appreciate that it may be good for others. If you would like to step up to become a maintainer of this package, contributing to this release would be the way to go.

I have started working on a ~~3.x~~ 4.x version of this module, which can be seen here: https://github.com/at0g/nunjucks-loader/tree/3x

Currently the new branch has a working example using html-webpack-plugin; along with a browser only version.

As of nunjucks@2, their packaging approach is literally hostile to UMD. This makes it very difficult to support code that runs in the node environment (html-webpack-plugin, extract-text-plugin etc). Instead, I have opted for the following approach:

  1. Precompile all of the template strings (almost environment agnostic) within the loader
  2. Use the browser slim version in both browser and node environments

The caveat is this:

if (target === 'node') {
    // perform some messy crap to use the nunjucks precompiled loader in node. 
    const window = global.window || {};
    window.nunjucksPrecompiled = false;
    global.window = window;
}

My concern with doing this is that it's fairly common to branch other code based on typeof window !== 'undefined'.

at0g avatar Jan 25 '18 16:01 at0g

I was just trying to solve this problem (making a loader for node-side that plays nicely with html-loader). It looks like I'll be using nunjucks for some time at work so I might be interested in helping out on this project.

I took a slightly different approach by parsing the templates during the "pitch" phase so that we can collect all the required templates sources and use a custom Loader to pull them out of the require system.... it wasn't pretty... but it works.

It's a shame there's no way to fully precompile the templates AND support other loaders since I'm sure there's some tree shaking that we will miss out on.

Anyway... I just pushed a PoC here: https://github.com/chrisfarms/njk-loader

Since I don't need browser compilation I didn't even attempt to solve that, but the same technic should work.

Let me know if you think this might be a good direction for nunjucks-loader and I'll have a go polishing it up into a PR with tests/browser support.

chrisfarms avatar Feb 05 '18 09:02 chrisfarms

@chrisfarms Thanks for your input. A custom Loader that leverages require sounds like a better approach than rewriting the runtime... so yes, I think this could be a good direction :)

Give me a week (to find some time) and I will update the sibling packages in the 3.x branch with test suites for specific scenarios (node, browser, umd, template inheritance, filters, macros etc).

at0g avatar Feb 05 '18 14:02 at0g

how about webpack 4.x support?

bonesoul avatar Mar 02 '18 08:03 bonesoul

I continued to mature the version I was working on at https://github.com/chrisfarms/njk-loader/pull/2 ... it should now work for browsers and webpack 4 ... hopefully I'll find some time this weekend to finish up adding tests ... but if @at0g can get some test in here I'd be happy to port it over to here rather than creating yet another loader.

chrisfarms avatar Mar 02 '18 08:03 chrisfarms

@chrisfarms - sorry about the delay... there is a lot going on with life at the moment. I'll try for tests this week. Failing that, I agree with not creating another loader, so will gladly hand over the module name if you want to maintain it.

at0g avatar Mar 02 '18 10:03 at0g

Thanks at0g ... I'd def be interested in taking over maintaining the module if you're not very active in the nunjucks space these days.... however what would be even better for the community is if I can get my workplace to adopt the project ... they are using nunjucks in a few projects so I'll see if I can drum up some support

chrisfarms avatar Mar 05 '18 09:03 chrisfarms

@chrisfarms any updates ?

danielpza avatar Jun 29 '18 19:06 danielpza

@at0g @chrisfarms Are there any updates on this? Trying to decide on a template engine and loader, so figured I'd drop a quick message here.

mdmoreau avatar Jan 04 '19 06:01 mdmoreau