|
@@ -7,18 +7,29 @@ var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file); };
|
|
|
var ref = require('chalk');
|
|
var ref = require('chalk');
|
|
|
var red = ref.red;
|
|
var red = ref.red;
|
|
|
var yellow = ref.yellow;
|
|
var yellow = ref.yellow;
|
|
|
|
|
+var webpack = require('webpack');
|
|
|
|
|
|
|
|
var prefix = "[vue-server-renderer-webpack-plugin]";
|
|
var prefix = "[vue-server-renderer-webpack-plugin]";
|
|
|
var warn = exports.warn = function (msg) { return console.error(red((prefix + " " + msg + "\n"))); };
|
|
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 tip = exports.tip = function (msg) { return console.log(yellow((prefix + " " + msg + "\n"))); };
|
|
|
|
|
|
|
|
|
|
+var isWebpack5 = !!(webpack.version && webpack.version[0] > 4);
|
|
|
|
|
+
|
|
|
var validate = function (compiler) {
|
|
var validate = function (compiler) {
|
|
|
if (compiler.options.target !== 'node') {
|
|
if (compiler.options.target !== 'node') {
|
|
|
warn('webpack config `target` should be "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.output) {
|
|
|
|
|
+ if (compiler.options.output.library) {
|
|
|
|
|
+ // Webpack >= 5.0.0
|
|
|
|
|
+ if (compiler.options.output.library.type !== 'commonjs2') {
|
|
|
|
|
+ warn('webpack config `output.library.type` should be "commonjs2".');
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (compiler.options.output.libraryTarget !== 'commonjs2') {
|
|
|
|
|
+ // Webpack < 5.0.0
|
|
|
|
|
+ warn('webpack config `output.libraryTarget` should be "commonjs2".');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!compiler.options.externals) {
|
|
if (!compiler.options.externals) {
|
|
@@ -29,8 +40,20 @@ var validate = function (compiler) {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-var onEmit = function (compiler, name, hook) {
|
|
|
|
|
- if (compiler.hooks) {
|
|
|
|
|
|
|
+var onEmit = function (compiler, name, stageName, hook) {
|
|
|
|
|
+ if (isWebpack5) {
|
|
|
|
|
+ // Webpack >= 5.0.0
|
|
|
|
|
+ compiler.hooks.compilation.tap(name, function (compilation) {
|
|
|
|
|
+ if (compilation.compiler !== compiler) {
|
|
|
|
|
+ // Ignore child compilers
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ var stage = webpack.Compilation[stageName];
|
|
|
|
|
+ compilation.hooks.processAssets.tapAsync({ name: name, stage: stage }, function (assets, cb) {
|
|
|
|
|
+ hook(compilation, cb);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ } else if (compiler.hooks) {
|
|
|
// Webpack >= 4.0.0
|
|
// Webpack >= 4.0.0
|
|
|
compiler.hooks.emit.tapAsync(name, hook);
|
|
compiler.hooks.emit.tapAsync(name, hook);
|
|
|
} else {
|
|
} else {
|
|
@@ -39,6 +62,13 @@ var onEmit = function (compiler, name, hook) {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+var getAssetName = function (asset) {
|
|
|
|
|
+ if (typeof asset === 'string') {
|
|
|
|
|
+ return asset
|
|
|
|
|
+ }
|
|
|
|
|
+ return asset.name
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
var VueSSRServerPlugin = function VueSSRServerPlugin (options) {
|
|
var VueSSRServerPlugin = function VueSSRServerPlugin (options) {
|
|
|
if ( options === void 0 ) options = {};
|
|
if ( options === void 0 ) options = {};
|
|
|
|
|
|
|
@@ -52,7 +82,8 @@ VueSSRServerPlugin.prototype.apply = function apply (compiler) {
|
|
|
|
|
|
|
|
validate(compiler);
|
|
validate(compiler);
|
|
|
|
|
|
|
|
- onEmit(compiler, 'vue-server-plugin', function (compilation, cb) {
|
|
|
|
|
|
|
+ var stage = 'PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER';
|
|
|
|
|
+ onEmit(compiler, 'vue-server-plugin', stage, function (compilation, cb) {
|
|
|
var stats = compilation.getStats().toJson();
|
|
var stats = compilation.getStats().toJson();
|
|
|
var entryName = Object.keys(stats.entrypoints)[0];
|
|
var entryName = Object.keys(stats.entrypoints)[0];
|
|
|
var entryInfo = stats.entrypoints[entryName];
|
|
var entryInfo = stats.entrypoints[entryName];
|
|
@@ -62,7 +93,9 @@ VueSSRServerPlugin.prototype.apply = function apply (compiler) {
|
|
|
return cb()
|
|
return cb()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var entryAssets = entryInfo.assets.filter(isJS);
|
|
|
|
|
|
|
+ var entryAssets = entryInfo.assets
|
|
|
|
|
+ .map(getAssetName)
|
|
|
|
|
+ .filter(isJS);
|
|
|
|
|
|
|
|
if (entryAssets.length > 1) {
|
|
if (entryAssets.length > 1) {
|
|
|
throw new Error(
|
|
throw new Error(
|
|
@@ -84,14 +117,14 @@ VueSSRServerPlugin.prototype.apply = function apply (compiler) {
|
|
|
maps: {}
|
|
maps: {}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- stats.assets.forEach(function (asset) {
|
|
|
|
|
- if (isJS(asset.name)) {
|
|
|
|
|
- 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());
|
|
|
|
|
|
|
+ Object.keys(compilation.assets).forEach(function (name) {
|
|
|
|
|
+ if (isJS(name)) {
|
|
|
|
|
+ bundle.files[name] = compilation.assets[name].source();
|
|
|
|
|
+ } else if (name.match(/\.js\.map$/)) {
|
|
|
|
|
+ bundle.maps[name.replace(/\.map$/, '')] = JSON.parse(compilation.assets[name].source());
|
|
|
}
|
|
}
|
|
|
// do not emit anything else for server
|
|
// do not emit anything else for server
|
|
|
- delete compilation.assets[asset.name];
|
|
|
|
|
|
|
+ delete compilation.assets[name];
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
var json = JSON.stringify(bundle, null, 2);
|
|
var json = JSON.stringify(bundle, null, 2);
|