using webpack on server side of nodejs

前端 未结 3 1841
情深已故
情深已故 2021-01-30 13:07

I\'ve been trying to use webpack with a nodejs application, and the client side is going fine - a reasonably good documentation on their website + links from google search.

3条回答
  •  时光说笑
    2021-01-30 14:03

    This might be useful: http://jlongster.com/Backend-Apps-with-Webpack--Part-I

    Key point is to make external all third party module (in node_modules directory) in webpack config file

    Final config file

    var webpack = require('webpack');
    var path = require('path');
    var fs = require('fs');
    
    var nodeModules = {};
    fs.readdirSync('node_modules')
      .filter(function(x) {
        return ['.bin'].indexOf(x) === -1;
      })
      .forEach(function(mod) {
        nodeModules[mod] = 'commonjs ' + mod;
      });
    
    module.exports = {
      entry: './src/main.js',
      target: 'node',
      output: {
        path: path.join(__dirname, 'build'),
        filename: 'backend.js'
      },
      externals: nodeModules,
      plugins: [
        new webpack.IgnorePlugin(/\.(css|less)$/),
        new webpack.BannerPlugin('require("source-map-support").install();',
                                 { raw: true, entryOnly: false })
      ],
      devtool: 'sourcemap'
    }
    

提交回复
热议问题