diff --git a/examples/javascript-advanced/dist/0714810ae3fb211173e2964249507195.png b/examples/javascript-advanced/dist/0714810ae3fb211173e2964249507195.png new file mode 100644 index 0000000..d71b3d7 Binary files /dev/null and b/examples/javascript-advanced/dist/0714810ae3fb211173e2964249507195.png differ diff --git a/examples/javascript-advanced/dist/bundle.js b/examples/javascript-advanced/dist/bundle.js new file mode 100644 index 0000000..29aa454 --- /dev/null +++ b/examples/javascript-advanced/dist/bundle.js @@ -0,0 +1,65 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; + +/******/ // The require function +/******/ function __webpack_require__(moduleId) { + +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) +/******/ return installedModules[moduleId].exports; + +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ exports: {}, +/******/ id: moduleId, +/******/ loaded: false +/******/ }; + +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); + +/******/ // Flag the module as loaded +/******/ module.loaded = true; + +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } + + +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; + +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; + +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; + +/******/ // Load entry module and return exports +/******/ return __webpack_require__(0); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ function(module, exports, __webpack_require__) { + + eval("__webpack_require__(1);\n\nvar universal = __webpack_require__(5);\nvar h1 = document.createElement('h1');\nh1.innerHTML = universal();\n\ndocument.body.appendChild(h1);\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./example.js\n ** module id = 0\n ** module chunks = 0\n **/\n//# sourceURL=webpack:///./example.js?"); + +/***/ }, +/* 1 */ +/***/ function(module, exports) { + + eval("// removed by extract-text-webpack-plugin\n\n/*****************\n ** WEBPACK FOOTER\n ** ./main.css\n ** module id = 1\n ** module chunks = 0\n **/\n//# sourceURL=webpack:///./main.css?"); + +/***/ }, +/* 2 */, +/* 3 */, +/* 4 */, +/* 5 */ +/***/ function(module, exports) { + + eval("// This file is used for frontend and backend\n'use strict';\n\n// If compiled by the html-webpack-plugin\n// HTML_WEBPACK_PLUGIN is set to true:\nvar backend = typeof HTML_WEBPACK_PLUGIN !== 'undefined';\n\nmodule.exports = function () {\n return 'Hello World from ' + (backend ? 'backend' : 'frontend');\n};\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./universial.js\n ** module id = 5\n ** module chunks = 0\n **/\n//# sourceURL=webpack:///./universial.js?"); + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/examples/javascript-advanced/dist/index.html b/examples/javascript-advanced/dist/index.html new file mode 100644 index 0000000..194f6fb --- /dev/null +++ b/examples/javascript-advanced/dist/index.html @@ -0,0 +1,2 @@ +Webpack AppHello World from backend -

Partial

+ \ No newline at end of file diff --git a/examples/javascript-advanced/dist/styles.css b/examples/javascript-advanced/dist/styles.css new file mode 100644 index 0000000..232a2cd --- /dev/null +++ b/examples/javascript-advanced/dist/styles.css @@ -0,0 +1,3 @@ +body { + background: snow; +} \ No newline at end of file diff --git a/examples/javascript-advanced/example.js b/examples/javascript-advanced/example.js new file mode 100644 index 0000000..e5aff3b --- /dev/null +++ b/examples/javascript-advanced/example.js @@ -0,0 +1,7 @@ +require('./main.css'); + +var universal = require('./universial.js'); +var h1 = document.createElement('h1'); +h1.innerHTML = universal(); + +document.body.appendChild(h1); diff --git a/examples/javascript-advanced/logo.png b/examples/javascript-advanced/logo.png new file mode 100644 index 0000000..d71b3d7 Binary files /dev/null and b/examples/javascript-advanced/logo.png differ diff --git a/examples/javascript-advanced/main.css b/examples/javascript-advanced/main.css new file mode 100644 index 0000000..232a2cd --- /dev/null +++ b/examples/javascript-advanced/main.css @@ -0,0 +1,3 @@ +body { + background: snow; +} \ No newline at end of file diff --git a/examples/javascript-advanced/partial.html b/examples/javascript-advanced/partial.html new file mode 100644 index 0000000..47dc0f7 --- /dev/null +++ b/examples/javascript-advanced/partial.html @@ -0,0 +1,2 @@ +

