2
0

server-plugin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 onEmit = function (compiler, name, hook) {
  25. if (compiler.hooks) {
  26. // Webpack >= 4.0.0
  27. compiler.hooks.emit.tapAsync(name, hook);
  28. } else {
  29. // Webpack < 4.0.0
  30. compiler.plugin('emit', hook);
  31. }
  32. };
  33. var VueSSRServerPlugin = function VueSSRServerPlugin (options) {
  34. if ( options === void 0 ) options = {};
  35. this.options = Object.assign({
  36. filename: 'vue-ssr-server-bundle.json'
  37. }, options);
  38. };
  39. VueSSRServerPlugin.prototype.apply = function apply (compiler) {
  40. var this$1 = this;
  41. validate(compiler);
  42. onEmit(compiler, 'vue-server-plugin', function (compilation, cb) {
  43. var stats = compilation.getStats().toJson();
  44. var entryName = Object.keys(stats.entrypoints)[0];
  45. var entryInfo = stats.entrypoints[entryName];
  46. if (!entryInfo) {
  47. // #5553
  48. return cb()
  49. }
  50. var entryAssets = entryInfo.assets.filter(isJS);
  51. if (entryAssets.length > 1) {
  52. throw new Error(
  53. "Server-side bundle should have one single entry file. " +
  54. "Avoid using CommonsChunkPlugin in the server config."
  55. )
  56. }
  57. var entry = entryAssets[0];
  58. if (!entry || typeof entry !== 'string') {
  59. throw new Error(
  60. ("Entry \"" + entryName + "\" not found. Did you specify the correct entry option?")
  61. )
  62. }
  63. var bundle = {
  64. entry: entry,
  65. files: {},
  66. maps: {}
  67. };
  68. stats.assets.forEach(function (asset) {
  69. if (isJS(asset.name)) {
  70. bundle.files[asset.name] = compilation.assets[asset.name].source();
  71. } else if (asset.name.match(/\.js\.map$/)) {
  72. bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source());
  73. }
  74. // do not emit anything else for server
  75. delete compilation.assets[asset.name];
  76. });
  77. var json = JSON.stringify(bundle, null, 2);
  78. var filename = this$1.options.filename;
  79. compilation.assets[filename] = {
  80. source: function () { return json; },
  81. size: function () { return json.length; }
  82. };
  83. cb();
  84. });
  85. };
  86. module.exports = VueSSRServerPlugin;