server-plugin.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 entryAssets = stats.entrypoints[entryName].assets.filter(isJS);
  37. if (entryAssets.length > 1) {
  38. throw new Error(
  39. "Server-side bundle should have one single entry file. " +
  40. "Avoid using CommonsChunkPlugin in the server config."
  41. )
  42. }
  43. var entry = entryAssets[0];
  44. if (!entry || typeof entry !== 'string') {
  45. throw new Error(
  46. ("Entry \"" + entryName + "\" not found. Did you specify the correct entry option?")
  47. )
  48. }
  49. var bundle = {
  50. entry: entry,
  51. files: {},
  52. maps: {}
  53. };
  54. stats.assets.forEach(function (asset) {
  55. if (asset.name.match(/\.js$/)) {
  56. bundle.files[asset.name] = compilation.assets[asset.name].source();
  57. } else if (asset.name.match(/\.js\.map$/)) {
  58. bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source());
  59. }
  60. // do not emit anything else for server
  61. delete compilation.assets[asset.name];
  62. });
  63. var json = JSON.stringify(bundle, null, 2);
  64. var filename = this$1.options.filename;
  65. compilation.assets[filename] = {
  66. source: function () { return json; },
  67. size: function () { return json.length; }
  68. };
  69. cb();
  70. });
  71. };
  72. module.exports = VueSSRServerPlugin;