Created
May 24, 2018 19:32
-
-
Save TheLarkInn/ab3bac8116079564f39064e416880027 to your computer and use it in GitHub Desktop.
Super primitive BomPrependingPlugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pluginMeta = { name: "BomPlugin" }; | |
class BomPlugin { | |
constructor(encoding = "\ufeff") { | |
this.encoding = encoding; | |
} | |
apply(/** @type {import("webpack/lib/Compiler")}*/ compiler) { | |
compiler.hooks.compilation.tap(pluginMeta, ( | |
/** @type {import("webpack/lib/Compilation")}*/ compilation, | |
params | |
) => { | |
compilation.hooks.afterOptimizeAssets.tap(pluginMeta, assets => { | |
for (let fileName in assets) { | |
if (fileName.endsWith(".js")) { | |
const bomEncodedAsset = new ConcatSource( | |
this.encoding, // BOM for UTF-8 and UTF-16 | |
assets[fileName] | |
); | |
assets[fileName] = bomEncodedAsset; | |
} | |
} | |
}); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment