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
|
const { sources } = require("webpack");
const perpectualIndexOf = (string, characterList, start) => {
if (start > 0) {
let x = start;
for (let i = 0; i < characterList.length; i++) {
x = string.indexOf(characterList[i], x + 1);
if (x < 0) {
return -1;
}
}
return x;
} else {
return -1;
}
};
class SanitizeScriptsImportPlugin {
apply(compiler) {
compiler.hooks.compilation.tap("SanitizeScriptsImportPlugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "SanitizeScriptsImportPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
additionalAssets: true
},
assets => {
for (const assetName in assets) {
if (assetName.endsWith(".js")) {
const currentAssetContent = compilation.getAsset(assetName).source.source();
let previouslyEnded = 0;
let code = "";
while (true) {
const start = perpectualIndexOf(currentAssetContent, ["\n/***/", "\n/***/", "\n"], previouslyEnded + 1);
previouslyEnded = perpectualIndexOf(currentAssetContent, ["\n/***/"], start + 1);
if (previouslyEnded < 0) {
break;
} else {
code += currentAssetContent.substring(start, previouslyEnded);
}
}
compilation.updateAsset(
assetName,
new sources.RawSource(code)
);
}
}
}
);
});
}
}
module.exports = SanitizeScriptsImportPlugin;
|