diff --git a/example/example.js b/example/example.js index 80372b0..29eb396 100755 --- a/example/example.js +++ b/example/example.js @@ -1 +1,2 @@ +require('./main.css'); document.body.innerHTML = 'Hello world!'; \ No newline at end of file diff --git a/example/logo.png b/example/logo.png new file mode 100644 index 0000000..d71b3d7 Binary files /dev/null and b/example/logo.png differ diff --git a/example/main.css b/example/main.css new file mode 100644 index 0000000..232a2cd --- /dev/null +++ b/example/main.css @@ -0,0 +1,3 @@ +body { + background: snow; +} \ No newline at end of file diff --git a/example/template.html b/example/template.html new file mode 100644 index 0000000..b7b4f97 --- /dev/null +++ b/example/template.html @@ -0,0 +1,11 @@ + + + + + Example template + + + + + + \ No newline at end of file diff --git a/example/webpack.config.js b/example/webpack.config.js index c3056a5..68ebf0a 100755 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -1,14 +1,28 @@ var HtmlWebpackPlugin = require('..'); +var ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { entry: './example.js', output: { - path: __dirname + "/dist", - publicPath: '/', - filename: "bundle.js" + 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(), new HtmlWebpackPlugin({ + filename: 'html-loader.html', + template: 'html!./template.html' + }), + new HtmlWebpackPlugin({ + title: 'HtmlWebpackPlugin example', favicon: 'favicon.ico', + filename: 'index.min.html', minify: { removeComments: true, collapseWhitespace: true, @@ -16,6 +30,7 @@ module.exports = { minifyJS: true, minifyCSS: true } - }) + }), + new ExtractTextPlugin('styles.css') ] }; \ No newline at end of file diff --git a/index.js b/index.js index fa7c8d5..e50443c 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,12 @@ function HtmlWebpackPlugin(options) { if(this.options.template.indexOf('!') === -1) { this.options.template = 'raw!' + this.options.template; } + // Resolve tempalte path + this.options.template = this.options.template.replace( + /(\!)(\.+\/[^\!\?]+)($|\?.+$)/, + function(match, prefix, filepath, postfix) { + return prefix + path.resolve(filepath) + postfix; + }); } HtmlWebpackPlugin.prototype.apply = function(compiler) { @@ -47,7 +53,9 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { // Create an additional child compiler which takes the template // and turns it into an Node.JS html factory. // This allows us to use loaders during the compilation - var childCompiler = compilation.createChildCompiler('html-webpack-plugin', outputOptions); + var compilerName = 'html-webpack-plugin for "' + path.basename(outputOptions.filename) + '"'; + var childCompiler = compilation.createChildCompiler(compilerName, outputOptions); + // Inherit the loader configuration childCompiler.apply(new NodeTemplatePlugin(outputOptions)); childCompiler.apply(new LibraryTemplatePlugin('result', 'var')); childCompiler.apply(new NodeTargetPlugin()); @@ -106,7 +114,12 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilationResu // Strip the leading 'var ' var source = compilationResult.source().substr(3); // Evaluate code and cast to string - var newSource = vm.runInThisContext(source); + var newSource; + try { + newSource = vm.runInThisContext(source); + } catch (e) { + return Promise.reject(e); + } return typeof newSource === 'string' ? Promise.resolve(newSource) : Promise.reject('The loader "' + this.options.template + '" didn\'t return html.'); diff --git a/package.json b/package.json index fc97e49..1750f30 100644 --- a/package.json +++ b/package.json @@ -29,15 +29,16 @@ }, "homepage": "https://github.com/ampedandwired/html-webpack-plugin", "devDependencies": { - "css-loader": "^0.15.4", - "extract-text-webpack-plugin": "^0.8.2", - "file-loader": "^0.8.4", - "jasmine-node": "^2.0.0", - "jshint": "^2.8.0", - "rimraf": "^2.4.1", - "style-loader": "^0.12.3", - "url-loader": "^0.5.6", - "webpack": "^1.10.1" + "css-loader": "^0.12.0", + "extract-text-webpack-plugin": "^0.7.1", + "file-loader": "^0.8.1", + "html-loader": "^0.3.0", + "jasmine-node": "^1.14.5", + "jshint": "^2.7.0", + "rimraf": "^2.3.3", + "style-loader": "^0.12.2", + "url-loader": "^0.5.5", + "webpack": "^1.8.11" }, "dependencies": { "bluebird": "^2.9.34", diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index ccbe3bd..d11e886 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -83,20 +83,6 @@ describe('HtmlWebpackPlugin', function() { ['