From 387ca2448ac87e28b166aa7bd9ce72f9f0e10e8f Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Mon, 29 Jun 2015 16:52:50 +0200 Subject: [PATCH] Use lodash to solve dependency issues for the fallback loader --- README.md | 8 ++++---- default_index.html | 2 +- index.js | 2 +- loader.js | 20 ++++++++++++++++++++ package.json | 2 +- spec/fixtures/webpackconfig.html | 4 ++-- 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 loader.js diff --git a/README.md b/README.md index d594cf9..189a1cb 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ HTML Webpack Plugin This is a [webpack](http://webpack.github.io/) plugin that simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation. You can either let the plugin generate an HTML file for you or supply -your own template (using [blueimp templates](https://github.com/blueimp/JavaScript-Templates)). +your own template (using lodash/ejs templates. Installation ------------ @@ -70,7 +70,7 @@ Allowed values are as follows: - `hash`: `true | false` if `true` then append a unique webpack compilation hash to all included scripts and css files. This is useful for cache busting. - `chunks`: Allows you to add only some chunks (e.g. only the unit-test chunk) -- `excludeChunks`: Allows you to skip some chunks (e.g. don't add the unit-test chunk) +- `excludeChunks`: Allows you to skip some chunks (e.g. don't add the unit-test chunk) Here's an example webpack config illustrating how to use these options: ```javascript @@ -121,7 +121,7 @@ and favicon files into the markup. ```javascript plugins: [ new HtmlWebpackPlugin({ - title: 'Custom template', + title: 'Custom template', template: 'my-index.html', // Load a custom template inject: 'body' // Inject all scripts into the body }) @@ -153,7 +153,7 @@ plugins: [ ] ``` -You can use the [blueimp template](https://github.com/blueimp/JavaScript-Templates) syntax out of the box. +You can use the lodash/ejs syntax out of the box. If the `inject` feature doesn't fit your needs and you want full control over the asset placement use the [default template](https://github.com/ampedandwired/html-webpack-plugin/blob/master/default_index.html) as a starting point for writing your own. diff --git a/default_index.html b/default_index.html index 5ba181b..f9466a4 100644 --- a/default_index.html +++ b/default_index.html @@ -2,7 +2,7 @@ - {%=o.htmlWebpackPlugin.options.title %} + <%= htmlWebpackPlugin.options.title %> diff --git a/index.js b/index.js index c599434..448ee3d 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ function HtmlWebpackPlugin(options) { }, options); // If the template doesn't use a loader use the blueimp template loader if(this.options.template.indexOf('!') === -1) { - this.options.template = 'blueimp-tmpl!' + path.resolve(this.options.template); + this.options.template = require.resolve('./loader.js') + '!' + path.resolve(this.options.template); } // Resolve template path this.options.template = this.options.template.replace( diff --git a/loader.js b/loader.js new file mode 100644 index 0000000..64fc5ab --- /dev/null +++ b/loader.js @@ -0,0 +1,20 @@ +var _ = require('lodash'); +var loaderUtils = require('loader-utils'); + +module.exports = function (source) { + 'use strict'; + var allLoadersButThisOne = this.loaders.filter(function(loader) { + return loader.module !== module.exports; + }); + // This loader shouldn't kick in if there is any other loader + if (allLoadersButThisOne.length > 0) { + return source; + } + // Use underscore for a minimalistic loader + if (this.cacheable) { + this.cacheable(); + } + var options = loaderUtils.parseQuery(this.query); + var template = _.template(source, options); + return 'module.exports = ' + template; +}; \ No newline at end of file diff --git a/package.json b/package.json index 8fdbd8f..eb790e5 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ }, "dependencies": { "bluebird": "^2.9.25", - "blueimp-tmpl": "~2.5.4", + "blueimp-tmpl": "^2.5.4", "html-minifier": "^0.7.2", "lodash": "~3.9.3" } diff --git a/spec/fixtures/webpackconfig.html b/spec/fixtures/webpackconfig.html index 122c9b5..8bc4abe 100644 --- a/spec/fixtures/webpackconfig.html +++ b/spec/fixtures/webpackconfig.html @@ -5,7 +5,7 @@ Test -

Public path is {%=o.webpackConfig.output.publicPath%}

- +

Public path is <%= webpackConfig.output.publicPath %>

+