1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
14 | if (k2 === undefined) k2 = k;
|
15 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
16 | }) : (function(o, m, k, k2) {
|
17 | if (k2 === undefined) k2 = k;
|
18 | o[k2] = m[k];
|
19 | }));
|
20 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
21 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
22 | }) : function(o, v) {
|
23 | o["default"] = v;
|
24 | });
|
25 | var __importStar = (this && this.__importStar) || function (mod) {
|
26 | if (mod && mod.__esModule) return mod;
|
27 | var result = {};
|
28 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
29 | __setModuleDefault(result, mod);
|
30 | return result;
|
31 | };
|
32 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
33 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
34 | };
|
35 | Object.defineProperty(exports, "__esModule", { value: true });
|
36 | exports.generateJSReferences = exports.generateCSSReferences = exports.generateAttributes = exports.defaultTemplate = exports.MiniHtmlWebpackPlugin = void 0;
|
37 | var path_1 = __importDefault(require("path"));
|
38 | var webpack_1 = __importStar(require("webpack"));
|
39 | function getFiles(entrypoints, chunks) {
|
40 | var ret = {};
|
41 | entrypoints.forEach(function (entry) {
|
42 | if (chunks && !chunks.includes(entry.name)) {
|
43 | return;
|
44 | }
|
45 | entry.getFiles().forEach(function (file) {
|
46 | var extension = path_1.default.extname(file).replace(/\./, '');
|
47 | if (!ret[extension]) {
|
48 | ret[extension] = [];
|
49 | }
|
50 | ret[extension].push(file);
|
51 | });
|
52 | });
|
53 | return ret;
|
54 | }
|
55 | function generateAttributes(attributes) {
|
56 | if (attributes === void 0) { attributes = {}; }
|
57 | var stringAttributes = Object.entries(attributes);
|
58 | if (stringAttributes.length === 0) {
|
59 | return '';
|
60 | }
|
61 | return (' ' +
|
62 | stringAttributes
|
63 | .map(function (attr) {
|
64 | if (attr[1] === true) {
|
65 | return attr[0];
|
66 | }
|
67 | return attr[0] + "=\"" + attr[1] + "\"";
|
68 | })
|
69 | .join(' '));
|
70 | }
|
71 | exports.generateAttributes = generateAttributes;
|
72 | function generateCSSReferences(_a) {
|
73 | 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;
|
74 | if (!attributes) {
|
75 | return '';
|
76 | }
|
77 | var allAttributes = __assign(__assign({}, attributes), { rel: 'rel' in attributes ? attributes.rel : 'stylesheet' });
|
78 | return files
|
79 | .map(function (file) {
|
80 | return "<link href=\"" + publicPath + file + "\"" + generateAttributes(allAttributes) + ">";
|
81 | })
|
82 | .join('');
|
83 | }
|
84 | exports.generateCSSReferences = generateCSSReferences;
|
85 | function generateJSReferences(_a) {
|
86 | 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;
|
87 | if (!attributes) {
|
88 | return '';
|
89 | }
|
90 | return files
|
91 | .map(function (file) {
|
92 | return "<script src=\"" + publicPath + file + "\"" + generateAttributes(attributes) + "></script>";
|
93 | })
|
94 | .join('');
|
95 | }
|
96 | exports.generateJSReferences = generateJSReferences;
|
97 | function defaultTemplate(_a) {
|
98 | 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 ? {
|
99 | lang: 'en',
|
100 | } : _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;
|
101 | var htmlAttrs = generateAttributes(htmlAttributes);
|
102 | var cssTags = generateCSSReferences({
|
103 | files: css,
|
104 | attributes: cssAttributes,
|
105 | publicPath: publicPath,
|
106 | });
|
107 | var jsTags = generateJSReferences({
|
108 | files: js,
|
109 | attributes: jsAttributes,
|
110 | publicPath: publicPath,
|
111 | });
|
112 | 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>";
|
113 | }
|
114 | exports.defaultTemplate = defaultTemplate;
|
115 | function isWebpack4() {
|
116 | return webpack_1.default.version.split('.')[0] === '4';
|
117 | }
|
118 | var MiniHtmlWebpackPlugin = (function () {
|
119 | function MiniHtmlWebpackPlugin(options) {
|
120 | this.options = options || {};
|
121 | this.webpack4plugin = this.webpack4plugin.bind(this);
|
122 | this.webpack5plugin = this.webpack5plugin.bind(this);
|
123 | }
|
124 | MiniHtmlWebpackPlugin.prototype.webpack4plugin = function (compilation, callback) {
|
125 | 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;
|
126 | var files = getFiles(compilation.entrypoints, chunks);
|
127 | var options = Object.assign({}, { publicPath: publicPath }, context, files);
|
128 | Promise.resolve((template || defaultTemplate)(options)).then(function (source) {
|
129 |
|
130 | var sources = require('webpack-sources');
|
131 | compilation.assets[filename] = new sources.RawSource(source, true);
|
132 | callback();
|
133 | });
|
134 | };
|
135 | MiniHtmlWebpackPlugin.prototype.webpack5plugin = function (compilation) {
|
136 | 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;
|
137 |
|
138 | return Promise.resolve((template || defaultTemplate)(__assign(__assign({ publicPath: publicPath }, context), getFiles(compilation.entrypoints, chunks)))).then(function (source) {
|
139 |
|
140 |
|
141 | var sources = require('webpack').sources;
|
142 | compilation.emitAsset(filename, new sources.RawSource(source, true));
|
143 | });
|
144 | };
|
145 | MiniHtmlWebpackPlugin.prototype.apply = function (compiler) {
|
146 | var _this = this;
|
147 | var pluginName = 'MiniHtmlWebpackPlugin';
|
148 | if (isWebpack4()) {
|
149 |
|
150 | compiler.hooks.emit.tapAsync(pluginName, this.webpack4plugin);
|
151 | }
|
152 | else {
|
153 | compiler.hooks.thisCompilation.tap(pluginName, function (compilation) {
|
154 | compilation.hooks.processAssets.tapPromise({
|
155 | name: pluginName,
|
156 |
|
157 | stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
|
158 | }, function () { return _this.webpack5plugin(compilation); });
|
159 | });
|
160 | }
|
161 | };
|
162 | return MiniHtmlWebpackPlugin;
|
163 | }());
|
164 | exports.MiniHtmlWebpackPlugin = MiniHtmlWebpackPlugin;
|