-->

window is not defined in server side render window

2019-07-27 21:20发布

问题:

I'm attempting to upgrade a Rails/Webpacker/ReactOnRails application to webpack 4. I have included a call to environment.splitChunks. That is placing this code:

window.webpackJsonp=window.webpackJsonp||[]).push([[11],.......

in my bundle. Since this is my server side bundle, window does not exist. Is there a way to completely exclude this bundle from having webpackJsonp added?

I have tried these two configurations:

environment.splitChunks();

and

environment.splitChunks(config =>
  Object.assign({}, config, {
    optimization: {
      splitChunks: {
        chunks(chunk) {
          return chunk.name !== 'server-bundle';
        }
      }
    }
  })
);

Both end up with the same result. If I don't include splitChunks, my code works fine.

I also just tried:

environment.splitChunks(config =>
  Object.assign({}, config, {
    optimization: {
      splitChunks: {
        cacheGroups: {
          server: {
            test: /server-bundle/,
            minChunks: 99999 // Do not ever chunk this file
          }
        }
      }
    }
  })
);

and

environment.splitChunks(config =>
  Object.assign({}, config, {
    optimization: {
      splitChunks: {
        cacheGroups: {
          server: {
            test: /server-bundle/,
            minChunks: 99999 // Do not ever chunk this file
          }
        },
        chunks(chunk) {
          return chunk.name !== 'server-bundle';
        }
      }
    }
  })
);

I thought one of those would put it in its own group that didn't get chunked, but no dice.