<commit_before>var Twig = require("twig");
var _ = require("underscore");
var path = require("path");
var loaderUtils = require("loader-utils");

Twig.extend(function(Twig) {
    var compiler = Twig.compiler;

    compiler.module['webpack'] = function(id, tokens, pathToTwig) {
        id = path.basename(id, ".twig");
        var includes = [];

        var processToken = function(token) {
            if (token.type == "logic" && token.token.type) {
                switch(token.token.type) {
                    case 'Twig.logic.type.block':
                    case 'Twig.logic.type.if':
                    case 'Twig.logic.type.elseif':
                    case 'Twig.logic.type.else':
                    case 'Twig.logic.type.for':
                    case 'Twig.logic.type.spaceless':
                        _.each(token.token.output, processToken);
                        break;
                    case 'Twig.logic.type.extends':
                        _.each(token.token.stack, function(token) {
                            includes.push("twig!" + token.value + ".twig");
                        });
                        break;
                    case 'Twig.logic.type.import':
                        if (token.token.expression != '_self') {
                            throw new Error("Twig-loader: Importing macro's is not yet supported");
                        }
                        break;
                    case 'Twig.logic.type.include':
                        _.each(token.token.stack, function(token) {
                            includes.push("twig!" + token.value + ".twig");
                        });
                        break;
                }
            }
        };

        _.each(JSON.parse(tokens), processToken);

        var output = [
            'var twig = require("' + pathToTwig + '").twig,',
            '    template = twig({id:"' + id + '", data:' + tokens + ', allowInlineIncludes: true});\n',
            'module.exports = function(context) { return template.render(context); }'
        ];

        if (includes.length > 0) {
            _.each(includes, function(file) {
                output.unshift("require("+ JSON.stringify(file) +");\n");
            });
        }

        return output.join('\n');
    };
});

module.exports = function(source) {
    var id = this.resource,
        tpl;

    this.cacheable && this.cacheable();

    // check if template already exists
    tpl = Twig.twig({ ref: id });
    if (!tpl) {
        tpl = Twig.twig({
            id: id,
            data: source,
            allowInlineIncludes: true
        });
    }

    tpl = tpl.compile({
        module: 'webpack',
        twig: 'twig'
    });

    this.callback(null, tpl);
};
<commit_msg>Try to resolve include (etc) statements so the absolute path is used as template reference. Hopefully this solve #3.<commit_after>var Twig = require("twig");
var _ = require("underscore");
var path = require("path");
var loaderUtils = require("loader-utils");

Twig.extend(function(Twig) {
    var compiler = Twig.compiler;

    compiler.module['webpack'] = function(id, tokens, pathToTwig) {
        var includes = [];
        var processDependency = function(token) {
            includes.push(token.value);
            token.value = path.resolve(path.dirname(id), token.value);
        };

        var processToken = function(token) {
            if (token.type == "logic" && token.token.type) {
                switch(token.token.type) {
                    case 'Twig.logic.type.block':
                    case 'Twig.logic.type.if':
                    case 'Twig.logic.type.elseif':
                    case 'Twig.logic.type.else':
                    case 'Twig.logic.type.for':
                    case 'Twig.logic.type.spaceless':
                        _.each(token.token.output, processToken);
                        break;
                    case 'Twig.logic.type.extends':
                        _.each(token.token.stack, processDependency);
                        break;
                    case 'Twig.logic.type.import':
                        if (token.token.expression != '_self') {
                            throw new Error("Twig-loader: Importing macro's is not yet supported");
                        }
                        break;
                    case 'Twig.logic.type.include':
                        _.each(token.token.stack, processDependency);
                        break;
                }
            }
        };

        var parsedTokens = JSON.parse(tokens);

        _.each(parsedTokens, processToken);

        var output = [
            'var twig = require("' + pathToTwig + '").twig,',
            '    template = twig({id:' + JSON.stringify(id) + ', data:' + JSON.stringify(parsedTokens) + ', allowInlineIncludes: true});\n',
            'module.exports = function(context) { return template.render(context); }'
        ];

        if (includes.length > 0) {
            _.each(includes, function(file) {
                output.unshift("require("+ JSON.stringify("twig!" + file) +");\n");
            });
        }

        return output.join('\n');
    };
});

module.exports = function(source) {
    var id = require.resolve(this.resource),
        tpl;
    this.cacheable && this.cacheable();

    // check if template already exists
    tpl = Twig.twig({ ref: id });
    if (!tpl) {
        tpl = Twig.twig({
            id: id,
            data: source,
            allowInlineIncludes: true
        });
    }

    tpl = tpl.compile({
        module: 'webpack',
        twig: 'twig'
    });

    this.callback(null, tpl);
};

{'fraction_non_alphanumeric': 0.13684771033013846, 'fraction_numerical': 0.0005324813631522897, 'mean_word_length': 1.4510656807307525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '429368', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1514, 'n_words': 306}