-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-plugin.js
127 lines (106 loc) · 3.91 KB
/
client-plugin.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
'use strict';
/* */
var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
var isCSS = function (file) { return /\.css(\?[^.]+)?$/.test(file); };
var ref = require('chalk');
var red = ref.red;
var yellow = ref.yellow;
var webpack = require('webpack');
var prefix = "[vue-server-renderer-webpack-plugin]";
var warn = exports.warn = function (msg) { return console.error(red((prefix + " " + msg + "\n"))); };
var tip = exports.tip = function (msg) { return console.log(yellow((prefix + " " + msg + "\n"))); };
var isWebpack5 = !!(webpack.version && webpack.version[0] > 4);
var onEmit = function (compiler, name, stageName, hook) {
if (isWebpack5) {
// Webpack >= 5.0.0
compiler.hooks.compilation.tap(name, function (compilation) {
if (compilation.compiler !== compiler) {
// Ignore child compilers
return
}
var stage = webpack.Compilation[stageName];
compilation.hooks.processAssets.tapAsync({ name: name, stage: stage }, function (assets, cb) {
hook(compilation, cb);
});
});
} else if (compiler.hooks) {
// Webpack >= 4.0.0
compiler.hooks.emit.tapAsync(name, hook);
} else {
// Webpack < 4.0.0
compiler.plugin('emit', hook);
}
};
var stripModuleIdHash = function (id) {
if (isWebpack5) {
// Webpack >= 5.0.0
return id.replace(/\|\w+$/, '')
}
// Webpack < 5.0.0
return id.replace(/\s\w+$/, '')
};
var getAssetName = function (asset) {
if (typeof asset === 'string') {
return asset
}
return asset.name
};
var hash = require('hash-sum');
var uniq = require('lodash.uniq');
var VueSSRClientPlugin = function VueSSRClientPlugin (options) {
if ( options === void 0 ) options = {};
this.options = Object.assign({
filename: 'vue-ssr-client-manifest.json'
}, options);
};
VueSSRClientPlugin.prototype.apply = function apply (compiler) {
var this$1 = this;
var stage = 'PROCESS_ASSETS_STAGE_ADDITIONAL';
onEmit(compiler, 'vue-client-plugin', stage, function (compilation, cb) {
var stats = compilation.getStats().toJson();
var allFiles = uniq(stats.assets
.map(function (a) { return a.name; }));
var initialFiles = uniq(Object.keys(stats.entrypoints)
.map(function (name) { return stats.entrypoints[name].assets; })
.reduce(function (assets, all) { return all.concat(assets); }, [])
.map(getAssetName)
.filter(function (file) { return isJS(file) || isCSS(file); }));
var asyncFiles = allFiles
.filter(function (file) { return isJS(file) || isCSS(file); })
.filter(function (file) { return initialFiles.indexOf(file) < 0; });
var manifest = {
publicPath: stats.publicPath,
all: allFiles,
initial: initialFiles,
async: asyncFiles,
modules: { /* [identifier: string]: Array<index: number> */ }
};
var assetModules = stats.modules.filter(function (m) { return m.assets.length; });
var fileToIndex = function (asset) { return manifest.all.indexOf(getAssetName(asset)); };
stats.modules.forEach(function (m) {
// ignore modules duplicated in multiple chunks
if (m.chunks.length === 1) {
var cid = m.chunks[0];
var chunk = stats.chunks.find(function (c) { return c.id === cid; });
if (!chunk || !chunk.files) {
return
}
var id = stripModuleIdHash(m.identifier);
var files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex);
// find all asset modules associated with the same chunk
assetModules.forEach(function (m) {
if (m.chunks.some(function (id) { return id === cid; })) {
files.push.apply(files, m.assets.map(fileToIndex));
}
});
}
});
var json = JSON.stringify(manifest, null, 2);
compilation.assets[this$1.options.filename] = {
source: function () { return json; },
size: function () { return json.length; }
};
cb();
});
};
module.exports = VueSSRClientPlugin;