question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vuejs/vue-ssr-webpack-plugin and webpack 5

See original GitHub issue

What problem does this feature solve?

github missing repository vuejs/vue-ssr-webpack-plugin, i update webpack to version 5 and have error [vue-server-renderer-webpack-plugin] webpack config output.libraryTarget should be “commonjs2”.

What does the proposed API look like?

in my output.libraryTarget is libraryTarget: ‘commonjs2’

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:46 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
trainiaccommented, Apr 10, 2021

The full set of changes I needed to make everything work.

diff --git a/node_modules/vue-server-renderer/build.dev.js b/node_modules/vue-server-renderer/build.dev.js
index fb4caf5..541941d 100644
--- a/node_modules/vue-server-renderer/build.dev.js
+++ b/node_modules/vue-server-renderer/build.dev.js
@@ -9060,12 +9060,12 @@ TemplateRenderer.prototype.renderScripts = function renderScripts (context) {
     var initial = this.preloadFiles.filter(function (ref) {
         var file = ref.file;
 
-        return isJS(file);
+        return isJS(file) && !file.endsWith('hot-update.js');
       });
     var async = (this.getUsedAsyncFiles(context) || []).filter(function (ref) {
         var file = ref.file;
-
-        return isJS(file);
+        // changed line
+        return isJS(file) && !file.endsWith('hot-update.js');
       });
     var needed = [initial[0]].concat(async, initial.slice(1));
     return needed.map(function (ref) {
diff --git a/node_modules/vue-server-renderer/client-plugin.js b/node_modules/vue-server-renderer/client-plugin.js
index 15cdcbd..7b091cc 100644
--- a/node_modules/vue-server-renderer/client-plugin.js
+++ b/node_modules/vue-server-renderer/client-plugin.js
@@ -45,9 +45,12 @@ VueSSRClientPlugin.prototype.apply = function apply (compiler) {
       .map(function (a) { return a.name; }));
 
     var initialFiles = uniq(Object.keys(stats.entrypoints)
-      .map(function (name) { return stats.entrypoints[name].assets; })
+      // changed line
+      .map(function (name) { return stats.entrypoints[name].assets.map(file => file.name); })
       .reduce(function (assets, all) { return all.concat(assets); }, [])
-      .filter(function (file) { return isJS(file) || isCSS(file); }));
+      .filter(function (file) {
+         return isJS(file) || isCSS(file);
+      }));
 
     var asyncFiles = allFiles
       .filter(function (file) { return isJS(file) || isCSS(file); })
@@ -71,7 +74,9 @@ VueSSRClientPlugin.prototype.apply = function apply (compiler) {
         if (!chunk || !chunk.files) {
           return
         }
-        var id = m.identifier.replace(/\s\w+$/, ''); // remove appended hash
+
+        // changed line
+        var id = m.identifier.replace(/\|\w+$/, ''); // remove appended hash
         var files = manifest.modules[hash(id)] = chunk.files.map(fileToIndex);
         // find all asset modules associated with the same chunk
         assetModules.forEach(function (m) {
diff --git a/node_modules/vue-server-renderer/server-plugin.js b/node_modules/vue-server-renderer/server-plugin.js
index 54ba2b3..121bdc4 100644
--- a/node_modules/vue-server-renderer/server-plugin.js
+++ b/node_modules/vue-server-renderer/server-plugin.js
@@ -17,7 +17,8 @@ var validate = function (compiler) {
     warn('webpack config `target` should be "node".');
   }
 
-  if (compiler.options.output && compiler.options.output.libraryTarget !== 'commonjs2') {
+  // changed line
+  if (compiler.options.output && compiler.options.output.library.type !== 'commonjs2') {
     warn('webpack config `output.libraryTarget` should be "commonjs2".');
   }
 
@@ -62,7 +63,8 @@ VueSSRServerPlugin.prototype.apply = function apply (compiler) {
       return cb()
     }
 
-    var entryAssets = entryInfo.assets.filter(isJS);
+    // changed line
+    var entryAssets = entryInfo.assets.filter(file => isJS(file.name));
 
     if (entryAssets.length > 1) {
       throw new Error(
@@ -72,14 +74,16 @@ VueSSRServerPlugin.prototype.apply = function apply (compiler) {
     }
 
     var entry = entryAssets[0];
-    if (!entry || typeof entry !== 'string') {
+    // changed line
+    if (!entry || typeof entry.name !== 'string') {
       throw new Error(
         ("Entry \"" + entryName + "\" not found. Did you specify the correct entry option?")
       )
     }
 
     var bundle = {
-      entry: entry,
+      // changed line
+      entry: entry.name,
       files: {},
       maps: {}
     };
  • manually make the edits above to the files in your node modules folder
  • npm install patch-package -D
  • npx patch-package vue-server-renderer

donezo

5reactions
efremenkovancommented, Oct 28, 2020

Also, I think it’s worth mentioning that even after my build succeeded I couldn’t launch my bundle due to this error:

TypeError: Cannot read property 'file' of undefined
    at {{YOUR_PATH_TO_PROJECT}}/node_modules/vue-server-renderer/build.dev.js:9074:24

The problem was in function that starts on line 9056:

TemplateRenderer.prototype.renderScripts = function renderScripts (context) {
  var this$1 = this;

  if (this.clientManifest) {
    var initial = this.preloadFiles.filter(function (ref) {
        var file = ref.file;

        return isJS(file);
      });
    var async = (this.getUsedAsyncFiles(context) || []).filter(function (ref) {
        var file = ref.file;

        return isJS(file);
      });
-   var needed = [initial[0]].concat(async, initial.slice(1));
+   var needed = (initial.length ? [initial[0]] : []).concat(async, initial.slice(1));
    return needed.map(function (ref) {
        var file = ref.file;

      return ("<script src=\"" + (this$1.publicPath) + file + "\" defer></script>")
    }).join('')
  } else {
    return ''
  }
};

It failed 'cause for some reason initial is now empty. So var needed = [initial[0]].concat(async, initial.slice(1)); ended up as [undefined]. I fixed this as shown above. And only after that my build launched fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue-ssr-webpack-plugin - npm
webpack plugin for generating a bundle manifest to use with Vue 2.x bundleRenderer. Latest version: 3.0.0, last published: 6 years ago.
Read more >
To v5 from v4 - webpack
This guide aims to help you migrating to webpack 5 when using webpack directly. ... Webpack 5 requires at least Node.js 10.13.0 (LTS),...
Read more >
Webpack 5 Crash Course | Frontend Development Setup
In this video, we will create a Webpack dev environment from scratch including... Webpack setupWebpack Dev ServerHTML Webpack PluginSass ...
Read more >
webpack Tutorial: How to Set Up webpack 5 From Scratch
Content. What is webpack; Installation; Basic configuration. Entry; Output. Plugins. HTML template; Clean. Modules and Loaders.
Read more >
A mostly complete guide to webpack 5 (2020)
Order of webpack loaders matters! Working with SASS. Working with modern JavaScript. How to set up React, webpack 5, and Babel from scratch....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found