Giter Club home page Giter Club logo

nunjucks-isomorphic-loader's Introduction

Nunjucks Isomorphic loader for webpack

Nunjucks loader for webpack, supporting both javascript templating and generating static HTML files through the HtmlWebpackPlugin.

Completely rewritten, now (mostly) error free. Supports context file (.js only) for passing data.

This loader supports the following extensions: .njk, .nunjucks, .html, .tpl, .tmpl

Warning: (Mostly) Unmaintained

Unfortunately, I rarely get much free time to work on this package any more. While I'll always try to answer any questions anyone has, it may take some time and I might not know the correct answer. Luckily the Open Source Community is filled with lovely people who have keep this updated and documented!

Special thanks to:

Installation

This loader need nunjucks as a peer dependency. Versions >= 2.5.0 <= 4.0.0 are accepted. If you do not have nunjucks already installed, first run:

Using yarn:

yarn add nunjucks -D

Using npm:

npm install nunjucks -D

If you have nunjucks installed, you can then install the loader.

Using yarn:

yarn add nunjucks-isomorphic-loader -D

Using npm:

npm install nunjucks-isomorphic-loader -D

Usage

Basic usage of this loader with html-webpack-plugin

module: {
  rules: [
    {
      test: /\.(njk|nunjucks|html|tpl|tmpl)$/,
      use: [
        {
          loader: 'nunjucks-isomorphic-loader',
          query: {
            root: [path.resolve(__dirname, 'path/to/templates/root')]
          }
        }
      ]
    }
  ]
},

plugins: [
  new HtmlWebpackPlugin({
    customData: { foo: 'bar' },
    filename: 'list.html',
    template: 'path/to/template.njk'
  })
]

Accessing data from the templates with the above config of html-webpack-plugin

path/to/template.njk :

{% set vars = htmlWebpackPlugin.options.customData %}

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{{ vars.foo }}</title>{# outputs 'bar' #}
  </head>
  <body>
    <header class="header">
    {% block header %}
      <h1 class="header-logo">
        <a href="#">{{ vars.foo }}</a>{# outputs 'bar' #}
      </h1>
    {% endblock %}
    </header>

    {% block content %}
      <section>
        <p>I was generated with html-webpack-plugin and nunjucks-isomorphic-loader!</p>
      </section>
    {% endblock %}
  </body>
</html>

nunjucks-isomorphic-loader's People

Contributors

3dos avatar sudocat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nunjucks-isomorphic-loader's Issues

Hardcoded file extensions

Right now, we have the "accepted" file extensions hard coded into the loader.

https://github.com/SudoCat/Nunjucks-Isomorphic-Loader/blob/master/src/node-loader.js#L54

We could instead provide a configuration option to allow users to customise this.

Even cooler would be if we could detect the "test" which has been applied to the loader, and then use this to match the extensions. This would remove the amount of user configuration required, however that could provide more unreliable.

Importing assets from templates

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:

{% import '../images/app/favicon.png' as favicon %}

with the following error message:

Error: /project/path/node_modules/nunjucks/browser/nunjucks-slim.js?:955
                      if(err) { throw err; }
                                ^
  Template render error: (orbital.njk)
    Template render error: (orbital.njk)
    TypeError: path.dirname is not a function
  
  - nunjucks-slim.js?:183 Object.exports.prettifyError
    [.]/[nunjucks]/browser/nunjucks-slim.js?:183:16
  
  - nunjucks-slim.js?:943 eval
    [.]/[nunjucks]/browser/nunjucks-slim.js?:943:32
  
  - loader.js:120 new_cls.root [as rootRenderFunc]
    [orbital.njk?.]/[html-webpack-plugin]/lib/loader.js:120:3
  
  - nunjucks-slim.js?:936 new_cls.render
    [.]/[nunjucks]/browser/nunjucks-slim.js?:936:16
  
  - nunjucks-slim.js?:767 eval
    [.]/[nunjucks]/browser/nunjucks-slim.js?:767:36
  
  - nunjucks-slim.js?:689 createTemplate
    [.]/[nunjucks]/browser/nunjucks-slim.js?:689:26
  
  - nunjucks-slim.js?:704 handle
    [.]/[nunjucks]/browser/nunjucks-slim.js?:704:26
  
  - nunjucks-slim.js?:718 eval
    [.]/[nunjucks]/browser/nunjucks-slim.js?:718:22
  
  - nunjucks-slim.js?:356 next
    [.]/[nunjucks]/browser/nunjucks-slim.js?:356:14
  
  - nunjucks-slim.js?:363 Object.exports.asyncIter
    [.]/[nunjucks]/browser/nunjucks-slim.js?:363:6

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!

Loader not recognising 'web' target

When I try to load a simple nunjucks template with this loader, the build fails with the following error message:

Module build failed: TypeError: Cannot read property 'root' of null
at Object.module.exports (/Users/jannie/Desktop/pim/node_modules/nunjucks-isomorphic-loader/src/node-loader.js:27:31)
at Object.module.exports (/Users/jannie/Desktop/pim/node_modules/nunjucks-isomorphic-loader/index.js:16:44)

From the line that is referenced it looks like the loader is not recognizing the webpack target:

module.exports = function (source) {
  return this.target === 'web' ? (
    require('nunjucks-loader').bind(this)(source)
  ) : (
    require('./src/node-loader').bind(this)(source)  // <--- this is line 16
  );
};

You can see from this gist that I explicitly set the target as 'web' in my webpack config.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.