using webpack on server side of nodejs

前端 未结 3 1842
情深已故
情深已故 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]
    
    0 讨论(0)
  • 2021-01-30 13:43

    Here is the webpack configuration I have used to in my Nodejs application when I wanted it to read JSX which as you know, Node cannot do.

    const path = require('path');
    
    module.exports = {
      // inform webpack that I am building a bundle for nodejs rather than for the
      // browser
      target: 'node',
    
      // tell webpack the root file of my server application
      entry: './src/index.js',
    
      // tells webpack where to put the output file generated
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'build')
      },
    
      // tells webpack to run babel on every file it runs through
      module: {
        rules: [
          {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            options: {
              presets: [
                'react',
                'stage-0',
                ['env', { targets: { browsers: ['last 2 versions'] } }]
              ]
            }
          }
        ]
      }
    };
    

    After you implement this though, don't forget to head over to your package.json file and include this script:

    {
      "name": "react-ssr",
      "version": "1.0.0",
      "description": "Server side rendering project",
      "main": "index.js",
      "scripts": {
        "dev:build:server": "webpack --config webpack.server.js"
      },
    
    0 讨论(0)
  • 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'
    }
    
    0 讨论(0)
提交回复
热议问题