diff --git a/examples/html-loader/webpack.config.js b/examples/html-loader/webpack.config.js index e31a36a..b34b7a1 100755 --- a/examples/html-loader/webpack.config.js +++ b/examples/html-loader/webpack.config.js @@ -10,14 +10,15 @@ module.exports = { module: { loaders: [ { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') }, - { test: /\.png$/, loader: 'file-loader' } + { test: /\.png$/, loader: 'file-loader' }, + { test: /\.html$/, loader: 'html-loader' }, ] }, plugins: [ new HtmlWebpackPlugin({ filename: 'html-loader.html', favicon: 'favicon.ico', - template: 'html!./template.html' + template: 'template.html' }), new ExtractTextPlugin('styles.css') ] diff --git a/examples/javascript/example.js b/examples/javascript/example.js new file mode 100644 index 0000000..8b8f606 --- /dev/null +++ b/examples/javascript/example.js @@ -0,0 +1,4 @@ +require('./main.css'); +var h1 = document.createElement('h1'); +h1.innerHTML = 'Hello world'; +document.body.appendChild(h1); \ No newline at end of file diff --git a/examples/javascript/logo.png b/examples/javascript/logo.png new file mode 100644 index 0000000..d71b3d7 Binary files /dev/null and b/examples/javascript/logo.png differ diff --git a/examples/javascript/main.css b/examples/javascript/main.css new file mode 100644 index 0000000..232a2cd --- /dev/null +++ b/examples/javascript/main.css @@ -0,0 +1,3 @@ +body { + background: snow; +} \ No newline at end of file diff --git a/examples/javascript/partial.html b/examples/javascript/partial.html new file mode 100644 index 0000000..47dc0f7 --- /dev/null +++ b/examples/javascript/partial.html @@ -0,0 +1,2 @@ +

Partial

+ \ No newline at end of file diff --git a/examples/javascript/template.js b/examples/javascript/template.js new file mode 100644 index 0000000..0dbe82a --- /dev/null +++ b/examples/javascript/template.js @@ -0,0 +1,5 @@ +// Webpack require: +var partial = require('./partial.html'); + +// Export a function / promise / or a string: +module.exports = '' + new Date() + partial + ''; diff --git a/examples/javascript/webpack.config.js b/examples/javascript/webpack.config.js new file mode 100644 index 0000000..8df7eec --- /dev/null +++ b/examples/javascript/webpack.config.js @@ -0,0 +1,23 @@ +var HtmlWebpackPlugin = require('../..'); +var ExtractTextPlugin = require('extract-text-webpack-plugin'); +module.exports = { + entry: './example.js', + output: { + path: __dirname + '/dist', + publicPath: '', + filename: 'bundle.js' + }, + module: { + loaders: [ + { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') }, + { test: /\.png$/, loader: 'file-loader' }, + { test: /\.html$/, loader: 'html-loader' } + ] + }, + plugins: [ + new HtmlWebpackPlugin({ + template: 'template.js' + }), + new ExtractTextPlugin('styles.css') + ] +}; \ No newline at end of file diff --git a/examples/two-loaders/two-loaders/example.js b/examples/two-loaders/two-loaders/example.js new file mode 100644 index 0000000..13a1ca1 --- /dev/null +++ b/examples/two-loaders/two-loaders/example.js @@ -0,0 +1,4 @@ +require('./main.css'); +var h1 = document.createElement('h1'); +h1.innerHTML = 'Hello world!'; +document.body.appendChild(h1); \ No newline at end of file diff --git a/examples/two-loaders/two-loaders/logo.png b/examples/two-loaders/two-loaders/logo.png new file mode 100644 index 0000000..d71b3d7 Binary files /dev/null and b/examples/two-loaders/two-loaders/logo.png differ diff --git a/examples/two-loaders/two-loaders/main.css b/examples/two-loaders/two-loaders/main.css new file mode 100644 index 0000000..232a2cd --- /dev/null +++ b/examples/two-loaders/two-loaders/main.css @@ -0,0 +1,3 @@ +body { + background: snow; +} \ No newline at end of file diff --git a/examples/two-loaders/two-loaders/partial.html b/examples/two-loaders/two-loaders/partial.html new file mode 100644 index 0000000..47dc0f7 --- /dev/null +++ b/examples/two-loaders/two-loaders/partial.html @@ -0,0 +1,2 @@ +

Partial

+ \ No newline at end of file diff --git a/examples/two-loaders/two-loaders/template.html b/examples/two-loaders/two-loaders/template.html new file mode 100644 index 0000000..edb44d6 --- /dev/null +++ b/examples/two-loaders/two-loaders/template.html @@ -0,0 +1,11 @@ + + + + + Example template + + + + <%= require('html!./partial.html') %> + + \ No newline at end of file diff --git a/examples/two-loaders/two-loaders/webpack.config.js b/examples/two-loaders/two-loaders/webpack.config.js new file mode 100644 index 0000000..7df9d54 --- /dev/null +++ b/examples/two-loaders/two-loaders/webpack.config.js @@ -0,0 +1,22 @@ +var HtmlWebpackPlugin = require('../..'); +var ExtractTextPlugin = require('extract-text-webpack-plugin'); +module.exports = { + entry: './example.js', + output: { + path: __dirname + '/dist', + publicPath: '', + filename: 'bundle.js' + }, + module: { + loaders: [ + { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') }, + { test: /\.png$/, loader: 'file-loader' } + ] + }, + plugins: [ + new HtmlWebpackPlugin({ + template: 'template.html' + }), + new ExtractTextPlugin('styles.css') + ] +}; \ No newline at end of file diff --git a/index.js b/index.js index f6059c0..49f45b1 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ function HtmlWebpackPlugin(options) { compile: true, favicon: false, minify: false, + cache: true, chunks: 'all', excludeChunks: [], title: 'Webpack App' @@ -84,7 +85,10 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { } // Once everything is compiled evaluate the html factory // and replace it with its content - return self.evaluateCompilationResult(compilation, resultAsset); + if (self.built || !self.options.cache) { + self.evaluatedCompilationResult = self.evaluateCompilationResult(compilation, resultAsset); + } + return self.evaluatedCompilationResult; }) // Execute the template .then(function(compilationResult) { @@ -153,15 +157,6 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename, new webpack.DefinePlugin({ HTML_WEBPACK_PLUGIN : 'true' }) ); - // Create a subCache (copied from https://github.com/SanderSpies/extract-text-webpack-plugin/blob/master/loader.js) - childCompiler.plugin('compilation', function(compilation) { - if(compilation.cache) { - if(!compilation.cache[compilerName]) { - compilation.cache[compilerName] = {}; - } - compilation.cache = compilation.cache[compilerName]; - } - }); // Compile and return a promise return new Promise(function (resolve, reject) { childCompiler.runAsChild(function(err, entries, childCompilation) { @@ -173,10 +168,12 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename, reject('Child compilation failed:\n' + errorDetails); } else { - resolve(compilation.assets[outputFilename]); + this.built = this.hash !== entries[0].hash; + this.hash = entries[0].hash; + resolve(childCompilation.assets[outputFilename]); } - }); - }); + }.bind(this)); + }.bind(this)); }; /** @@ -187,6 +184,7 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, co if(!compilationResult) { return Promise.reject('The child compilation didn\'t provide a result'); } + var source = compilationResult.source(); // The LibraryTemplatePlugin stores the template result in a local variable. // To extract the result during the evaluation this part has to be removed. @@ -247,6 +245,9 @@ HtmlWebpackPlugin.prototype.executeTemplate = function(templateFunction, chunks, */ HtmlWebpackPlugin.prototype.postProcessHtml = function(html, assets) { var self = this; + if (typeof html !== 'string') { + return Promise.reject('Expected html to be a string but got ' + JSON.stringify(html)); + } return Promise.resolve() // Inject .then(function() { @@ -296,7 +297,7 @@ HtmlWebpackPlugin.prototype.addFileToAssets = function(filename, compilation) { * Helper to sort chunks */ HtmlWebpackPlugin.prototype.sortChunks = function(chunks, sortMode) { - // Sort mode auto by default: + // Sort mode auto by default: if (typeof sortMode === 'undefined' || sortMode === 'auto') { return chunks.sort(function orderEntryLast(a, b) { if (a.entry !== b.entry) { diff --git a/loader.js b/loader.js index 778e1f8..9932354 100644 --- a/loader.js +++ b/loader.js @@ -14,6 +14,10 @@ module.exports = function (source) { if (allLoadersButThisOne.length > 0) { return source; } + // Skip .js files + if (/\.js$/.test(this.request)) { + return source; + } // Use underscore for a minimalistic loader var options = loaderUtils.parseQuery(this.query); var template = _.template(source, options);