-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (129 loc) · 3.72 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
/* eslint-disable */
const HtmlWebpackPlugin = require('html-webpack-plugin')
class WebpackDynamicCdnPlugin {
constructor(windowVar) {
this.windowVar = windowVar || '__webpack_public_path__'
this._currentVersion = 3
this._mapTags = {
3: {
head: 'head',
body: 'body'
},
4: {
head: 'headTags',
body: 'bodyTags'
}
}
}
getBodyTagsKey () {
return this._mapTags[this._currentVersion].body
}
getHeadTagsKey () {
return this._mapTags[this._currentVersion].head
}
formatURL (url) {
if (url.indexOf('/') === 0) {
// remove first '/' of url
return url.slice(1)
}
return url
}
generateHeadTag (tagArray) {
let tags = []
tagArray.forEach((tagObject) => {
const tag = {}
tag.href = this.formatURL(tagObject.attributes.href)
if (tagObject.attributes.rel) {
tag.rel = tagObject.attributes.rel
}
if (tagObject.attributes.as) {
tag.as = tagObject.attributes.as
}
tags.push(JSON.stringify(tag))
})
const renderHeadFunction =`
(function() {
var publicPath = window.${this.windowVar} || '/';
/** set webpack public path */
__webpack_public_path__ = publicPath;
var tags = [${tags}];
var headTag = document.head;
tags.forEach((tag) => {
var link = document.createElement('link');
link.href = publicPath + tag.href;
if (tag.rel) {
link.rel = tag.rel;
}
if (tag.as) {
link.as = tag.as;
}
document.head.appendChild(link);
})
})(window, document);
`
return renderHeadFunction;
}
generateBodyTag (tagArray) {
let tags = []
tagArray.forEach((tagObject) => {
const tag = {}
tag.src = this.formatURL(tagObject.attributes.src)
tags.push(JSON.stringify(tag))
})
const renderBodyFunction =`
(function() {
var publicPath = window.${this.windowVar} || '/';
var tags = [${tags}];
var bodyTag = document.body;
tags.forEach((tag) => {
var script = document.createElement('script');
script.src = window.${this.windowVar} + tag.src;
document.body.appendChild(script);
})
})(window, document);
`
return renderBodyFunction;
}
processingAlterTags (data, cb) {
const headTag = {
tagName: 'script',
closeTag: true,
attributes: {
type: 'text/javascript'
},
innerHTML: this.generateHeadTag(data[this.getHeadTagsKey()])
}
const bodyTag = {
tagName: 'script',
closeTag: true,
attributes: {
type: 'text/javascript'
},
innerHTML: this.generateBodyTag(data[this.getBodyTagsKey()])
}
const alteredData = Object.assign({}, data, { body: [bodyTag], head: [headTag] })
if (cb) {
cb(null, alteredData)
} else {
return Promise.resolve(result)
}
}
plugToHook(compilation) {
if (compilation.hooks.htmlWebpackPluginAlterAssetTags) {
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('DynamicCdnPluginAlterAssets', (data, cb) => this.processingAlterTags(data, cb))
} else if (HtmlWebpackPlugin.getHooks) {
// HtmlWebPackPlugin 4.x
this.currentVersion = 4
let hooks = HtmlWebpackPlugin.getHooks(compilation);
hooks.alterAssetTags.tapAsync('DynamicCdnPluginAlterAssets', (data, cb) => this.processingAlterTags(data, cb))
} else {
compilation.errors.push('HtmlWebpackPlugin not found! \n This plugin work only with HtmlWebpackPlugin')
}
}
apply(compiler) {
compiler.hooks.compilation.tap('DynamicCdnPluginProcess', (compilation) => {
this.plugToHook(compilation)
})
}
}
module.exports = WebpackDynamicCdnPlugin