diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-08 13:54:04 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-08 13:54:04 +0530 |
| commit | 4993aaf41e132dd0805bf6c487901d9f2aecfcfe (patch) | |
| tree | a51ebf7bff551e9188d68f777d100e8ffb70922d /config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js | |
| parent | 8234acb45cd99fc7bcd99df7f71cac02845bb534 (diff) | |
| download | banglar-bhumi-utils-4993aaf41e132dd0805bf6c487901d9f2aecfcfe.tar.gz banglar-bhumi-utils-4993aaf41e132dd0805bf6c487901d9f2aecfcfe.zip | |
Combining similar scripts into 1 for improved performence and less files in build.
Diffstat (limited to 'config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js')
| -rw-r--r-- | config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js b/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js new file mode 100644 index 0000000..61ce804 --- /dev/null +++ b/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js @@ -0,0 +1,56 @@ +const { sources } = require("webpack"); + +const propectualIndexOf = (string, characterList, start) => { + if (start > 0) { + let i = 0; + let x = start; + while (i < characterList.length) { + x = string.indexOf(characterList[i], x + 1); + if (x < 0) { + return -1; + } + i++; + } + 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 = propectualIndexOf(currentAssetContent, ["\n/***/", "\n/***/", "\n"], previouslyEnded + 1); + previouslyEnded = propectualIndexOf(currentAssetContent, ["\n/***/"], start + 1); + if (previouslyEnded < 0) { + break; + } else { + code += currentAssetContent.substring(start, previouslyEnded); + } + } + compilation.updateAsset( + assetName, + new sources.RawSource(code) + ); + } + } + } + ); + }); + } +} + +module.exports = SanitizeScriptsImportPlugin; |
