forked from SparkDevNetwork/Rock
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwebpack-eol-plugin.js
More file actions
49 lines (42 loc) · 1.81 KB
/
webpack-eol-plugin.js
File metadata and controls
49 lines (42 loc) · 1.81 KB
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
const eol = require("eol");
const sources = require("webpack-sources");
const pluginName = "WebpackEolPlugin";
/**
* This plugin forces outputs from webpack to use CRLF instead of OS native.
* Fixes issues with git thinking files are different when they are not.
*/
class WebpackEolPlugin {
apply(compilation) {
compilation.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.afterOptimizeAssets.tap(
{
name: pluginName,
stage: compilation.PROCESS_ASSETS_STAGE_DEV_TOOL
},
(assets) => {
for (let i in assets) {
if (i.endsWith(".js.map")) {
continue;
}
const asset = compilation.getAsset(i); // <- standardized version of asset object
let source = asset.source.source(); // <- standardized way of getting asset source
// Special case for SVG files. We know they are text
// but because they are an asset, webpack treats them
// as a binary blob.
if (i.endsWith(".svg") && typeof source === "object" && source instanceof Buffer) {
source = source.toString();
}
if (typeof source !== "string") {
continue;
}
// standardized way of updating asset source
compilation.updateAsset(
i,
new sources.RawSource(eol.crlf(source))
);
}
});
});
}
}
module.exports = WebpackEolPlugin;