1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
|
6 |
|
7 | var isCSS = function (file) { return /\.css(\?[^.]+)?$/.test(file); };
|
8 |
|
9 | var ref = require('chalk');
|
10 | var red = ref.red;
|
11 | var yellow = ref.yellow;
|
12 |
|
13 | var prefix = "[vue-server-renderer-webpack-plugin]";
|
14 | var warn = exports.warn = function (msg) { return console.error(red((prefix + " " + msg + "\n"))); };
|
15 | var tip = exports.tip = function (msg) { return console.log(yellow((prefix + " " + msg + "\n"))); };
|
16 |
|
17 | var onEmit = function (compiler, name, hook) {
|
18 | if (compiler.hooks) {
|
19 |
|
20 | compiler.hooks.emit.tapAsync(name, hook);
|
21 | } else {
|
22 |
|
23 | compiler.plugin('emit', hook);
|
24 | }
|
25 | };
|
26 |
|
27 | var hash = require('hash-sum');
|
28 | var uniq = require('lodash.uniq');
|
29 |
|
30 | var 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 |
|
38 | VueSSRClientPlugin.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: { }
|
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 |
|
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+$/, '');
|
75 | var files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex);
|
76 |
|
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 |
|
94 | module.exports = VueSSRClientPlugin;
|