forked from cydrobolt/nullchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.js
163 lines (130 loc) · 5.38 KB
/
replace.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
import MagicString from 'magic-string';
import { createFilter } from '@rollup/pluginutils';
import path from 'path';
const nodeNativeDeps = ['tls', 'crypto', 'http', 'fs', 'path', 'events', 'url', 'net', 'zlib', 'tty', 'querystring', 'util', 'buffer', 'domain', 'stream', 'os', 'https', 'string_decoder']
function escape(str) {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
}
function ensureFunction(functionOrValue) {
if (typeof functionOrValue === 'function') return functionOrValue;
return () => functionOrValue;
}
function longest(a, b) {
return b.length - a.length;
}
function getReplacements(options) {
if (options.values) {
return Object.assign({}, options.values);
}
const values = Object.assign({}, options);
delete values.delimiters;
delete values.include;
delete values.exclude;
delete values.sourcemap;
delete values.sourceMap;
return values;
}
function mapToFunctions(object) {
return Object.keys(object).reduce((fns, key) => {
const functions = Object.assign({}, fns);
functions[key] = ensureFunction(object[key]);
return functions;
}, {});
}
export default function replace(options = {}) {
const filter = createFilter(options.include, options.exclude);
const { delimiters } = options;
const replacements = {
// 'commonjsRequire.resolve': 'require.resolve', // workaround for promise.resolve usage and commonjs plugin
}
const optionalRegexTemplate = `[\\w\\s=_\\$\\(]*require\\((\\'|\\")LIBRARY_NAME(\\'|\\")\\)(.)*\\n`
// `(.|\\s|=_)*require\\((\\'|\\")LIBRARY_NAME(\\'|\\")\\)(.)*\\n`
const functionValues = mapToFunctions(getReplacements(replacements));
const keys = Object.keys(functionValues)
.sort(longest)
.map(escape);
const optionalDeps = [];
const pattern = delimiters
? new RegExp(`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g')
: new RegExp(`\\b(${keys.join('|')})\\b`, 'g');
return {
name: 'nodejs',
resolveId ( source ) {
if (!source.includes(path.sep) && nodeNativeDeps.includes(source.split('?')[0])){
return {id: source, external: true};
} else if (!source.includes(path.sep) && !nodeNativeDeps.includes(source.split('?')[0])){
optionalDeps.push(source.split('?')[0]);
return {id: source, external: true};
}
return null; // other ids should be handled as usually
},
renderChunk(code, chunk) {
const id = chunk.fileName;
if (!keys.length) return null;
if (!filter(id)) return null;
return executeReplacement(code, id);
},
transform(code, id) {
if (!keys.length) return null;
if (!filter(id)) return null;
return executeReplacement(code, id);
},
generateBundle(options, bundleInfo){
// remove same values
const finalOptionalDeps = Array.from(new Set(optionalDeps));
for(let [key, value] of Object.entries(bundleInfo)) {
finalOptionalDeps.forEach( optionalLibrary => {
console.log(getRegexFromLibraryName(optionalLibrary))
value.code = value.code.replace(getRegexFromLibraryName(optionalLibrary), '')
})
}
}
};
function getRegexFromLibraryName(libraryName){
return new RegExp(optionalRegexTemplate.replace('LIBRARY_NAME', libraryName));
}
function executeReplacementChunk(code, id){
const magicString = new MagicString(code);
// execute specific chunk replacement for optional deps
console.log(new RegExp(optionalRegexTemplate,'g'))
// if(code.includes('bufferutil'))
codeHasReplacements(code, id, magicString, new RegExp(optionalRegexTemplate,'g'))
if (!codeHasReplacements(code, id, magicString)) {
return null;
}
const result = { code: magicString.toString() };
if (isSourceMapEnabled()) {
result.map = magicString.generateMap({ hires: true });
}
return result;
}
function executeReplacement(code, id) {
const magicString = new MagicString(code);
if (!codeHasReplacements(code, id, magicString)) {
return null;
}
const result = { code: magicString.toString() };
if (isSourceMapEnabled()) {
result.map = magicString.generateMap({ hires: true });
}
return result;
}
function codeHasReplacements(code, id, magicString, specificPattern = undefined, replacement = '') {
let result = false;
let match;
const patternToExecute = specificPattern ? specificPattern : pattern;
// eslint-disable-next-line no-cond-assign
while ((match = patternToExecute.exec(code)) !== null) {
result = true;
const start = match.index;
const end = start + match[0].length;
const finalReplacment = specificPattern ? replacement : String(functionValues[match[1]](id));
if(match[1].includes('bufferutil')) console.log(finalReplacment)
magicString.overwrite(start, end, finalReplacment);
}
return result;
}
function isSourceMapEnabled() {
return options.sourceMap !== false && options.sourcemap !== false;
}
}