From e566dac40d2aa278657bbdc1293ebddc90762f73 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Mon, 18 Jan 2016 14:47:54 +0100 Subject: [PATCH] Fix context --- index.js | 26 ++++++++++++++++---------- lib/compiler.js | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index ed43a8d..8bd1c19 100644 --- a/index.js +++ b/index.js @@ -24,16 +24,6 @@ function HtmlWebpackPlugin(options) { excludeChunks: [], title: 'Webpack App' }, options); - // If the template doesn't use a loader use the lodash template loader - if(this.options.template.indexOf('!') === -1) { - this.options.template = require.resolve('./lib/loader.js') + '!' + path.resolve(this.options.template); - } - // Resolve template path - this.options.template = this.options.template.replace( - /(\!)([^\/\\][^\!\?]+|[^\/\\!?])($|\?.+$)/, - function(match, prefix, filepath, postfix) { - return prefix + path.resolve(filepath) + postfix; - }); } HtmlWebpackPlugin.prototype.apply = function(compiler) { @@ -41,6 +31,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { var isCompilationCached = false; var compilationPromise; + this.options.template = this.getFullTemplatePath(this.options.template, compiler.context); + compiler.plugin('make', function(compilation, callback) { // Compile the template (queued) compilationPromise = getNextCompilationSlot(compiler, function() { @@ -258,6 +250,7 @@ HtmlWebpackPlugin.prototype.postProcessHtml = function(html, assets) { * Pushes the content of the given filename to the compilation assets */ HtmlWebpackPlugin.prototype.addFileToAssets = function(filename, compilation) { + filename = path.resolve(compilation.compiler.context, filename); return Promise.props({ size: fs.statAsync(filename), source: fs.readFileAsync(filename) @@ -492,6 +485,19 @@ HtmlWebpackPlugin.prototype.appendHash = function (url, hash) { return url + (url.indexOf('?') === -1 ? '?' : '&') + hash; }; +HtmlWebpackPlugin.prototype.getFullTemplatePath = function(template, context) { + // If the template doesn't use a loader use the lodash template loader + if(template.indexOf('!') === -1) { + template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template); + } + // Resolve template path + return template.replace( + /(\!)([^\/\\][^\!\?]+|[^\/\\!?])($|\?.+$)/, + function(match, prefix, filepath, postfix) { + return prefix + path.resolve(filepath) + postfix; + }); +}; + /** * Helper to prevent html-plugin compilation in parallel * Fixes "No source available" where incomplete cache modules were used diff --git a/lib/compiler.js b/lib/compiler.js index 4e2c5b3..55378e9 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -7,7 +7,6 @@ 'use strict'; var Promise = require('bluebird'); var path = require('path'); -var webpack = require('webpack'); var NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin'); var NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin'); var LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin'); @@ -43,6 +42,7 @@ module.exports.compileTemplate = function compileTemplate(template, context, out // This allows us to use loaders during the compilation var compilerName = getCompilerName(context, outputFilename); var childCompiler = compilation.createChildCompiler(compilerName, outputOptions); + childCompiler.context = context; childCompiler.apply( new NodeTemplatePlugin(outputOptions), new NodeTargetPlugin(),