using webpack on server side of nodejs

前端 未结 3 1844
情深已故
情深已故 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

    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"
      },
    

提交回复
热议问题