html-webpack-plugin 3.1.0 → 4.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +113 -36
- package/default_index.ejs +1 -1
- package/index.js +618 -397
- package/lib/chunksorter.js +19 -116
- package/lib/compiler.js +359 -101
- package/lib/errors.js +2 -1
- package/lib/hooks.js +116 -0
- package/lib/html-tags.js +73 -0
- package/lib/loader.js +16 -34
- package/package.json +34 -15
package/lib/hooks.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/// <reference path="../typings.d.ts" />
|
|
4
|
+
/* eslint-enable */
|
|
5
|
+
'use strict';
|
|
6
|
+
/**
|
|
7
|
+
* This file provides access to all public htmlWebpackPlugin hooks
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
|
|
11
|
+
/** @typedef {import("../index.js")} HtmlWebpackPlugin */
|
|
12
|
+
|
|
13
|
+
const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
|
|
14
|
+
|
|
15
|
+
// The following typedef holds the API definition for all available hooks
|
|
16
|
+
// to allow easier access when using ts-check or typescript inside plugins
|
|
17
|
+
/** @typedef {{
|
|
18
|
+
|
|
19
|
+
beforeAssetTagGeneration:
|
|
20
|
+
AsyncSeriesWaterfallHook<{
|
|
21
|
+
assets: {
|
|
22
|
+
publicPath: string,
|
|
23
|
+
js: Array<string>,
|
|
24
|
+
css: Array<string>,
|
|
25
|
+
favicon?: string | undefined,
|
|
26
|
+
manifest?: string | undefined
|
|
27
|
+
},
|
|
28
|
+
outputName: string,
|
|
29
|
+
plugin: HtmlWebpackPlugin
|
|
30
|
+
}>,
|
|
31
|
+
|
|
32
|
+
alterAssetTags:
|
|
33
|
+
AsyncSeriesWaterfallHook<{
|
|
34
|
+
assetTags: {
|
|
35
|
+
scripts: Array<HtmlTagObject>,
|
|
36
|
+
styles: Array<HtmlTagObject>,
|
|
37
|
+
meta: Array<HtmlTagObject>,
|
|
38
|
+
},
|
|
39
|
+
outputName: string,
|
|
40
|
+
plugin: HtmlWebpackPlugin
|
|
41
|
+
}>,
|
|
42
|
+
|
|
43
|
+
alterAssetTagGroups:
|
|
44
|
+
AsyncSeriesWaterfallHook<{
|
|
45
|
+
headTags: Array<HtmlTagObject | HtmlTagObject>,
|
|
46
|
+
bodyTags: Array<HtmlTagObject | HtmlTagObject>,
|
|
47
|
+
outputName: string,
|
|
48
|
+
plugin: HtmlWebpackPlugin
|
|
49
|
+
}>,
|
|
50
|
+
|
|
51
|
+
afterTemplateExecution:
|
|
52
|
+
AsyncSeriesWaterfallHook<{
|
|
53
|
+
html: string,
|
|
54
|
+
headTags: Array<HtmlTagObject | HtmlTagObject>,
|
|
55
|
+
bodyTags: Array<HtmlTagObject | HtmlTagObject>,
|
|
56
|
+
outputName: string,
|
|
57
|
+
plugin: HtmlWebpackPlugin,
|
|
58
|
+
}>,
|
|
59
|
+
|
|
60
|
+
beforeEmit:
|
|
61
|
+
AsyncSeriesWaterfallHook<{
|
|
62
|
+
html: string,
|
|
63
|
+
outputName: string,
|
|
64
|
+
plugin: HtmlWebpackPlugin,
|
|
65
|
+
}>,
|
|
66
|
+
|
|
67
|
+
afterEmit:
|
|
68
|
+
AsyncSeriesWaterfallHook<{
|
|
69
|
+
outputName: string,
|
|
70
|
+
plugin: HtmlWebpackPlugin
|
|
71
|
+
}>,
|
|
72
|
+
|
|
73
|
+
}} HtmlWebpackPluginHooks
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
|
|
78
|
+
*/
|
|
79
|
+
const htmlWebpackPluginHooksMap = new WeakMap();
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns all public hooks of the html webpack plugin for the given compilation
|
|
83
|
+
*
|
|
84
|
+
* @param {WebpackCompilation} compilation
|
|
85
|
+
* @returns {HtmlWebpackPluginHooks}
|
|
86
|
+
*/
|
|
87
|
+
function getHtmlWebpackPluginHooks (compilation) {
|
|
88
|
+
let hooks = htmlWebpackPluginHooksMap.get(compilation);
|
|
89
|
+
// Setup the hooks only once
|
|
90
|
+
if (hooks === undefined) {
|
|
91
|
+
hooks = createHtmlWebpackPluginHooks();
|
|
92
|
+
htmlWebpackPluginHooksMap.set(compilation, hooks);
|
|
93
|
+
}
|
|
94
|
+
return hooks;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Add hooks to the webpack compilation object to allow foreign plugins to
|
|
99
|
+
* extend the HtmlWebpackPlugin
|
|
100
|
+
*
|
|
101
|
+
* @returns {HtmlWebpackPluginHooks}
|
|
102
|
+
*/
|
|
103
|
+
function createHtmlWebpackPluginHooks () {
|
|
104
|
+
return {
|
|
105
|
+
beforeAssetTagGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
|
|
106
|
+
alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
|
|
107
|
+
alterAssetTagGroups: new AsyncSeriesWaterfallHook(['pluginArgs']),
|
|
108
|
+
afterTemplateExecution: new AsyncSeriesWaterfallHook(['pluginArgs']),
|
|
109
|
+
beforeEmit: new AsyncSeriesWaterfallHook(['pluginArgs']),
|
|
110
|
+
afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
getHtmlWebpackPluginHooks
|
|
116
|
+
};
|
package/lib/html-tags.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/// <reference path="../typings.d.ts" />
|
|
4
|
+
/* eslint-enable */
|
|
5
|
+
/**
|
|
6
|
+
* @file
|
|
7
|
+
* This file provides to helper to create html as a object repesentation as
|
|
8
|
+
* thoses objects are easier to modify than pure string representations
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* ```
|
|
12
|
+
* const element = createHtmlTagObject('h1', {class: 'demo'}, 'Hello World');
|
|
13
|
+
* const html = htmlTagObjectToString(element);
|
|
14
|
+
* console.log(html) // -> <h1 class="demo">Hello World</h1>
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* All html tag elements which must not contain innerHTML
|
|
20
|
+
* @see https://www.w3.org/TR/html5/syntax.html#void-elements
|
|
21
|
+
*/
|
|
22
|
+
const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Turn a tag definition into a html string
|
|
26
|
+
* @param {HtmlTagObject} tagDefinition
|
|
27
|
+
* A tag element according to the htmlWebpackPlugin object notation
|
|
28
|
+
*
|
|
29
|
+
* @param xhtml {boolean}
|
|
30
|
+
* Wether the generated html should add closing slashes to be xhtml compliant
|
|
31
|
+
*/
|
|
32
|
+
function htmlTagObjectToString (tagDefinition, xhtml) {
|
|
33
|
+
const attributes = Object.keys(tagDefinition.attributes || {})
|
|
34
|
+
.filter(function (attributeName) {
|
|
35
|
+
return tagDefinition.attributes[attributeName] !== false;
|
|
36
|
+
})
|
|
37
|
+
.map(function (attributeName) {
|
|
38
|
+
if (tagDefinition.attributes[attributeName] === true) {
|
|
39
|
+
return xhtml ? attributeName + '="' + attributeName + '"' : attributeName;
|
|
40
|
+
}
|
|
41
|
+
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
|
|
42
|
+
});
|
|
43
|
+
return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (tagDefinition.voidTag && xhtml ? '/' : '') + '>' +
|
|
44
|
+
(tagDefinition.innerHTML || '') +
|
|
45
|
+
(tagDefinition.voidTag ? '' : '</' + tagDefinition.tagName + '>');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Static helper to create a tag object to be get injected into the dom
|
|
50
|
+
*
|
|
51
|
+
* @param {string} tagName
|
|
52
|
+
* the name of the tage e.g. 'div'
|
|
53
|
+
*
|
|
54
|
+
* @param {{[attributeName: string]: string|boolean}} [attributes]
|
|
55
|
+
* tag attributes e.g. `{ 'class': 'example', disabled: true }`
|
|
56
|
+
*
|
|
57
|
+
* @param {string} [innerHTML]
|
|
58
|
+
*
|
|
59
|
+
* @returns {HtmlTagObject}
|
|
60
|
+
*/
|
|
61
|
+
function createHtmlTagObject (tagName, attributes, innerHTML) {
|
|
62
|
+
return {
|
|
63
|
+
tagName: tagName,
|
|
64
|
+
voidTag: voidTags.indexOf(tagName) !== -1,
|
|
65
|
+
attributes: attributes || {},
|
|
66
|
+
innerHTML: innerHTML
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = {
|
|
71
|
+
createHtmlTagObject: createHtmlTagObject,
|
|
72
|
+
htmlTagObjectToString: htmlTagObjectToString
|
|
73
|
+
};
|
package/lib/loader.js
CHANGED
|
@@ -1,51 +1,33 @@
|
|
|
1
1
|
/* This loader renders the template with underscore if no other loader was found */
|
|
2
|
+
// @ts-nocheck
|
|
2
3
|
'use strict';
|
|
3
|
-
|
|
4
4
|
const _ = require('lodash');
|
|
5
5
|
const loaderUtils = require('loader-utils');
|
|
6
6
|
|
|
7
7
|
module.exports = function (source) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
// Get templating options
|
|
9
|
+
const options = this.query !== '' ? loaderUtils.getOptions(this) : {};
|
|
10
|
+
const force = options.force || false;
|
|
11
|
+
|
|
11
12
|
const allLoadersButThisOne = this.loaders.filter(function (loader) {
|
|
12
|
-
|
|
13
|
-
return (loader.module || loader.normal) !== module.exports;
|
|
13
|
+
return loader.normal !== module.exports;
|
|
14
14
|
});
|
|
15
|
-
// This loader shouldn't kick in if there is any other loader
|
|
16
|
-
if (allLoadersButThisOne.length > 0) {
|
|
15
|
+
// This loader shouldn't kick in if there is any other loader (unless it's explicitly enforced)
|
|
16
|
+
if (allLoadersButThisOne.length > 0 && !force) {
|
|
17
17
|
return source;
|
|
18
18
|
}
|
|
19
|
-
// Skip .js files
|
|
20
|
-
if (/\.js$/.test(this.resourcePath)) {
|
|
19
|
+
// Skip .js files (unless it's explicitly enforced)
|
|
20
|
+
if (/\.js$/.test(this.resourcePath) && !force) {
|
|
21
21
|
return source;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
// The following part renders the
|
|
24
|
+
// The following part renders the template with lodash as aminimalistic loader
|
|
25
25
|
//
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// need to unwrap them ourselves here. This is essentially what lodash does internally
|
|
31
|
-
// To tell lodash it should not use with we set a variable
|
|
32
|
-
const template = _.template(source, _.defaults(options, { variable: 'data' }));
|
|
33
|
-
// All templateVariables which should be available
|
|
34
|
-
// @see HtmlWebpackPlugin.prototype.executeTemplate
|
|
35
|
-
const templateVariables = [
|
|
36
|
-
'compilation',
|
|
37
|
-
'webpack',
|
|
38
|
-
'webpackConfig',
|
|
39
|
-
'htmlWebpackPlugin'
|
|
40
|
-
];
|
|
41
|
-
return 'var _ = require(' + loaderUtils.stringifyRequest(this, require.resolve('lodash')) + ');' +
|
|
42
|
-
'module.exports = function (templateParams) {' +
|
|
43
|
-
// Declare the template variables in the outer scope of the
|
|
44
|
-
// lodash template to unwrap them
|
|
45
|
-
templateVariables.map(function (variableName) {
|
|
46
|
-
return 'var ' + variableName + ' = templateParams.' + variableName;
|
|
47
|
-
}).join(';') + ';' +
|
|
26
|
+
const template = _.template(source, _.defaults(options, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data' }));
|
|
27
|
+
// Require !!lodash - using !! will disable all loaders (e.g. babel)
|
|
28
|
+
return 'var _ = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('lodash')) + ');' +
|
|
29
|
+
'module.exports = function (templateParams) { with(templateParams) {' +
|
|
48
30
|
// Execute the lodash template
|
|
49
31
|
'return (' + template.source + ')();' +
|
|
50
|
-
'}';
|
|
32
|
+
'}}';
|
|
51
33
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-webpack-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Simplifies creation of HTML files to serve your webpack bundles",
|
|
6
6
|
"author": "Charles Blaxland <charles.blaxland@gmail.com> (https://github.com/ampedandwired)",
|
|
@@ -12,8 +12,12 @@
|
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
14
|
"pretest": "semistandard",
|
|
15
|
+
"posttest": "tsc",
|
|
16
|
+
"commit": "git-cz",
|
|
15
17
|
"build-examples": "node examples/build-examples.js",
|
|
16
|
-
"test": "
|
|
18
|
+
"test": "jest --runInBand --verbose --coverage",
|
|
19
|
+
"test-watch": "jest --runInBand --watch",
|
|
20
|
+
"puml": "npx puml generate flow.puml -o flow.png",
|
|
17
21
|
"release": "standard-version"
|
|
18
22
|
},
|
|
19
23
|
"semistandard": {
|
|
@@ -22,37 +26,41 @@
|
|
|
22
26
|
]
|
|
23
27
|
},
|
|
24
28
|
"devDependencies": {
|
|
29
|
+
"@types/loader-utils": "1.1.3",
|
|
30
|
+
"@types/node": "10.1.1",
|
|
25
31
|
"appcache-webpack-plugin": "^1.3.0",
|
|
32
|
+
"commitizen": "2.9.6",
|
|
26
33
|
"css-loader": "^0.26.1",
|
|
34
|
+
"cz-conventional-changelog": "2.1.0",
|
|
27
35
|
"dir-compare": "1.3.0",
|
|
28
|
-
"
|
|
29
|
-
"extract-text-webpack-plugin": "^1.0.1",
|
|
36
|
+
"extract-text-webpack-plugin": "^4.0.0-beta.0",
|
|
30
37
|
"file-loader": "^0.9.0",
|
|
31
38
|
"html-loader": "^0.4.4",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"jasmine-diff-matchers": "^2.0.0",
|
|
39
|
+
"jest": "23.3.0",
|
|
40
|
+
"pug": "2.0.3",
|
|
41
|
+
"pug-loader": "2.4.0",
|
|
36
42
|
"rimraf": "^2.5.4",
|
|
37
43
|
"semistandard": "8.0.0",
|
|
38
44
|
"standard-version": "^4.3.0",
|
|
39
45
|
"style-loader": "^0.13.1",
|
|
46
|
+
"typescript": "2.9.1",
|
|
40
47
|
"underscore-template-loader": "^0.7.3",
|
|
41
48
|
"url-loader": "^0.5.7",
|
|
42
|
-
"webpack": "
|
|
43
|
-
"webpack-
|
|
49
|
+
"webpack": "4.1.0",
|
|
50
|
+
"webpack-cli": "2.0.12",
|
|
51
|
+
"webpack-recompilation-simulator": "^3.0.0"
|
|
44
52
|
},
|
|
45
53
|
"dependencies": {
|
|
54
|
+
"@types/tapable": "1.0.2",
|
|
46
55
|
"html-minifier": "^3.2.3",
|
|
47
|
-
"loader-utils": "^
|
|
48
|
-
"lodash": "^4.17.
|
|
56
|
+
"loader-utils": "^1.1.0",
|
|
57
|
+
"lodash": "^4.17.10",
|
|
49
58
|
"pretty-error": "^2.0.2",
|
|
50
59
|
"tapable": "^1.0.0",
|
|
51
|
-
"toposort": "^1.0.0",
|
|
52
60
|
"util.promisify": "1.0.0"
|
|
53
61
|
},
|
|
54
62
|
"peerDependencies": {
|
|
55
|
-
"webpack": "^
|
|
63
|
+
"webpack": "^4.0.0"
|
|
56
64
|
},
|
|
57
65
|
"keywords": [
|
|
58
66
|
"webpack",
|
|
@@ -64,6 +72,17 @@
|
|
|
64
72
|
"homepage": "https://github.com/jantimon/html-webpack-plugin",
|
|
65
73
|
"repository": "https://github.com/jantimon/html-webpack-plugin.git",
|
|
66
74
|
"engines": {
|
|
67
|
-
"node": ">=6.
|
|
75
|
+
"node": ">=6.9"
|
|
76
|
+
},
|
|
77
|
+
"config": {
|
|
78
|
+
"commitizen": {
|
|
79
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"jest": {
|
|
83
|
+
"watchPathIgnorePatterns": [
|
|
84
|
+
"<rootDir>/dist"
|
|
85
|
+
],
|
|
86
|
+
"testEnvironment": "node"
|
|
68
87
|
}
|
|
69
88
|
}
|