/
specified-insert.js
108 lines (92 loc) · 2.55 KB
/
specified-insert.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
/**
* InjectContentHtmlPlugin
*/
function insertStringAfter(raw, keyword, inserted) {
keyword = String(keyword);
var index = raw.indexOf(keyword);
if (index < 0) {
return raw;
}
var endIndex = index + keyword.length;
return raw.slice(0, endIndex) + inserted.trim() + raw.slice(endIndex);
}
function insertStringBefore(raw, keyword, inserted) {
keyword = String(keyword);
var index = raw.indexOf(keyword);
if (index < 0) {
return raw;
}
return raw.slice(0, index) + inserted.trim() + raw.slice(index);
}
const insertHelper = {
before: insertStringBefore,
after: insertStringAfter
};
const positionMap = {
'prepend-head': ['after', '<head>'],
'append-head': ['before', '</head>'],
'prepend-body': ['after', '<body>'],
'append-body': ['before', '</body>']
};
// var result = insertContent(
// `<!DOCTYPE html>
// <html lang="en">
// <head>
// <title>Document</title>
// </head>
// <body>
// <p>content</p>
// </body>
// </html>
// `,
// `
// <script>var foo=1;</script>
// `,
// {
// position: 'body',
// type: 'prepend'
// }
// );
/**
*
* @param {string} raw 原始字符串
* @param {string} content 插入的字符串
* @param {object} options 选项 { position: 'head', type: 'append' }
* @returns 字符串
*/
const insertContent = (raw, content, options) => {
if (!raw) return raw;
const opt = `${options.type}-${options.position}`;
if (!positionMap[opt]) return raw;
const [insertedType, insertedTag] = positionMap[opt];
const insertor = insertHelper[insertedType];
return insertor(raw, insertedTag, content);
};
/**
*
* new InjectContentHtmlPlugin({
* content: '<script>window.foo = 'foo';</script>',
* position: 'head',
* type: 'prepend'
* });
*/
class InjectContentHtmlPlugin {
constructor(option) {
// cotent { options: '', position: 'head', type: 'append'}
this.injectOption = option;
}
apply(complier) {
complier.plugin('compilation', compilation => {
compilation.plugin('html-webpack-plugin-before-html-processing', (data, callback) => {
const { content, ...option } = this.injectOption;
data.html = insertContent(data.html, content, option);
if (callback) {
callback(null, data);
} else {
return Promise.resolve(data);
}
});
});
}
}
module.exports = InjectContentHtmlPlugin;