Skip to content

Instantly share code, notes, and snippets.

@izdrail
Created December 26, 2017 18:24
Show Gist options
  • Save izdrail/2082f4684b7401a4cde8ea9cc68970f6 to your computer and use it in GitHub Desktop.
Save izdrail/2082f4684b7401a4cde8ea9cc68970f6 to your computer and use it in GitHub Desktop.
Webpack 3 multiple nunjuck js templates
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
//
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
myOptions: { foo: 'bar' },
inject: 'body',
template: 'nunjucks-html-loader!./theme/templates/index.njk',
filename: 'index.html',
});
const HtmlWebpackPluginConfigGroups = new HtmlWebpackPlugin({
myOptions: { foo: 'bar' },
inject: 'body',
template: 'nunjucks-html-loader!./theme/templates/groups.njk',
filename: 'groups.html',
});
module.exports = {
entry: {
home: './assets/js/home.js',
groups: './assets/js/groups.js'
},
output: {
filename: 'production/[name].js',
},
module: {
rules: [
{
test: /\.js$/,
loaders: ['babel-loader']
},
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src']
}
}
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
{
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.
minimize: false,
minifyJS:true,
minifyCSS:true,
collapseWhitespace: false,
searchPaths: ['./theme/templates'],
root: path.resolve(__dirname, 'production'),
}
}]
},
{
test: /\.(gif|png|jpe?g|svg|webp)$/i,
loaders: [
'file-loader?hash=sha512&digest=hex&name=./production/static/[hash].[ext]',
'image-webpack-loader'
]
}
]
},
plugins: [
new ExtractTextPlugin('production/[name].css'),
new webpack.HotModuleReplacementPlugin(),
HtmlWebpackPluginConfig,
HtmlWebpackPluginConfigGroups
],
devServer: {
contentBase: [path.join(__dirname, "production"), path.join(__dirname, "assets")],
publicPath: "/",
compress: false,
port: 9000,
inline: false,
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment