-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (117 loc) · 4.88 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
/**
* Created by timxuan on 2016/11/4.
*/
"use strict";
let path = require('path');
let linkTest = /(?:\s*(<link([^>]*?)(stylesheet){1}([^>]*?)(?:\/)?>))/ig,
styleUrl = /(?:\shref\s*=\s*)('([^'<>]+)'|"([^"<>]+)"|[^\s\/>]+)/i,
ScriptTest = /(?:(\s*<script([^>]*)>([\s\S]*?)<\/script>))/ig,
scriptSrc = /(?:\ssrc\s*=\s*)('([^<>']+)'|"([^<>\"]+)")/i,
deleteComment = /((<!(?:--)?\[[\s\S]*?<\!\[endif\](?:--)?>|<!--[\s\S]*?(?:-->|$))|(?:(\s*<script[^>]*>[^<>]*<\/script>)|(?:\s*(<link([^>]*?)(?:\/)?>)|(<style([^>]*)>([\s\S]*?)<\/style>))))<!--delete-->/ig;
let class2type = {};
"Boolean Number String Function Array Date RegExp Object Error".split(" ").forEach(function(name){
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
function type(obj) {
return obj == null ? String(obj) :
class2type[toString.call(obj)] || "object";
}
function checkNum(num,len){
return num >= len ? 0: (num < 0 ? (len - 1):num);
}
function replaceHtml(html,linkTest,urlTest,filePath,domain){
let num = 0,
len = 0,
domainStr = domain,
isArray = domain instanceof Array;
if(isArray){
len = domain.length;
domainStr = '';
if(len > 0){
num = Math.floor(Math.random() / (1 / len));
num = checkNum(num, len);
domainStr = domain[num]
}
}
return html.replace(linkTest,function(v,u){
let urlPath = v.replace(urlTest,function(url,$1){
let route = $1.replace(/\'|\"/ig,'').trim(),
realPath = path.isAbsolute(route)?route:path.join(filePath,route);
realPath = path.normalize(realPath).split('\\').join('\/');
if(realPath.charAt(0) != '/'){
realPath = '/' + realPath;
}
if(domainStr){
let str = domainStr.charAt(domainStr.length - 1);
if(str.match(/\\|\//gi)){
domainStr = domainStr.substring(0,domainStr.length - 1);
}
realPath = domainStr + realPath;
}
return url.replace($1,'"'+realPath+'"');
});
if(isArray){
num = checkNum(num + 1, len);
domainStr = domain[num];
}
return urlPath;
});
}
function AbsolutePath(options){
this.cssDomain = '';
this.jsDomain = '';
if(type(options) === 'object'){
for (var i in options) this[i] = options[i];
}
}
AbsolutePath.prototype.apply = function(compiler) {
/*compiler.plugin("compile", function(params) {
console.log("The compiler is starting to compile...");
});*/
compiler.plugin("compilation", function(compilation) {
//console.log("The compiler is starting a new compilation...");
/*compilation.plugin('html-webpack-plugin-before-html-generation', function(htmlPluginData, callback) {
console.log('html-webpack-plugin-before-html-generation-->');
console.log(htmlPluginData);
callback(null, htmlPluginData);
});*/
compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) {
htmlPluginData.html = htmlPluginData.html.replace(deleteComment,''); //删除测试的css与js
callback(null, htmlPluginData);
});
/*compilation.plugin('html-webpack-plugin-alter-asset-tags', function(htmlPluginData, callback) {
console.log('html-webpack-plugin-alter-asset-tags-->');
console.log(htmlPluginData);
callback(null, htmlPluginData);
});*/
compilation.plugin('html-webpack-plugin-after-html-processing', function(htmlPluginData, callback) {
let htmlPath = htmlPluginData.plugin.options.filename,
html = htmlPluginData.html,
linkArray = html.match(linkTest),
scriptArray = html.match(ScriptTest);
if(linkArray && linkArray.length > 0 && this.cssDomain){
html = replaceHtml(html,linkTest,styleUrl,htmlPath,this.cssDomain);
}
if(scriptArray && scriptArray.length > 0 && this.jsDomain){
html = replaceHtml(html,ScriptTest,scriptSrc,htmlPath,this.jsDomain);
}
htmlPluginData.html = html;
callback(null, htmlPluginData);
}.bind(this));
/*
compilation.plugin('html-webpack-plugin-after-emit', function(htmlPluginData, callback) {
console.log('html-webpack-plugin-after-emit-->');
console.log(htmlPluginData);
callback(null, htmlPluginData);
});
compilation.plugin("optimize", function() {
console.log("The compilation is starting to optimize files...");
});
*/
}.bind(this));
/*compiler.plugin("emit", function(compilation, callback) {
console.log("The compilation is going to emit files...");
callback();
});*/
};
module.exports = AbsolutePath;