Partial

+ \ No newline at end of file diff --git a/examples/javascript-advanced/readme.md b/examples/javascript-advanced/readme.md new file mode 100644 index 0000000..64d224d --- /dev/null +++ b/examples/javascript-advanced/readme.md @@ -0,0 +1,4 @@ +# isomorphic javascript-advanced example + +This example is similar to the javascript example however it allows takes +parameters from the config and works asynchronously \ No newline at end of file diff --git a/examples/javascript-advanced/template.js b/examples/javascript-advanced/template.js new file mode 100644 index 0000000..bd0f705 --- /dev/null +++ b/examples/javascript-advanced/template.js @@ -0,0 +1,13 @@ +// Webpack require: +var partial = require('./partial.html'); +var universal = require('./universial.js'); + +// Export a function / promise / or a string: +// This function has to return a string or promised string: +module.exports = function (templateParams) { + var html = '' + + '' + templateParams.htmlWebpackPlugin.options.title + '' + + '' + universal() + ' - ' + partial + ''; + + return html; +}; diff --git a/examples/javascript-advanced/universial.js b/examples/javascript-advanced/universial.js new file mode 100644 index 0000000..e63d688 --- /dev/null +++ b/examples/javascript-advanced/universial.js @@ -0,0 +1,10 @@ +// This file is used for frontend and backend +'use strict'; + +// If compiled by the html-webpack-plugin +// HTML_WEBPACK_PLUGIN is set to true: +var backend = typeof HTML_WEBPACK_PLUGIN !== 'undefined'; + +module.exports = function () { + return 'Hello World from ' + (backend ? 'backend' : 'frontend'); +}; diff --git a/examples/javascript-advanced/webpack.config.js b/examples/javascript-advanced/webpack.config.js new file mode 100644 index 0000000..e0d314e --- /dev/null +++ b/examples/javascript-advanced/webpack.config.js @@ -0,0 +1,24 @@ +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' } + ] + }, + devtool: 'eval', + plugins: [ + new HtmlWebpackPlugin({ + template: 'template.js' + }), + new ExtractTextPlugin('styles.css') + ] +}; diff --git a/index.js b/index.js index edebe37..2217c3e 100644 --- a/index.js +++ b/index.js @@ -174,11 +174,13 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function (compilation, s // The LibraryTemplatePlugin stores the template result in a local variable. // To extract the result during the evaluation this part has to be removed. source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', ''); - + var template = this.options.template.replace(/^.+!/, '').replace(/\?.+$/, ''); + var vmContext = vm.createContext(_.extend({HTML_WEBPACK_PLUGIN: true, require: require}, global)); + var vmScript = new vm.Script(source, {filename: template}); // Evaluate code and cast to string var newSource; try { - newSource = vm.runInNewContext(source, {HTML_WEBPACK_PLUGIN: true}, {filename: 'html-plugin-evaluation'}); + newSource = vmScript.runInContext(vmContext); } catch (e) { return Promise.reject(e); } diff --git a/package.json b/package.json index 5cb05b6..d9cc045 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-webpack-plugin", - "version": "2.7.1", + "version": "2.7.2", "description": "Simplifies creation of HTML files to serve your webpack bundles", "main": "index.js", "files": [ diff --git a/spec/ExampleSpec.js b/spec/ExampleSpec.js index 82840ac..7588020 100644 --- a/spec/ExampleSpec.js +++ b/spec/ExampleSpec.js @@ -67,4 +67,8 @@ describe('HtmlWebpackPlugin Examples', function () { it('javascript example', function (done) { runExample('javascript', done); }); + + it('javascript-advanced example', function (done) { + runExample('javascript-advanced', done); + }); });