Skip to content

Commit

Permalink
chore(package): Drop support for node version older than 6 (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon authored Feb 28, 2018
1 parent e01700e commit f669052
Show file tree
Hide file tree
Showing 8 changed files with 641 additions and 693 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.11.5
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change History
==============

v3.0.0
---
* Add support for the new [https://github.com/webpack/tapable](webpack tapable) to be compatible with webpack 4.x
* Similar to webpack 4.x the support for node versions older than 6 are no longer supported
* Remove bluebird dependency

v2.30.1
---
* Revert part the performance optimization (#723) because of #753.
Expand Down
1,185 changes: 569 additions & 616 deletions index.js

Large diffs are not rendered by default.

76 changes: 32 additions & 44 deletions lib/chunksorter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var toposort = require('toposort');
var _ = require('lodash');
const toposort = require('toposort');
const _ = require('lodash');

/**
Sorts dependencies between chunks by their "parents" attribute.
Expand All @@ -26,56 +26,48 @@ var _ = require('lodash');
@return {Array} A topologically sorted version of the input chunks
*/
module.exports.dependency = function (chunks, chunkGroups) {
module.exports.dependency = (chunks, chunkGroups) => {
if (!chunks) {
return chunks;
}

// We build a map (chunk-id -> chunk) for faster access during graph building.
var nodeMap = {};
const nodeMap = {};

chunks.forEach(function (chunk) {
chunks.forEach(chunk => {
nodeMap[chunk.id] = chunk;
});

// Next, we add an edge for each parent relationship into the graph
var edges = [];
let edges = [];

if (chunkGroups) {
// Add an edge for each parent (parent -> child)
edges = chunkGroups.reduce(function (result, chunkGroup) {
return result.concat(
Array.from(chunkGroup.parentsIterable, function (parentGroup) {
return [parentGroup, chunkGroup];
})
);
}, []);
var sortedGroups = toposort.array(chunkGroups, edges);
edges = chunkGroups.reduce((result, chunkGroup) => result.concat(
Array.from(chunkGroup.parentsIterable, parentGroup => [parentGroup, chunkGroup])
), []);
const sortedGroups = toposort.array(chunkGroups, edges);
// flatten chunkGroup into chunks
var sortedChunks = sortedGroups
.reduce(function (result, chunkGroup) {
return result.concat(chunkGroup.chunks);
}, [])
.map(function (chunk) {
// use the chunk from the list passed in, since it may be a filtered list
return nodeMap[chunk.id];
})
.filter(function (chunk, index, self) {
const sortedChunks = sortedGroups
.reduce((result, chunkGroup) => result.concat(chunkGroup.chunks), [])
.map(chunk => // use the chunk from the list passed in, since it may be a filtered list
nodeMap[chunk.id])
.filter((chunk, index, self) => {
// make sure exists (ie excluded chunks not in nodeMap)
var exists = !!chunk;
const exists = !!chunk;
// make sure we have a unique list
var unique = self.indexOf(chunk) === index;
const unique = self.indexOf(chunk) === index;
return exists && unique;
});
return sortedChunks;
} else {
// before webpack 4 there was no chunkGroups
chunks.forEach(function (chunk) {
chunks.forEach(chunk => {
if (chunk.parents) {
// Add an edge for each parent (parent -> child)
chunk.parents.forEach(function (parentId) {
chunk.parents.forEach(parentId => {
// webpack2 chunk.parents are chunks instead of string id(s)
var parentChunk = _.isObject(parentId) ? parentId : nodeMap[parentId];
const parentChunk = _.isObject(parentId) ? parentId : nodeMap[parentId];
// If the parent chunk does not exist (e.g. because of an excluded chunk)
// we ignore that parent
if (parentChunk) {
Expand All @@ -95,36 +87,32 @@ module.exports.dependency = function (chunks, chunkGroups) {
* @param {Array} chunks the list of chunks to sort
* @return {Array} The sorted list of chunks
*/
module.exports.id = function (chunks) {
return chunks.sort(function orderEntryLast (a, b) {
if (a.entry !== b.entry) {
return b.entry ? 1 : -1;
} else {
return b.id - a.id;
}
});
};
module.exports.id = chunks => chunks.sort(function orderEntryLast (a, b) {
if (a.entry !== b.entry) {
return b.entry ? 1 : -1;
} else {
return b.id - a.id;
}
});

/**
* Performs identity mapping (no-sort).
* @param {Array} chunks the chunks to sort
* @return {Array} The sorted chunks
*/
module.exports.none = function (chunks) {
return chunks;
};
module.exports.none = chunks => chunks;

/**
* Sort manually by the chunks
* @param {Array} chunks the chunks to sort
* @return {Array} The sorted chunks
*/
module.exports.manual = function (chunks, specifyChunks) {
var chunksResult = [];
var filterResult = [];
module.exports.manual = (chunks, specifyChunks) => {
const chunksResult = [];
let filterResult = [];
if (Array.isArray(specifyChunks)) {
for (var i = 0; i < specifyChunks.length; i++) {
filterResult = chunks.filter(function (chunk) {
filterResult = chunks.filter(chunk => {
if (chunk.names[0] && chunk.names[0] === specifyChunks[i]) {
return true;
}
Expand Down
39 changes: 18 additions & 21 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
*
*/
'use strict';
var Promise = require('bluebird');
var _ = require('lodash');
var path = require('path');
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');
const _ = require('lodash');
const path = require('path');
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');

/**
* Compiles the template into a nodejs factory, adds its to the compilation.assets
Expand All @@ -33,17 +32,17 @@ var SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
module.exports.compileTemplate = function compileTemplate (template, context, outputFilename, compilation) {
// The entry file is just an empty helper as the dynamic template
// require is added in "loader.js"
var outputOptions = {
const outputOptions = {
filename: outputFilename,
publicPath: compilation.outputOptions.publicPath
};
// Store the result of the parent compilation before we start the child compilation
var assetsBeforeCompilation = _.assign({}, compilation.assets[outputOptions.filename]);
const assetsBeforeCompilation = _.assign({}, 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 = getCompilerName(context, outputFilename);
var childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
const compilerName = getCompilerName(context, outputFilename);
const childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
childCompiler.context = context;
childCompiler.apply(
new NodeTemplatePlugin(outputOptions),
Expand All @@ -56,7 +55,7 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
// Fix for "Uncaught TypeError: __webpack_require__(...) is not a function"
// Hot module replacement requires that every child compiler has its own
// cache. @see https://github.com/ampedandwired/html-webpack-plugin/pull/179
childCompiler.plugin('compilation', function (compilation) {
childCompiler.plugin('compilation', compilation => {
if (compilation.cache) {
if (!compilation.cache[compilerName]) {
compilation.cache[compilerName] = {};
Expand All @@ -66,20 +65,18 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
});

// Compile and return a promise
return new Promise(function (resolve, reject) {
childCompiler.runAsChild(function (err, entries, childCompilation) {
return new Promise((resolve, reject) => {
childCompiler.runAsChild((err, entries, childCompilation) => {
// Resolve / reject the promise
if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
var errorDetails = childCompilation.errors.map(function (error) {
return error.message + (error.error ? ':\n' + error.error : '');
}).join('\n');
const errorDetails = childCompilation.errors.map(error => error.message + (error.error ? ':\n' + error.error : '')).join('\n');
reject(new Error('Child compilation failed:\n' + errorDetails));
} else if (err) {
reject(err);
} else {
// Replace [hash] placeholders in filename
// In webpack 4 the plugin interface changed, so check for available fns
var outputName = compilation.mainTemplate.getAssetPath
const outputName = compilation.mainTemplate.getAssetPath
? compilation.mainTemplate.hooks.assetPath.call(outputOptions.filename, {
hash: childCompilation.hash,
chunk: entries[0]
Expand Down Expand Up @@ -116,7 +113,7 @@ module.exports.compileTemplate = function compileTemplate (template, context, ou
* Returns the child compiler name e.g. 'html-webpack-plugin for "index.html"'
*/
function getCompilerName (context, filename) {
var absolutePath = path.resolve(context, filename);
var relativePath = path.relative(context, absolutePath);
const absolutePath = path.resolve(context, filename);
const relativePath = path.relative(context, absolutePath);
return 'html-webpack-plugin for "' + (absolutePath.length < relativePath.length ? absolutePath : relativePath) + '"';
}
4 changes: 2 additions & 2 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var PrettyError = require('pretty-error');
var prettyError = new PrettyError();
const PrettyError = require('pretty-error');
const prettyError = new PrettyError();
prettyError.withoutColors();
prettyError.skipPackage(['html-plugin-evaluation']);
prettyError.skipNodeFiles();
Expand Down
12 changes: 6 additions & 6 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* This loader renders the template with underscore if no other loader was found */
'use strict';

var _ = require('lodash');
var loaderUtils = require('loader-utils');
const _ = require('lodash');
const loaderUtils = require('loader-utils');

module.exports = function (source) {
if (this.cacheable) {
this.cacheable();
}
var allLoadersButThisOne = this.loaders.filter(function (loader) {
const allLoadersButThisOne = this.loaders.filter(function (loader) {
// Loader API changed from `loader.module` to `loader.normal` in Webpack 2.
return (loader.module || loader.normal) !== module.exports;
});
Expand All @@ -24,15 +24,15 @@ module.exports = function (source) {
// The following part renders the tempalte with lodash as aminimalistic loader
//
// Get templating options
var options = loaderUtils.parseQuery(this.query);
const options = loaderUtils.parseQuery(this.query);
// Webpack 2 does not allow with() statements, which lodash templates use to unwrap
// the parameters passed to the compiled template inside the scope. We therefore
// need to unwrap them ourselves here. This is essentially what lodash does internally
// To tell lodash it should not use with we set a variable
var template = _.template(source, _.defaults(options, { variable: 'data' }));
const template = _.template(source, _.defaults(options, { variable: 'data' }));
// All templateVariables which should be available
// @see HtmlWebpackPlugin.prototype.executeTemplate
var templateVariables = [
const templateVariables = [
'compilation',
'webpack',
'webpackConfig',
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-webpack-plugin",
"version": "2.30.1",
"version": "3.0.0",
"license": "MIT",
"description": "Simplifies creation of HTML files to serve your webpack bundles",
"author": "Charles Blaxland <charles.blaxland@gmail.com> (https://github.com/ampedandwired)",
Expand Down Expand Up @@ -43,13 +43,13 @@
"webpack-recompilation-simulator": "^1.3.0"
},
"dependencies": {
"bluebird": "^3.4.7",
"html-minifier": "^3.2.3",
"loader-utils": "^0.2.16",
"lodash": "^4.17.3",
"pretty-error": "^2.0.2",
"tapable": "^1.0.0",
"toposort": "^1.0.0"
"toposort": "^1.0.0",
"util.promisify": "1.0.0"
},
"peerDependencies": {
"extract-text-webpack-plugin": "^1.0.0 || ^2.0.0 || 3.0.0 || ^4.0.0-alpha.0 || ^4.0.0",
Expand All @@ -63,5 +63,8 @@
],
"bugs": "https://github.com/jantimon/html-webpack-plugin/issues",
"homepage": "https://github.com/jantimon/html-webpack-plugin",
"repository": "https://github.com/jantimon/html-webpack-plugin.git"
"repository": "https://github.com/jantimon/html-webpack-plugin.git",
"engines": {
"node": ">=6.11.5"
}
}

0 comments on commit f669052

Please sign in to comment.