forked from rupurt/webpack-sprockets-rails-manifest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (56 loc) · 1.94 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var fs = require("fs");
var fse = require("fs-extra");
var DEFAULT_PARAMS = {
manifestFile: "../../config/sprockets-manifest.json",
statsFile: "webpack-stats.json"
};
function WebpackSprocketsRailsManifestPlugin(options) {
var params = options || {};
this._manifestFile = params.manifestFile || DEFAULT_PARAMS.manifestFile;
this._statsFile = params.statsFile || DEFAULT_PARAMS.statsFile;
}
WebpackSprocketsRailsManifestPlugin.prototype.apply = function(compiler) {
var manifestFile = this._manifestFile;
var statsFile = this._statsFile;
var sprockets = {
files: {},
assets: {}
};
compiler.plugin("done", function(stats) {
var statsJson = stats.toJson();
var chunks = statsJson.chunks;
var devServer = compiler.options.devServer;
var outputPath;
if (devServer && devServer.contentBase) {
outputPath = devServer.contentBase;
} else {
outputPath = compiler.options.output.path;
}
var manifestPath = outputPath + "/" + manifestFile;
chunks.forEach(function(chunk) {
chunk.files.forEach(function (chunkFilename) {
var chunkPath = outputPath + "/" + chunkFilename;
var chunkExtension = chunkFilename.split(".").pop();
var logicalPath = chunk.names[0] + "." + chunkExtension;
var mtime;
if (fs.existsSync(chunkPath)) {
mtime = fs.statSync(chunkPath).mtime;
} else {
mtime = new Date();
}
sprockets.files[chunkFilename] = {
"logical_path": logicalPath,
"mtime": mtime.toISOString(),
"size": chunk.size,
"digest": chunk.hash,
// TODO
// "integrity": "sha256-Zk2O+Q1SFSuzslxNc6LuqFrAN5PlRHlbKeGzXfN4Xmc="
};
sprockets.assets[logicalPath] = chunkFilename;
});
});
fse.mkdirpSync(outputPath);
fse.outputFileSync(manifestPath, JSON.stringify(sprockets, null, " "));
});
};
module.exports = WebpackSprocketsRailsManifestPlugin;