Comments (15)
@Aendrew Thank you! Your post lead me to my current solution. In conjunction with this post, I was able to come up with a solution that allows me to use a .njk or .nunjucks file as an entry point:
First, we install nunjucks-html-loader:
npm i nunjucks-html-loader -D
Or with yarn:
yarn add nunjucks-html-loader -D
Imagine we have the following folder structure:
- client/
-templates/
index.njk
layout.njk
- webpack.config.js
Inside index.njk:
<!-- index.nunjucks -->
{% extends "layout.njk" %}
{% block content %}
{% endblock %}
As you can see I'm including layout.njk inside my index.njk
Here's how we achieve it with Webpack: You're flexible to modify it.
Second, we configure Webpack with the following:
//#1: Define the HTML Webpack Plugin:
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
filename: 'index.html',
inject: 'body',
// Here is part of the magic, we get the index.njk but we tell
// webpack to pass it through the nunjucks-html-loader
template: 'nunjucks-html-loader!./client/templates/index.njk',
});
module.exports = {
// You should not have this the same. This is from my site. Go down to see the important part:
entry: './client/index.js',
output: {
filename: production ? '[name]-[hash].js' : 'bundle.js',
path: __dirname + '/dist',
publicPath: 'dist/' //Important!!! : https://github.com/webpack/webpack/issues/1426
},
// #2 We load the HTMLWebpackPluginConfig
plugins: [
HtmlWebpackPluginConfig,
extractTextPlugin
],
resolve: {
extensions: ['.Webpack.js', '.web.js', '.ts', '.js', '.tsx']
},
// HERE is the important part
module: {
loaders: [
{
// HTML LOADER
// Super important: We need to test for the html
// as well as the nunjucks files
test: /\.html$|njk|nunjucks/,
use: ['html-loader',{
loader: 'nunjucks-html-loader',
options : {
// Other super important. This will be the base
// directory in which webpack is going to find
// the layout and any other file index.njk is calling.
searchPaths: ['./client/templates'],
}
}]
}
]
}
}
Run webpack, and you shall be ready to go ;)
from nunjucks-loader.
I'm getting the same here.
Any news on this?
Using:
"html-webpack-plugin": "^2.22.0",
"nunjucks": "^2.5.2",
"nunjucks-loader": "^2.4.4",
"webpack": "^2.1.0-beta.25",
Html Webpack Plugin:
Error: Child compilation failed:
Module build failed: Error: [nunjucks-loader] non-web targets are not supported
- Error: [nunjucks-loader] non-web targets are not supported
- compiler.js:76
[rpg-web]/[html-webpack-plugin]/lib/compiler.js:76:16
- Compiler.js:279 Compiler.
[rpg-web]/[webpack]/lib/Compiler.js:279:10
- Compiler.js:474
[rpg-web]/[webpack]/lib/Compiler.js:474:13
[...]
from nunjucks-loader.
Anyone needing to use Nunjucks with html-webpack-plugin
, one solution is to use nunjucks-html-loader
in conjunction with HTML loader:
{
test: /\.(html|njk)$/,
use: [
{
loader: 'html-loader',
},
{
loader: 'nunjucks-html-loader',
options: {
searchPaths: [
resolve(__dirname, 'views'),
],
filters: require('./filters'),
context: {hi: "there"},
}
},
]
},
...
plugins: [
new HtmlWebpackPlugin({
template: 'client/index.html',
}),
]
nunjucks-html-loader
is sort of outdated (I think is more geared towards Webpack 1?) and I've forked it to be a bit more modern: https://github.com/ft-interactive/nunjucks-html-loader
from nunjucks-loader.
Unfortunately this is difficult (possibly won't fix) due to how nunjucks 2.x loads dependencies (and a lack of UMD support).
If you want to take a crack at it, a PR is welcome
from nunjucks-loader.
What is the exact problem, though?
Nunjucks uses it's own mechanism for loading dependencies and that mechanism depends on the template object (and not just the render
method). In a nutshell, this module is monkey patching nunjucks own proprietary dependency resolution system to use require from webpack.
What this means (and getting to the exact problem) is that when b depends on a, it's dependent on a being an instance of nunjucks.Template
and not just the render
method.
In regards to the non-web targets are not supported - the 1.x versions of this library did support UMD/node build targets, but a packaging change when nunjucks 2.x was released made this difficult - as a result, I dropped node support. That said, there have been a few changes to nunjucks recently that may make this possible again.
from nunjucks-loader.
What is the exact problem, though? It seems that html-webpack-plugin
expects the template loader to return a template function, i.e.:
const template = require('./index.nunjucks')
template(context);
Instead of:
const template = require('./index.nunjucks')
template.render(context)
If that's the case, then perhaps users can use the exports loader somehow?
from nunjucks-loader.
That said, there have been a few changes to nunjucks recently that may make this possible again.
Still possible ?
from nunjucks-loader.
@at0g yes ? no ?
We have to make another template choice for this ?
Thank you
from nunjucks-loader.
@stephanedemotte that's a no for now
from nunjucks-loader.
Thanks ;)
from nunjucks-loader.
Thank you @superjose ! This was helpful but I still got a little issue here. I cannot manage to pass data from webpack to the templates. Even htmlWebpackPlugin.options
isn't available. Any hint or solution to accomplish this ?
I tried the following
new HtmlWebpackPlugin({
someVar: 'hey, I want to be available in the template',
filename: path.join(__dirname, '/' + page.filename),
template: 'nunjucks-html-loader!assets/templates/' + page.name + '.njk'
})
And defining global variables in my webpack.config.js
but nothing seems to do the trick.
EDIT: Nevermind, I just realized nunchucks-html-loader
was a static template loader. Seems like there are currently no way to dynamically use a extendable template engine with the html webpack plugin and I'm definitely not experienced enough to dig into this loader to make the said feature available
from nunjucks-loader.
@3dos Wait. Are you referring to using layouts on nunjucks?
from nunjucks-loader.
@superjose I finally tried another loader, details on stackoverflow
from nunjucks-loader.
Can gulp tasks related to nunjucks be somehow helpful? It is sad that we have no robust loader in webpack ecosystem...
from nunjucks-loader.
Have a look at #60
from nunjucks-loader.
Related Issues (20)
- 2.4.6 breaks webpack builds HOT 3
- Version 2.4.7 broke compatibility HOT 2
- webpack2 support HOT 1
- Doesn't work with extract-text-webpack-plugin
- opts key for query not working HOT 2
- DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic HOT 2
- Can't add custom filters and extensions
- -v HOT 1
- Is this usable to inject script tags ? HOT 1
- Configurable `autoescape` is still enabled in browser environment HOT 2
- Create 4x branch HOT 8
- Cannot read property 'context' of undefined HOT 2
- webpack 4.x release HOT 2
- Options are not being passed to browser environment
- Images handling in nunjucks template
- Load config file via webpack loaders HOT 3
- Can't get Nunjucks loader working in Vue JS HOT 7
- Relative paths are not being resolved HOT 2
- Inline svg loader returns [object Object] HOT 1
- Vulnerabilitie in loader-utils.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nunjucks-loader.