vue/packages/vue-server-renderer/client-plugin.js

95 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-04-26 18:32:30 +08:00
'use strict';
/* */
var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
2018-12-03 05:12:52 +08:00
var isCSS = function (file) { return /\.css(\?[^.]+)?$/.test(file); };
2017-04-26 18:32:30 +08:00
var ref = require('chalk');
var red = ref.red;
var yellow = ref.yellow;
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"))); };
2018-12-03 05:12:52 +08:00
var onEmit = function (compiler, name, hook) {
if (compiler.hooks) {
// Webpack >= 4.0.0
compiler.hooks.emit.tapAsync(name, hook);
} else {
// Webpack < 4.0.0
compiler.plugin('emit', hook);
}
};
2017-04-26 18:32:30 +08:00
var hash = require('hash-sum');
2017-05-02 15:58:34 +08:00
var uniq = require('lodash.uniq');
2018-12-03 05:12:52 +08:00
2017-04-26 18:32:30 +08:00
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;
2018-12-03 05:12:52 +08:00
onEmit(compiler, 'vue-client-plugin', function (compilation, cb) {
2017-04-26 18:32:30 +08:00
var stats = compilation.getStats().toJson();
2017-05-02 15:58:34 +08:00
var allFiles = uniq(stats.assets
.map(function (a) { return a.name; }));
2017-04-26 18:32:30 +08:00
2017-05-02 15:58:34 +08:00
var initialFiles = uniq(Object.keys(stats.entrypoints)
2017-04-26 18:32:30 +08:00
.map(function (name) { return stats.entrypoints[name].assets; })
.reduce(function (assets, all) { return all.concat(assets); }, [])
2018-12-03 05:12:52 +08:00
.filter(function (file) { return isJS(file) || isCSS(file); }));
2017-04-26 18:32:30 +08:00
var asyncFiles = allFiles
2018-12-03 05:12:52 +08:00
.filter(function (file) { return isJS(file) || isCSS(file); })
2017-04-26 18:32:30 +08:00
.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 (file) { return manifest.all.indexOf(file); };
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; });
2017-05-02 15:58:34 +08:00
if (!chunk || !chunk.files) {
return
}
2018-12-03 05:12:52 +08:00
var id = m.identifier.replace(/\s\w+$/, ''); // remove appended hash
var files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex);
2017-04-26 18:32:30 +08:00
// 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;