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

后端 未结 1 637
难免孤独
难免孤独 2021-02-01 08:58

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 tryi

相关标签:
1条回答
  • 2021-02-01 09:47

    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.

    0 讨论(0)
提交回复
热议问题