-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (119 loc) · 3.09 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
var lzstr = require("lz-string"),
querystring = require("querystring");
var zippers = {
base64:{
compress(content){
return lzstr.compressToBase64(content);
},
decompress(compressed){
return `lzstr.decompressFromBase64("${compressed}")`;
}
},
utf16:{
compress(content){
return lzstr.compressToUTF16(content);
},
decompress(compressed){
return `lzstr.decompressFromUTF16("${compressed}")`;
}
},
"webkit-utf16":{
compress(content){
return lzstr.compress(content);
},
decompress(compressed){
return `lzstr.decompress("${compressed}")`;
}
},
"uri":{
compress(content){
return lzstr.compressToEncodedURIComponent(content);
},
decompress(compressed){
return `lzstr.decompressFromEncodedURIComponent("${compressed}")`;
}
},
"uint8":{
compress(content){
return lzstr.compressToUint8Array(content);
},
decompress(compressed){
return `lzstr.decompressFromUint8Array("${compressed}")`;
}
}
}
const DECOMPRESS = "lzstr.decompressFromBase64";
var compressContent = function(content,params){
if(params && params.to && zippers[params.to]){
return zippers[params.to].compress(content);
}
return lzstr.compressToBase64(content);
}
var stringify = function(content){
var t = typeof content;
switch(t){
case "object":return JSON.stringify(content);
case "string":return content;
case "function":return content.toString();
}
}
var resolveContent = function(self,content,type){
var val = self.inputValue || content;
if(Array.isArray(val)) return stringify(val[0]);
else return stringify(val);
}
var getLastLoader = function(self){
var lastLoader = self.loaders[self.loaderIndex+1];
if(!lastLoader) return false;
if(!lastLoader.path) return false;
var path = lastLoader.path.split("\\");
var lastloader = path[path.length-2];
if(!lastloader) return false;
return lastloader;
}
var parseQuery = function(query){
if(!query) return false;
var params = querystring.parse(query.replace("?",""));
return params;
}
var resolveType = function(self){
var lastloader = getLastLoader(self);
switch(lastloader){
case "json-loader": return "json";
case "style-loader": return "style";
}
return "string";
}
var transformers = {
json(uncompressed){
return `JSON.parse(${uncompressed})`;
},
string(uncompressed){
return `${uncompressed}`;
}
}
var inlineDecompress = function(compressed,params){
if(params && params.to && zippers[params.to]){
return zippers[params.to].decompress(compressed);
}
return zippers.base64.decompress(compressed);
}
var inliner = function(compressed,type,params){
var transformer = transformers[type] || transformers["string"];
var resolved = transformer(inlineDecompress(compressed,params));
return `
var lzstr = require("lz-string");
module.exports = ${resolved};
`;
}
module.exports = function(content) {
var self = this;
self.cacheable && self.cacheable();
var params = parseQuery(self.query);
var type = resolveType(self);
var content = resolveContent(self,content,type);
var compressed = compressContent(content,params);
self.value = [compressed];
var inlined = inliner(compressed,type,params);
return inlined;
}