Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Created May 24, 2018 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheLarkInn/ab3bac8116079564f39064e416880027 to your computer and use it in GitHub Desktop.
Save TheLarkInn/ab3bac8116079564f39064e416880027 to your computer and use it in GitHub Desktop.
Super primitive BomPrependingPlugin
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