Socket
Socket
Sign inDemoInstall

html-webpack-plugin

Package Overview
Dependencies
76
Maintainers
2
Versions
138
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.5.0 to 2.6.0

115

index.js

@@ -7,11 +7,6 @@ 'use strict';

var path = require('path');
var childCompiler = require('./lib/compiler.js');
var prettyError = require('./lib/errors.js');
Promise.promisifyAll(fs);
var webpack = require('webpack');
var NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
var NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
var LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
var LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
var SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
function HtmlWebpackPlugin(options) {

@@ -28,2 +23,3 @@ // Default options

cache: true,
showErrors: true,
chunks: 'all',

@@ -35,3 +31,3 @@ excludeChunks: [],

if(this.options.template.indexOf('!') === -1) {
this.options.template = require.resolve('./loader.js') + '!' + path.resolve(this.options.template);
this.options.template = require.resolve('./lib/loader.js') + '!' + path.resolve(this.options.template);
}

@@ -48,4 +44,4 @@ // Resolve template path

var self = this;
var isCompilationCached = false;
var compilationPromise;
self.context = compiler.context;

@@ -55,7 +51,16 @@ compiler.plugin('make', function(compilation, callback) {

compilationPromise = getNextCompilationSlot(compiler, function() {
return self.compileTemplate(self.options.template, self.options.filename, compilation)
return childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation)
.catch(function(err) {
return new Error(err);
compilation.errors.push(prettyError(err, compiler.context).toString());
return {
content: self.options.showErrors ? prettyError(err, compiler.context).toJsonHtml() : 'ERROR',
};
})
.finally(callback);
.then(function(compilationResult) {
// If the compilation change didnt change the cache is valid
isCompilationCached = compilationResult.hash && self.hash === compilationResult.hash;
self.hash = compilation.hash;
callback();
return compilationResult.content;
});
});

@@ -81,3 +86,3 @@ });

var assetJson = JSON.stringify(assets);
if (self.options.cache && !self.built && assetJson === self.assetJson) {
if (isCompilationCached && self.options.cache && assetJson === self.assetJson) {
return callback();

@@ -103,5 +108,2 @@ } else {

.then(function(compiledTemplate) {
if (compiledTemplate instanceof Error) {
return Promise.reject(compiledTemplate);
}
// Allow to use a custom function / string instead

@@ -145,5 +147,4 @@ if (self.options.templateContent) {

// with the error message and an error is logged
var errorMessage = "HtmlWebpackPlugin " + err;
compilation.errors.push(new Error(errorMessage));
return errorMessage;
compilation.errors.push(prettyError(err, compiler.context).toString());
return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
})

@@ -180,69 +181,10 @@ .then(function(html) {

/**
* Returns the child compiler name
*/
HtmlWebpackPlugin.prototype.getCompilerName = function() {
var absolutePath = path.resolve(this.context, this.options.filename);
var relativePath = path.relative(this.context, absolutePath);
return 'html-webpack-plugin for "' + (absolutePath.length < relativePath.length ? absolutePath : relativePath) + '"';
};
/**
* Compiles the template into a nodejs factory, adds its to the compilation.assets
* and returns a promise of the result asset object.
*/
HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename, compilation) {
// The entry file is just an empty helper as the dynamic template
// require is added in "loader.js"
var outputOptions = {
filename: outputFilename,
publicPath: compilation.outputOptions.publicPath
};
var cachedAsset = compilation.assets[outputOptions.filename];
// 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 compilerName = this.getCompilerName();
var childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
childCompiler.apply(
new NodeTemplatePlugin(outputOptions),
new NodeTargetPlugin(),
new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var'),
new SingleEntryPlugin(this.context, template),
new LoaderTargetPlugin('node'),
new webpack.DefinePlugin({ HTML_WEBPACK_PLUGIN : 'true' })
);
// Compile and return a promise
return new Promise(function (resolve, reject) {
childCompiler.runAsChild(function(err, entries, childCompilation) {
compilation.assets[outputOptions.filename] = cachedAsset;
if (cachedAsset === undefined) {
delete compilation.assets[outputOptions.filename];
}
// Resolve / reject the promise
if (childCompilation.errors && childCompilation.errors.length) {
var errorDetails = childCompilation.errors.map(function(error) {
return error.message + (error.error ? ':\n' + error.error: '');
}).join('\n');
reject('Child compilation failed:\n' + errorDetails);
} else {
this.built = this.hash !== entries[0].hash;
this.hash = entries[0].hash;
resolve(childCompilation.assets[outputOptions.filename]);
}
}.bind(this));
}.bind(this));
};
/**
* Evaluates the child compilation result
* Returns a promise
*/
HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, compilationResult) {
if(!compilationResult) {
HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, source) {
if (!source) {
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.

@@ -255,11 +197,4 @@ // To extract the result during the evaluation this part has to be removed.

try {
newSource = vm.runInThisContext(source);
newSource = vm.runInNewContext(source, {HTML_WEBPACK_PLUGIN: true}, {filename: 'html-plugin-evaluation'});
} catch (e) {
// Log syntax error
var syntaxError = require('syntax-error')(source);
var errorMessage = 'Template compilation failed: ' + e +
(syntaxError ? '\n' + syntaxError + '\n\n\n' + source.split('\n').map(function(row, i) {
return (1 + i) + ' - ' + row;
}).join('\n') : '');
compilation.errors.push(new Error(errorMessage));
return Promise.reject(e);

@@ -569,4 +504,4 @@ }

/**
* Helper to prevent compilation in parallel
* Fixes an issue where incomplete cache modules were used
* Helper to prevent html-plugin compilation in parallel
* Fixes "No source available" where incomplete cache modules were used
*/

@@ -573,0 +508,0 @@ function getNextCompilationSlot(compiler, newEntry) {

{
"name": "html-webpack-plugin",
"version": "2.5.0",
"version": "2.6.0",
"description": "Simplifies creation of HTML files to serve your webpack bundles",

@@ -48,8 +48,8 @@ "main": "index.js",

"dependencies": {
"loader-utils": "^0.2.12",
"syntax-error": "^1.1.4",
"bluebird": "^3.1.1",
"html-minifier": "^1.1.1",
"lodash": "^3.10.1"
"loader-utils": "^0.2.12",
"lodash": "^3.10.1",
"pretty-error": "^2.0.0"
}
}

@@ -71,2 +71,4 @@ HTML Webpack Plugin

included scripts and css files. This is useful for cache busting.
- `cache`: `true | false` if `true` (default) try to emit the file only if it was changed.
- `showErrors`: `true | false` if `true` (default) errors details will be written into the html page.
- `chunks`: Allows you to add only some chunks (e.g. only the unit-test chunk)

@@ -73,0 +75,0 @@ - `chunksSortMode`: Allows to controll how chunks should be sorted before they are included to the html. Allowed values: 'none' | 'default' | {function} - default: 'auto'

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc