catamphetamine / universal-webpack

Isomorphic Webpack: both on client and server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove webpack dependency on production

papigers opened this issue · comments

This isn't really an issue, since it really solvable without any changes in the code, but I'll explain. I want to remove the dependency of webpack in the production build, since i deploy the built bundle already.

My problem is that universal-webpack forces me to put webpack in my (non dev-)dependencies:

import { server_configuration } from 'universal-webpack'
import settings from './universal-webpack-settings'
import configuration from './webpack.config' // THIS requires webpack and another things which should belong only to dev: postcss plugins etc...

export default server_configuration(configuration, settings)

I've looked into the server.js file and it seems it only requires the context and the output paths, so it is possible to pass just those paths and be done with it, requiring the entire webpack config is a bit unnecessary.

So like I said, it really isn't an issue since we can just change it to:

import { server_configuration } from 'universal-webpack'
import settings from './universal-webpack-settings'

export default server_configuration({
  context: /* context path */,
  output: {
    path: /* server build output */
  }
}, settings)

But maybe document it?

Ok, added this info to the docs.

Hi guys, I'm trying to understand this, because I also want to remove webpack from my production dependencies.

What I don't understand, is it seems like server_configuration from universal_webpack still requires webpack. So even by stubbing the configuration passed into server_configuration, you haven't eliminated the production dependency on webpack. Is that right? Or can you help me understand that? Thank you in advance.

it seems like server_configuration from universal_webpack still requires webpack.

What makes you think so

I thought so from looking at the source file:
universal-webpack/source/server configuration.js

At line 3 it imports webpack:
import webpack from 'webpack'

Am I looking in the wrong place?

Yes, it uses webpack for its plugins: it removes webpack.HotModuleReplacementPlugin and webpack.optimize.CommonsChunkPlugin and also adds webpack.optimize.LimitChunkCountPlugin.

The original poster said:

since i deploy the built bundle already.

Therefore, currently webpack is used during build but it's not used when running the built bundle

Ah I understand! Thank you.

Ok I understand what was throwing me off now. server_configuration() does require a relatively full webpack configuration. It's server() that only requires context and output: {path: }. This is what you updated in the documentation - I think in @papigers initial post he shows an example using server_configuration() but really he meant server().

Hi sorry to keep harping on this, I appreciate any help.

I find that I am still hitting a dependency on webpack. Running this line in production is trying to find webpack:

import { server } from 'universal-webpack';

If I go into index.common.js in node_modules/universal-webpack and comment out these lines:

server_configuration : require('./build/server configuration').default,
client_configuration : require('./build/client configuration').default,
serverConfiguration : require('./build/server configuration').default,
clientConfiguration : require('./build/client configuration').default,

Then I can get it to work. So it seems that just by importing universal-webpack I am hitting the dependency on webpack. So I can also get around this by requiring server like this:

import server from 'universal-webpack/build/server';

instead of like this:

import { server } from 'universal-webpack';

Does this make sense?

Yes, it does: seems that indeed webpack is required for running the .common.js bundle.
Presumably this is fixed in Webpack 2 because it should "tree shake" the unused dependencies.
But for Webpack 1, as a workaround, import server from 'universal-webpack/build/server' is gonna work without any difference.

Ok thank you, that makes sense.

Oh, disregard my comment above: here Node.js is used, not Webpack. And Node.js doesn't "tree shake". So, yes, the workaround you proposed seems to be the one:

import server from 'universal-webpack/build/server'

Great, seems like it works. I wonder what is the right way to represent universal-webpack's dependency on webpack. I guess it might make sense to break up the build_configuration aspect from the runtime aspect. Then the build_configuration part would depend on webpack and the runtime part would not. Not sure that is worth doing just interesting to think about.

And how would you require it then?
You only have one index.js and one way or another it's gonna be a separate require() from lib.

I can make a simple shortcut though.
Like universal-webpack/server.
Yeah, I guess I'll make it now.

Yeah I don't know enough about structuring npm packages...

I added two new exports:

import { client, server } from 'universal-webpack/config'
import startServer from 'universal-webpack/server'

You can test these in the newly released version.

Very cool, I won't be able to test for a while. I'm still on Webpack 1 so not ready for the latest version of universal-webpack. Hoping to have some time to make the upgrade soon.