-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
565 lines (531 loc) · 17.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
'use strict';
class HtmlWebpackPugPlugin {
/**
* @param {HtmlWebpackOptions} [options]
*/
constructor (options) {
this.options = Object.assign({
ast: false,
adjustIndent: false
}, options);
}
apply (compiler) {
var pluginName = 'HtmlWebpackPugPlugin';
var self = this;
// Hook into the html-webpack-plugin processing
var beforeAssetsInjection = function (htmlPluginData, callback) {
self.preProcessHtml(htmlPluginData, callback);
}
var afterAssetsInjection = function (htmlPluginData, callback) {
self.postProcessHtml(htmlPluginData, callback);
}
// Webpack 4+
if (compiler.hooks) {
compiler.hooks.compilation.tap(pluginName, function (compilation) {
var myHooks = (function (comp) {
if (comp.hooks.htmlWebpackPluginBeforeHtmlGeneration) {
// HtmlWebPackPlugin 3.x
return [
{ hook: comp.hooks.htmlWebpackPluginBeforeHtmlProcessing, func: beforeAssetsInjection},
{ hook: comp.hooks.htmlWebpackPluginAfterHtmlProcessing, func: afterAssetsInjection},
];
} else {
var HtmlWebpackPlugin = require('html-webpack-plugin');
var hooks = HtmlWebpackPlugin.getHooks(comp);
return [
{ hook: hooks.afterTemplateExecution, func: beforeAssetsInjection},
{ hook: hooks.beforeEmit, func: afterAssetsInjection},
];
}
})(compilation);
myHooks.forEach(function (v) {
v.hook.tapAsync(pluginName, v.func);
})
});
} else {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-before-html-processing', beforeAssetsInjection);
compilation.plugin('html-webpack-plugin-after-html-processing', afterAssetsInjection);
});
}
};
/**
* Is it no longer supported
* @param htmlPluginData
* @return bool no longer supported -> true
*/
isNoLongerSupported (htmlPluginData) {
var target = ['haml', 'slim'];
var ext = htmlPluginData.outputName.split('.').pop();
var fileType = htmlPluginData.plugin.options.filetype;
// If the plugin configuration set `filename` extension or `filetype` to ('haml' or 'slim')
return target.indexOf(ext) >= 0 || target.indexOf(fileType) >= 0;
};
/**
* Is it processing target
* @param htmlPluginData
* @return bool processing target -> true
*/
isProcessingTarget (htmlPluginData) {
var target = ['pug', 'jade'];
var ext = htmlPluginData.outputName.split('.').pop();
var fileType = htmlPluginData.plugin.options.filetype;
// If the plugin configuration set `filename` extension or `filetype` to ('pug' or 'jade')
return target.indexOf(ext) >= 0 || target.indexOf(fileType) >= 0;
};
/**
* Process the generated HTML(before assets injection)
*/
preProcessHtml (htmlPluginData, callback) {
var self = this;
if (self.isNoLongerSupported(htmlPluginData)) {
var message = '\
Sorry, Slim and Haml are no longer supported.\n\
Please change to these packages.\n\
\n\
Slim: [html-webpack-slim-plugin](https://www.npmjs.com/package/html-webpack-slim-plugin)\n\
Haml: [html-webpack-haml-plugin](https://www.npmjs.com/package/html-webpack-haml-plugin)';
throw new Error(message);
}
if (self.isProcessingTarget(htmlPluginData)) {
// do not minify
htmlPluginData.plugin.options.minify = false;
htmlPluginData.html = self.deleteExtraNewlines(htmlPluginData.html);
if (this.options.adjustIndent) {
htmlPluginData.html = self.adjustElementsIndentation(htmlPluginData.html);
}
}
callback(null, htmlPluginData);
};
/**
* Process the generated HTML(after assets injection)
*/
postProcessHtml (htmlPluginData, callback) {
var self = this;
var options = htmlPluginData.plugin.options;
// If the plugin configuration set `inject` to true and (set `filename` extension or `filetype` to ('pug' or 'jade'))
if (options.inject && self.isProcessingTarget(htmlPluginData)) {
if (options.filename === 'index.html') {
var filename = 'index.' + options.filetype;
htmlPluginData.outputName = filename;
htmlPluginData.plugin.childCompilationOutputName = filename;
htmlPluginData.plugin.options.filename = filename;
}
htmlPluginData.html = self.injectAssetsIntoFile(htmlPluginData);
}
callback(null, htmlPluginData);
};
/**
* Adjust elements indentation
* @param html htmlPluginData.html (pug)
*/
adjustElementsIndentation (html) {
var self = this;
html = self.adjustHeadElementsIndentation(html);
html = self.adjustBodyElementsIndentation(html);
return html;
};
/**
* Delete trailing extra newlines
* e.g.
* before
* #footer
* Footer content
*
*
* after
* #footer
* Footer content
*
* @param html htmlPluginData.html (Pug/Jade)
*/
deleteExtraNewlines (html) {
return html.replace(/(\r?\n){2,}$/im, '$1');
}
/**
* Adjust head elements indentation
* e.g.
* before
* head
* meta(charset="utf-8")
* meta(http-equiv="X-UA-Compatible" content="IE=edge")
* meta(name="viewport" content="width=device-width, initial-scale=1")
* meta(name="description" content="Webpack App")
* title
* block title
* after
* head
* meta(charset="utf-8")
* meta(http-equiv="X-UA-Compatible" content="IE=edge")
* meta(name="viewport" content="width=device-width, initial-scale=1")
* meta(name="description" content="Webpack App")
* title
* block title
* @param html htmlPluginData.html (Pug/Jade)
*/
adjustHeadElementsIndentation (html) {
var regExp = /^(([ |\t]*)html.*\n)(([ |\t]*)head\n)([ |\t]*[\s\S]*)(\n[ |\t]*body)/im;
var match = regExp.exec(html);
if (match) {
var nextIndent = match[4].repeat(2).replace(match[2], '');
var elements = match[5].split('\n').map(function(v) {
if (/^[ |\t]*(title|style|meta|link|script|base)/.test(v)) {
return v.replace(/^[ |\t]*/, nextIndent).replace(/[ | |\t]+$/, '');
}
return v;
});
html = html.replace(regExp, match[1] + match[3] + elements.join('\n') + match[6]);
}
return html;
}
/**
* Adjust body elements indentation
* !Operation guarantee of this function is limited
* e.g.
* before
* body#body.main
* h1 Main
* img(src="logo.png")
* h1 Pug - node template engine
* #container.col
* if youAreUsingPug
* p You are amazing
* else
* p Get on it!
* p.
* Pug is a terse and simple templating language with a
* strong focus on performance and powerful features.
* #footer
* Footer content
* after
* body#body.main
* h1 Main
* img(src="logo.png")
* h1 Pug - node template engine
* #container.col
* if youAreUsingPug
* p You are amazing
* else
* p Get on it!
* p.
* Pug is a terse and simple templating language with a
* strong focus on performance and powerful features.
* #footer
* Footer content
* @param html htmlPluginData.html (Pug/Jade)
*/
adjustBodyElementsIndentation (html) {
var regExp = function(html) {
var h = /^([ |\t]*)head/im.exec(html);
var topSpace = h ? h[1] : '[ |\t]*';
return new RegExp('^(' + topSpace + ')(body.*\\n)([ |\t]*[\\s\\S]*)', 'im');;
}(html);
var match = regExp.exec(html);
if (match) {
var padding = false;
var indent = match[1];
var addIndent = indent.repeat(2);
var elements = match[3].split('\n');
var newElements = [];
for (var i = 0; i < elements.length; i++) {
var elm = elements[i];
// Skip first element
if (i === 0) {
newElements.push(elm);
continue;
}
// Skip blank element
if (elm.trim() === '') {
newElements.push(elm.trim());
continue;
}
var m = /^([ |\t]*).*$/i.exec(elm);
// If the indentation is shallower than the body
if (padding || (m && (m[1].length < indent.length))) {
// After that, add indentation to all elements
padding = true;
elm = addIndent + elm;
}
newElements.push(elm);
}
html = html.replace(regExp, match[1] + match[2] + newElements.join('\n'));
}
return html;
}
/**
* Create a tag of pug format
*/
createPugTag (tag) {
if (tag.search(/=['"]/i) === -1) { return tag };
return tag.replace(' ', '(') + ')';
};
/**
* Injects the assets into the given file string
*/
injectAssetsIntoFile (htmlPluginData) {
var self = this;
var options = htmlPluginData.plugin.options;
var assets = self.getAssets(htmlPluginData);
var html = htmlPluginData.html;
var hasTemplate = self.hasTemplate(options.template);
if (!hasTemplate) {
html = html.replace(/\r?\n[ ]*/g, '');
}
var tagsOfHead = self.headExtraction(html).map(function (e) {
return e.match(/title>.*<\/title/i) ? 'title ' + options.title : e;
});
var tagsOfBody = htmlPluginData.plugin.options.inject !== 'head' ? self.bodyExtraction(html) : [];
var file = hasTemplate ? self.removeUnnecessaryTags(html) : self.defaultTemplate();
tagsOfHead = tagsOfHead.map(self.createPugTag);
tagsOfBody = tagsOfBody.map(self.createPugTag);
return this.options.ast
? self.injectAssetsUsingAST (file, tagsOfHead, tagsOfBody, assets, options.inject)
: self.injectAssets(file, tagsOfHead, tagsOfBody, assets);
};
/**
* Is a valid template file set
* @param filename template file name
*/
hasTemplate (filename) {
var ext = filename.split('.').pop();
return ['pug', 'jade', 'js'].indexOf(ext) >= 0;
};
/**
* Default template
*/
defaultTemplate () {
return '\
doctype html\n\
html\n\
head\n\
body';
};
/**
* Get Assets
* @param html htmlPluginData
*/
getAssets (htmlPluginData) {
if (htmlPluginData.assets) {
return htmlPluginData.assets;
}
return (function (str) {
var regExp = /([\w-]*\.appcache)/i;
var match = regExp.exec(str);
if (!match || match.length < 2) {
return undefined;
}
return { manifest: match[1] };
})(htmlPluginData.plugin.assetJson);
};
/**
* Extract the style tags from head
* @param html htmlPluginData.html (Pug/Jade)
*/
headExtraction (html) {
var regExp = /<head><(.*)><\/head>/i;
var match = regExp.exec(html);
if (!match || match.length < 2) {
return [];
}
return match[1].split('><').filter(function(v) { return !v.startsWith('/') });
};
/**
* Extract the script tags from body
* @param html htmlPluginData.html (Pug/Jade)
*/
bodyExtraction (html) {
var regExp = /<(script.*)><\/script>/i;
var match = regExp.exec(html);
if (!match || match.length < 2) {
return [];
}
return match[1].split('></script><');
};
/**
* Remove html format tags
* @param html htmlPluginData.html (Pug/Jade)
*/
removeUnnecessaryTags (html) {
var headRegExp = /<head><(.*)><\/head>/i;
var bodyRegExp = /<(script.*)><\/script>/i;
return html.replace(headRegExp, '').replace(bodyRegExp, '');
};
/*******************
Regular Expression
*******************/
/**
* Injects the assets into the given string
* @param html htmlPluginData.html (Pug/Jade)
* @param head inject in the head tag (e.g. style tag)
* @param body inject in the body tag (e.g. script tag)
* @param manifest
*/
injectAssets (html, head, body, assets) {
var self = this;
var regExp = function(html) {
var h = /^([ |\t]*)head/im.exec(html);
var topSpace = h ? h[1] : '[ |\t]*';
return new RegExp('^(' + topSpace + ')(body)\\b', 'im');;
}(html);
var match = regExp.exec(html);
if (match) {
var headSpace = match[1];
var hlSpace = function(space, html) {
// delete extra space (left space of html tag)
var m = /^([ |\t]*)html/im.exec(html);
return m ? space.replace(m[1], '') : space;
}(headSpace.repeat(2), html);
if (head.length) {
head = head.map(function(v) {
return hlSpace + v;
});
if (!/head/.test(html)) {
head = [headSpace + 'head'].concat(head)
}
// Append assets to head element
html = html.replace(regExp, head.join('\n') + '\n' + match[0]);
}
if (body.length) {
body = body.map(function(v) {
return hlSpace + v;
});
// Append scripts to the end of the html:
if (html[html.length-1] != '\n') {
html += '\n'
}
html += body.join('\n');
}
}
// Inject manifest into the opening html tag
if (assets) {
html = self.injectManifest(html, assets.manifest);
}
return html;
};
/**
* Inject manifest into the opening html tag
* @param html htmlPluginData.html (Pug/Jade)
* @param manifest
*/
injectManifest (html, manifest) {
if (!manifest) {
return html;
}
return html.replace(/^([ |\t]*html.*)$/im, function (match, p1) {
// Append the manifest only if no manifest was specified
if (/\smanifest\s*=/.test(match)) {
return match;
}
var regExp = /^([ |\t]*html[^\(]*)(\([^\)]*)?(\))?$/i;
var match = regExp.exec(p1);
if (match) {
var elements = match.filter(function(v) { return v != undefined });
if (elements.length <= 3) {
return elements[1] + '(manifest="' + manifest + '")';
};
return elements[1] + elements[2].trim() + ' manifest="' + manifest + '"' + elements[3];
}
return match;
});
};
/**********
AST
**********/
/**
* Injects the assets into the given string usint AST.
* @param html htmlPluginData.html (Pug/Jade)
* @param head inject in the head tag (e.g. style tag)
* @param body inject in the body tag (e.g. script tag)
* @param manifest
* @param inject htmlPluginData.plugin.options.inject
*/
injectAssetsUsingAST (html, head, body, assets, inject) {
var lexer, parser, walk, generater;
try {
lexer = require('pug-lexer');
parser = require('pug-parser');
walk = require('pug-walk');
generater = require('pug-source-gen');
} catch (err){
var message = '\
Sorry, the following packages are required for AST mode (for security reasons, they are not implicitly installed in this packages).\n\
・pug-lexer\n\
・pug-parser\n\
・pug-walk\n\
・pug-source-gen\n\
\n\
Caution: It is recommended to avoid using AST mode as much as possible, as some packages are old and vulnerable.';
throw new Error(message);
}
var self = this;
var ast = parser(lexer(html));
var noHead = true;
var noBody = true;
walk(ast, function(node) {
if (!node.name || node.type != 'Tag') {
return;
}
if (node.name === 'head') {
noHead = false;
} else if (node.name === 'body') {
noBody = false;
}
});
if (inject === 'head' && noHead) {
throw new Error('When inject is \'head\', the head tag is required.');
}
var injectTags = self.getInjectTags(head, body, inject, noHead, noBody, parser, lexer);
walk(ast, function(node, replace) {
if (node.name && node.type === 'Tag') {
if (node.name === 'html') {
if (assets) {
var attrs = node.attrs;
Object.keys(assets).forEach(function(key) {
attrs.push({ name: key, val: '"' + assets[key] + '"', mustEscape: true });
});
node.attrs = attrs;
replace(node);
}
}
var key = node.name;
if (injectTags[key]) {
const block = node.block;
block.nodes = block.nodes.concat(injectTags[key].nodes);
node.block = block;
replace(node);
}
}
if (node.attrs && 1 <= node.attrs.length) {
var attrs = node.attrs;
Object.keys(attrs).forEach(function(key) {
attrs[key].escaped = attrs[key].mustEscape;
});
node.attrs = attrs;
replace(node);
}
});
return generater(ast, { indentChar: self.getIndentChar(html) });
}
getInjectTags (head, body, inject, noHead, noBody, parser, lexer) {
if (inject === 'head') {
return { head: parser(lexer(head.join('\n'))) }
}
if (noHead || noBody) {
var str = (head.concat(body || [])).join('\n');
return { [noHead ? 'body': 'head']: parser(lexer(str)) }
}
return {
head: parser(lexer(head.join('\n'))),
body: parser(lexer(body.join('\n')))
}
}
getIndentChar (html) {
var regExp = /^[ |\t]*html.*\n([ |\t]*)[^( |\t)]/im;
var match = regExp.exec(html);
if (!match || match.length < 2) {
return ' ';
}
return match[1];
}
}
module.exports = HtmlWebpackPugPlugin;