ryanhornberger / nunjucks-html-loader

Webpack Loader for Nunjucks that returns raw html. Also works with Webpack watching system.
MIT License
27 stars 23 forks source link

Support Webpack 2 #7

Closed romanmarkelov closed 7 years ago

romanmarkelov commented 7 years ago

Hi! In webpack2 I get error precompiledContext is not defined Show example using the loader

jeremyzahner commented 7 years ago

@webmarkelov Hi there. I did not yet test the loader in Webpack 2. I might do that in the next few days since we are thinking about migrating our stacks anyway.

alexalv commented 7 years ago

Hi! First of all, my case certainly does not address the problem mentioned in the first comment, but maybe it would be helpful. I am currently using nunjucks-html-loader for one of the projects, although the usage is not extensive (I am using it for includes mostly). I've upgraded webpack to 2.2.1 and all I had to change in regards to nunjucks-html-loader was add -loader postfix to file and nunjucks-html. Basically made this

{
    test: /\.html$/,
    loader: 'file?name=[path][name].html!nunjucks-html?' +
                JSON.stringify({
                    'searchPaths': [
                        'path/to/includes/'
                    ]
                })
}

into this

{
    test: /\.html$/,
    loader: 'file-loader?name=[path][name].html!nunjucks-html-loader?' +
                JSON.stringify({
                    'searchPaths': [
                        'path/to/includes/'
                    ]
                })
}

and all worked perfectly.

jeremyzahner commented 7 years ago

@alexalv Thanks! I'll update the documentation with this asap.

romanmarkelov commented 7 years ago

@alexalv I tried but not work :( Can i see example?

jeremyzahner commented 7 years ago

@webmarkelov Could you post your webpack2 config?

romanmarkelov commented 7 years ago

My mistake! work perfectly with webpack version 2.2.1

example:

// webpack.config.js
const path = require('path');
const webpack = require("webpack");

module.exports = {
    entry: './assets/app.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },

    module: {
      rules: [{
          test: /\.njk$/,
          loader: 'file-loader?name=[path][name].html!nunjucks-html-loader?' +
          JSON.stringify({
              'searchPaths': [
                  path.resolve(__dirname, 'assets/views')
              ]
          })
      }]
    }
};
// app.js
require.context("./views/pages/", true, /^\.\/.*\.njk$/);
romanmarkelov commented 7 years ago

Improve:

// webpack.config.js
const path = require('path');
const webpack = require("webpack");

module.exports = {
    entry: './assets/app.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },

    module: {
        rules: [{
            test: /\.njk$/,
            use: [
                {
                    loader: 'file-loader',
                    options: {
                        name: '[path][name].html'
                    }
                },
                {
                    loader: 'nunjucks-html-loader',
                    options: {
                        searchPaths: path.resolve(__dirname, 'assets/views')
                    }
                }
            ]
        }]
    },
};
jeremyzahner commented 7 years ago

@webmarkelov Thanks!