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