-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (39 loc) · 1.18 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
/* eslint-disable no-param-reassign */
import { getOptions } from 'loader-utils';
module.exports = function (content) {
const options = getOptions(this);
const loaderReg = {
server: {
beginReg: /\/\/\s*(only\s*)?client\s*js\s*begin/,
endReg: /\/\/\s*(only\s*)?client\s*js\s*end/,
},
client: {
beginReg: /\/\/\s*(only\s*)?server\s*js\s*begin/,
endReg: /\/\/\s*(only\s*)?server\s*js\s*end/,
},
};
return splitOnlyLoader(content, options.isServer ? loaderReg.server : loaderReg.client);
};
// client js begin
// client js end
// server js begin
// server js end
function splitOnlyLoader(content, { beginReg, endReg }) {
let finish = false;
let tempContent = '';
while (!finish) {
const begin = content.search(beginReg);
const end = content.search(endReg);
if (begin === -1 || end === -1) break;
if (begin > end) {
this.emitError(new Error('js end comments in front of begin comments'));
} else if (end > begin) {
tempContent += content.slice(0, begin);
content = content.slice(end + content.match(endReg)[0].length);
} else {
finish = true;
}
}
tempContent += content;
return tempContent;
}