UNPKG

3.1 kBJavaScriptView Raw
1'use strict';
2
3/* */
4
5var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
6
7var isCSS = function (file) { return /\.css(\?[^.]+)?$/.test(file); };
8
9var ref = require('chalk');
10var red = ref.red;
11var yellow = ref.yellow;
12
13var prefix = "[vue-server-renderer-webpack-plugin]";
14var warn = exports.warn = function (msg) { return console.error(red((prefix + " " + msg + "\n"))); };
15var tip = exports.tip = function (msg) { return console.log(yellow((prefix + " " + msg + "\n"))); };
16
17var onEmit = function (compiler, name, hook) {
18 if (compiler.hooks) {
19 // Webpack >= 4.0.0
20 compiler.hooks.emit.tapAsync(name, hook);
21 } else {
22 // Webpack < 4.0.0
23 compiler.plugin('emit', hook);
24 }
25};
26
27var hash = require('hash-sum');
28var uniq = require('lodash.uniq');
29
30var VueSSRClientPlugin = function VueSSRClientPlugin (options) {
31 if ( options === void 0 ) options = {};
32
33 this.options = Object.assign({
34 filename: 'vue-ssr-client-manifest.json'
35 }, options);
36};
37
38VueSSRClientPlugin.prototype.apply = function apply (compiler) {
39 var this$1 = this;
40
41 onEmit(compiler, 'vue-client-plugin', function (compilation, cb) {
42 var stats = compilation.getStats().toJson();
43
44 var allFiles = uniq(stats.assets
45 .map(function (a) { return a.name; }));
46
47 var initialFiles = uniq(Object.keys(stats.entrypoints)
48 .map(function (name) { return stats.entrypoints[name].assets; })
49 .reduce(function (assets, all) { return all.concat(assets); }, [])
50 .filter(function (file) { return isJS(file) || isCSS(file); }));
51
52 var asyncFiles = allFiles
53 .filter(function (file) { return isJS(file) || isCSS(file); })
54 .filter(function (file) { return initialFiles.indexOf(file) < 0; });
55
56 var manifest = {
57 publicPath: stats.publicPath,
58 all: allFiles,
59 initial: initialFiles,
60 async: asyncFiles,
61 modules: { /* [identifier: string]: Array<index: number> */ }
62 };
63
64 var assetModules = stats.modules.filter(function (m) { return m.assets.length; });
65 var fileToIndex = function (file) { return manifest.all.indexOf(file); };
66 stats.modules.forEach(function (m) {
67 // ignore modules duplicated in multiple chunks
68 if (m.chunks.length === 1) {
69 var cid = m.chunks[0];
70 var chunk = stats.chunks.find(function (c) { return c.id === cid; });
71 if (!chunk || !chunk.files) {
72 return
73 }
74 var id = m.identifier.replace(/\s\w+$/, ''); // remove appended hash
75 var files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex);
76 // find all asset modules associated with the same chunk
77 assetModules.forEach(function (m) {
78 if (m.chunks.some(function (id) { return id === cid; })) {
79 files.push.apply(files, m.assets.map(fileToIndex));
80 }
81 });
82 }
83 });
84
85 var json = JSON.stringify(manifest, null, 2);
86 compilation.assets[this$1.options.filename] = {
87 source: function () { return json; },
88 size: function () { return json.length; }
89 };
90 cb();
91 });
92};
93
94module.exports = VueSSRClientPlugin;