| 
									
										
										
										
											2017-04-26 18:32:30 +08:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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"))); }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var validate = function (compiler) { | 
					
						
							|  |  |  |   if (compiler.options.target !== 'node') { | 
					
						
							|  |  |  |     warn('webpack config `target` should be "node".'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (compiler.options.output && compiler.options.output.libraryTarget !== 'commonjs2') { | 
					
						
							|  |  |  |     warn('webpack config `output.libraryTarget` should be "commonjs2".'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!compiler.options.externals) { | 
					
						
							|  |  |  |     tip( | 
					
						
							|  |  |  |       'It is recommended to externalize dependencies in the server build for ' + | 
					
						
							|  |  |  |       'better build performance.' | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 VueSSRServerPlugin = function VueSSRServerPlugin (options) { | 
					
						
							|  |  |  |   if ( options === void 0 ) options = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   this.options = Object.assign({ | 
					
						
							|  |  |  |     filename: 'vue-ssr-server-bundle.json' | 
					
						
							|  |  |  |   }, options); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | VueSSRServerPlugin.prototype.apply = function apply (compiler) { | 
					
						
							|  |  |  |     var this$1 = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   validate(compiler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 05:12:52 +08:00
										 |  |  |   onEmit(compiler, 'vue-server-plugin', function (compilation, cb) { | 
					
						
							| 
									
										
										
										
											2017-04-26 18:32:30 +08:00
										 |  |  |     var stats = compilation.getStats().toJson(); | 
					
						
							|  |  |  |     var entryName = Object.keys(stats.entrypoints)[0]; | 
					
						
							| 
									
										
										
										
											2017-05-02 15:58:34 +08:00
										 |  |  |     var entryInfo = stats.entrypoints[entryName]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!entryInfo) { | 
					
						
							|  |  |  |       // #5553
 | 
					
						
							|  |  |  |       return cb() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var entryAssets = entryInfo.assets.filter(isJS); | 
					
						
							| 
									
										
										
										
											2017-04-26 18:32:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (entryAssets.length > 1) { | 
					
						
							|  |  |  |       throw new Error( | 
					
						
							|  |  |  |         "Server-side bundle should have one single entry file. " + | 
					
						
							|  |  |  |         "Avoid using CommonsChunkPlugin in the server config." | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var entry = entryAssets[0]; | 
					
						
							|  |  |  |     if (!entry || typeof entry !== 'string') { | 
					
						
							|  |  |  |       throw new Error( | 
					
						
							|  |  |  |         ("Entry \"" + entryName + "\" not found. Did you specify the correct entry option?") | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var bundle = { | 
					
						
							|  |  |  |       entry: entry, | 
					
						
							|  |  |  |       files: {}, | 
					
						
							|  |  |  |       maps: {} | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stats.assets.forEach(function (asset) { | 
					
						
							| 
									
										
										
										
											2018-12-03 05:12:52 +08:00
										 |  |  |       if (isJS(asset.name)) { | 
					
						
							| 
									
										
										
										
											2017-04-26 18:32:30 +08:00
										 |  |  |         bundle.files[asset.name] = compilation.assets[asset.name].source(); | 
					
						
							|  |  |  |       } else if (asset.name.match(/\.js\.map$/)) { | 
					
						
							|  |  |  |         bundle.maps[asset.name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[asset.name].source()); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       // do not emit anything else for server
 | 
					
						
							|  |  |  |       delete compilation.assets[asset.name]; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var json = JSON.stringify(bundle, null, 2); | 
					
						
							|  |  |  |     var filename = this$1.options.filename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     compilation.assets[filename] = { | 
					
						
							|  |  |  |       source: function () { return json; }, | 
					
						
							|  |  |  |       size: function () { return json.length; } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cb(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = VueSSRServerPlugin; |