1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var path_1 = tslib_1.__importDefault(require("path"));
|
5 | var webpack_sources_1 = require("webpack-sources");
|
6 | function getFiles(entrypoints, chunks) {
|
7 | var ret = {};
|
8 | entrypoints.forEach(function (entry) {
|
9 | if (chunks && !chunks.includes(entry.name)) {
|
10 | return;
|
11 | }
|
12 | entry.getFiles().forEach(function (file) {
|
13 | var extension = path_1.default.extname(file).replace(/\./, '');
|
14 | if (!ret[extension]) {
|
15 | ret[extension] = [];
|
16 | }
|
17 | ret[extension].push(file);
|
18 | });
|
19 | });
|
20 | return ret;
|
21 | }
|
22 | function generateAttributes(attributes) {
|
23 | if (attributes === void 0) { attributes = {}; }
|
24 | var stringAttributes = Object.entries(attributes);
|
25 | if (stringAttributes.length === 0) {
|
26 | return '';
|
27 | }
|
28 | return (' ' +
|
29 | stringAttributes
|
30 | .map(function (attr) {
|
31 | if (attr[1] === true) {
|
32 | return attr[0];
|
33 | }
|
34 | return attr[0] + "=\"" + attr[1] + "\"";
|
35 | })
|
36 | .join(' '));
|
37 | }
|
38 | exports.generateAttributes = generateAttributes;
|
39 | function generateCSSReferences(_a) {
|
40 | var _b = _a.files, files = _b === void 0 ? [] : _b, _c = _a.publicPath, publicPath = _c === void 0 ? '' : _c, _d = _a.attributes, attributes = _d === void 0 ? {} : _d;
|
41 | var allAttributes = tslib_1.__assign(tslib_1.__assign({}, attributes), { rel: 'rel' in attributes ? attributes.rel : 'stylesheet' });
|
42 | return files
|
43 | .map(function (file) {
|
44 | return "<link href=\"" + publicPath + file + "\"" + generateAttributes(allAttributes) + ">";
|
45 | })
|
46 | .join('');
|
47 | }
|
48 | exports.generateCSSReferences = generateCSSReferences;
|
49 | function generateJSReferences(_a) {
|
50 | var _b = _a.files, files = _b === void 0 ? [] : _b, _c = _a.publicPath, publicPath = _c === void 0 ? '' : _c, _d = _a.attributes, attributes = _d === void 0 ? {} : _d;
|
51 | attributes = generateAttributes(attributes);
|
52 | return files
|
53 | .map(function (file) { return "<script src=\"" + publicPath + file + "\"" + attributes + "></script>"; })
|
54 | .join('');
|
55 | }
|
56 | exports.generateJSReferences = generateJSReferences;
|
57 | function defaultTemplate(_a) {
|
58 | var _b = _a.css, css = _b === void 0 ? [] : _b, _c = _a.js, js = _c === void 0 ? [] : _c, _d = _a.publicPath, publicPath = _d === void 0 ? '' : _d, _e = _a.title, title = _e === void 0 ? '' : _e, _f = _a.htmlAttributes, htmlAttributes = _f === void 0 ? {
|
59 | lang: 'en',
|
60 | } : _f, _g = _a.head, head = _g === void 0 ? '' : _g, _h = _a.body, body = _h === void 0 ? '' : _h, _j = _a.cssAttributes, cssAttributes = _j === void 0 ? {} : _j, _k = _a.jsAttributes, jsAttributes = _k === void 0 ? {} : _k;
|
61 | var htmlAttrs = generateAttributes(htmlAttributes);
|
62 | var cssTags = generateCSSReferences({
|
63 | files: css,
|
64 | attributes: cssAttributes,
|
65 | publicPath: publicPath,
|
66 | });
|
67 | var jsTags = generateJSReferences({
|
68 | files: js,
|
69 | attributes: jsAttributes,
|
70 | publicPath: publicPath,
|
71 | });
|
72 | return "<!DOCTYPE html>\n <html" + htmlAttrs + ">\n <head>\n <meta charset=\"UTF-8\">\n <title>" + title + "</title>\n " + head + cssTags + "\n </head>\n <body>\n " + body + jsTags + "\n </body>\n </html>";
|
73 | }
|
74 | exports.defaultTemplate = defaultTemplate;
|
75 | var MiniHtmlWebpackPlugin = (function () {
|
76 | function MiniHtmlWebpackPlugin(options) {
|
77 | this.options = options || {};
|
78 | this.plugin = this.plugin.bind(this);
|
79 | }
|
80 | MiniHtmlWebpackPlugin.prototype.plugin = function (compilation, callback) {
|
81 | var _a = this.options, _b = _a.filename, filename = _b === void 0 ? 'index.html' : _b, _c = _a.publicPath, publicPath = _c === void 0 ? '' : _c, template = _a.template, context = _a.context, chunks = _a.chunks;
|
82 | var files = getFiles(compilation.entrypoints, chunks);
|
83 | var options = Object.assign({}, { publicPath: publicPath }, context, files);
|
84 | Promise.resolve((template || defaultTemplate)(options)).then(function (source) {
|
85 | compilation.assets[filename] = new webpack_sources_1.RawSource(source);
|
86 | callback();
|
87 | });
|
88 | };
|
89 | MiniHtmlWebpackPlugin.prototype.apply = function (compiler) {
|
90 | compiler.hooks.emit.tapAsync('MiniHtmlWebpackPlugin', this.plugin);
|
91 | };
|
92 | return MiniHtmlWebpackPlugin;
|
93 | }());
|
94 | exports.default = MiniHtmlWebpackPlugin;
|