Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the most reasonable way apply webpack to a full-stack node app?

i've been studying up on webpack for a couple of weeks now, and i've seen many examples of front end setups, and probably just this one setup for a backend.

i'm trying to setup a react app with a node back-end (e.g. express, koa, hapi, etc) where i would need at least one transpilation step for the back-end (e.g. babel, coffeescript, etc), and i think it would be nice to use webpack there for consistency vs adding another build mechanism to the mix (e.g. gulp, grunt, etc).

it would also be great if i could make changes to the backend and have the server restart automatically (watch style).

i'm wondering if the best way to do that is to basically have two distinct project setups with their own package.json and webpack.config files. maybe nest the back-end one under a server folder in the top level project folder, and use one or more script directives in the top level package.json file to control the two.

i guess i might have to proxy one server to the other to avoid CORS issues as well.

looking for any guidance from those more webpack battle tested than i for a decent setup.

regards, tony.

like image 945
tony_k Avatar asked Jun 06 '15 19:06

tony_k


People also ask

How do I speed up time on webpack?

Use the uglifyjs-webpack-plugin v1 It is one of the main methods used to reduce the build time. But this modification process itself can take a considerable amount of time as the project size increases. So if your project is scaling, you can use uglifyjs-webpack-plugin v1 to optimize the modification time.

How much faster is webpack 5?

Production build time is reduced by 29.16%, we can see improvement in Build time as it is reduced from 11 mins to 8.2 mins (29.16%) on Jenkins. 4. Development build time for mobile client when using webpack 4 is 220.36s and using webpack 5 169.83s on the local machine which is another 22.93% improvement.


1 Answers

Easiest way is to split this into two tasks: a build step that outputs to a folder (e.g. 'server'), then watch the output folder for changes and restart server task.

1. Build task

This can be in the same webpack.config as client building code - you can export an array and webpack will watch all of them. Example webpack.config.js (top half is for server)

module.exports = [
{
  name: 'server code, output to ./server',
  entry: './index.js',
  output: {
    filename: './server/index.js'
  },
  target: 'node'
},
{
  name: 'client side, output to ./public',
  entry: './app.js',
  output: {
    filename: './public/app.js'
  }
}
];

2.Watch Step

For the watch step, nodemon monitor changes and restart. Otherwise you could add a watch task to your server.js manually using something like fs.watch or node-watch.

like image 198
psimyn Avatar answered Nov 12 '22 07:11

psimyn