using webpack on server side of nodejs

前端 未结 3 1884
情深已故
情深已故 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 13:43

    A real example with webpack 2.x

    I want to highlight the difference from client side config:

    1. target: 'node'

    2. externals: [nodeExternals()]

    for node.js, it doesn't make much sense to bundle node_modules/

    3. output.libraryTarget: 'commonjs2'

    without this, you cannot require('your-library')

    webpack.config.js

    import nodeExternals from 'webpack-node-externals'
    
    const config = {
      target: 'node',
      externals: [nodeExternals()],
      entry: {
        'src/index': './src/index.js',
        'test/index': './test/index.js'
      },
      output: {
        path: __dirname,
        filename: '[name].bundle.js',
        libraryTarget: 'commonjs2'
      },
      module: {
        rules: [{
          test: /\.js$/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                ['env', {
                  'targets': {
                    'node': 'current'
                  }
                }]
              ]
            }
          }
        }]
      }
    }
    
    export default [config]
    

提交回复
热议问题