server-plugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. /* */
  3. var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
  4. var ref = require('chalk');
  5. var red = ref.red;
  6. var yellow = ref.yellow;
  7. var prefix = "[vue-server-renderer-webpack-plugin]";
  8. var warn = exports.warn = function (msg) { return console.error(red((prefix + " " + msg + "\n"))); };
  9. var tip = exports.tip = function (msg) { return console.log(yellow((prefix + " " + msg + "\n"))); };
  10. var validate = function (compiler) {
  11. if (compiler.options.target !== 'node') {
  12. warn('webpack config `target` should be "node".');
  13. }
  14. if (compiler.options.output && compiler.options.output.libraryTarget !== 'commonjs2') {
  15. warn('webpack config `output.libraryTarget` should be "commonjs2".');
  16. }
  17. if (!compiler.options.externals) {
  18. tip(
  19. 'It is recommended to externalize dependencies in the server build for ' +
  20. 'better build performance.'
  21. );
  22. }
  23. };
  24. var VueSSRServerPlugin = function VueSSRServerPlugin (options) {
  25. if ( options === void 0 ) options = {};
  26. this.options = Object.assign({
  27. filename: 'vue-ssr-server-bundle.json'
  28. }, options);
  29. };
  30. VueSSRServerPlugin.prototype.apply = function apply (compiler) {
  31. var this$1 = this;
  32. validate(compiler);
  33. compiler.plugin('emit', function (compilation, cb) {
  34. var stats = compilation.getStats().toJson();
  35. var entryName = Object.keys(stats.entrypoints)[0];
  36. var entryInfo = stats.entrypoints[entryName];
  37. if (!entryInfo) {
  38. // #5553
  39. return cb()
  40. }
  41. var entryAssets = entryInfo.assets.filter(isJS);
  42. if (entryAssets.length > 1) {
  43. throw new Error(
  44. "Server-side bundle should have one single entry file. " +
  45. "Avoid using CommonsChunkPlugin in the server config."
  46. )
  47. }
  48. var entry = entryAssets[0];
  49. if (!entry || typeof entry !== 'string') {
  50. throw new Error(
  51. ("Entry \"" + entryName + "\" not found. Did you specify the correct entry option?")
  52. )
  53. }
  54. var bundle = {
  55. entry: entry,
  56. files: {},
  57. maps: {}
  58. };
  59. stats.assets.forEach(function (asset) {
  60. if (asset.name.match(/\.js$/)) {
  61. bundle.files[asset.name] = compilation.assets[asset.name].source();
  62. } else if (asset.name.match(/\.js\.map$/)) {
  63. bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source());
  64. }
  65. // do not emit anything else for server
  66. delete compilation.assets[asset.name];
  67. });
  68. var json = JSON.stringify(bundle, null, 2);
  69. var filename = this$1.options.filename;
  70. compilation.assets[filename] = {
  71. source: function () { return json; },
  72. size: function () { return json.length; }
  73. };
  74. cb();
  75. });
  76. };
  77. module.exports = VueSSRServerPlugin;