-
Notifications
You must be signed in to change notification settings - Fork 811
/
Manifest.js
136 lines (103 loc) · 2.71 KB
/
Manifest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
let objectValues = require('lodash').values;
let without = require('lodash').without;
let path = require('path');
class Manifest {
/**
* Create a new Manifest instance.
*
* @param {string} name
*/
constructor(name = 'mix-manifest.json') {
this.manifest = {};
this.name = name;
}
/**
* Get the underlying manifest collection.
*/
get(file = null) {
if (file) {
return path.join(
Config.publicPath,
this.manifest[this.normalizePath(file)]
);
}
return this.manifest;
}
/**
* Add the given path to the manifest file.
*
* @param {string} filePath
*/
add(filePath) {
filePath = this.normalizePath(filePath);
let original = filePath.replace(/\?id=\w{20}/, '');
this.manifest[original] = filePath;
return this;
}
/**
* Add a new hashed key to the manifest.
*
* @param {string} file
*/
hash(file) {
let hash = new File(path.join(Config.publicPath, file)).version();
this.manifest[file] = file + '?id=' + hash;
return this;
}
/**
* Transform the Webpack stats into the shape we need.
*
* @param {object} stats
*/
transform(stats) {
let customAssets = Config.customAssets.map(asset => asset.pathFromPublic());
this.flattenAssets(stats).concat(customAssets).forEach(this.add.bind(this));
return this;
}
/**
* Refresh the mix-manifest.js file.
*/
refresh() {
File.find(this.path()).write(this.manifest);
}
/**
* Retrieve the JSON output from the manifest file.
*/
read() {
return JSON.parse(File.find(this.path()).read());
}
/**
* Get the path to the manifest file.
*/
path() {
return path.join(Config.publicPath, this.name);
}
/**
* Flatten the generated stats assets into an array.
*
* @param {Object} stats
*/
flattenAssets(stats) {
let assets = Object.assign({}, stats.assetsByChunkName);
// If there's a temporary mix.js chunk, we can safely remove it.
if (assets.mix) {
assets.mix = without(assets.mix, 'mix.js');
}
return flatten(assets);
}
/**
* Prepare the provided path for processing.
*
* @param {string} filePath
*/
normalizePath(filePath) {
filePath = filePath.replace(
new RegExp('^' + Config.publicPath), ''
).replace(/\\/g, '/');
if (! filePath.startsWith('/')) {
filePath = '/' + filePath;
}
return filePath;
}
}
module.exports = Manifest;