我如何使用 Webpack 为 Rest API Express NodeJS 服务器创建包

How can I use Webpack to create bundle for a Rest API Express NodeJS Server

希望你一切都好:)

我正在使用 Nodejs 在 REST API 服务器上工作,在完成 alpha 版本后,我想使用构建工具为其创建一个包,尽管我在某些时候成功地捆绑了应用程序,但仍然可以不让 Express Rest API 脚本被捆绑。由于我对 Webpack 没有真正的经验,我很确定我做错了什么,必须有办法做到这一点。您还可以在下面看到我的 webpack.config.js、.babelrc 和 package.json:

Webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var module_dir = `${__dirname}/node_modules`;
const path = require('path');

module.exports = {
  entry: {
    app: [
      'babel-polyfill',
      './index.js',
    ],
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'app.bundle.js',
  },
  module: {
    rules: [{
        test: /\.js?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
           presets: ['env', 'stage-0']
        }
    }]
  },
  resolveLoader: {
    modules: [
          __dirname + '/node_modules'
        ]
  }
}

.Babelrc

{
    "presets": ["@babel/env"]
}

package.json

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "nodemon index.js",
    "build": "webpack --mode production --progress"
  },
  "keywords": [
    "log",
    "npm",
    "node",
    "rest",
    "api",
    "debug",
    "bug"
  ],
  "author": "Mehdi Roshan Fekr",
  "license": "ISC",
  "dependencies": {
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "express": "^4.16.4",
    "joi": "^14.3.1",
    "nodemon": "^1.18.10"
  },
  "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.4",
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2"
  }
}

我也读过这篇关于在 webpack 中使用 express 的文章,但我无法正确实现它,我认为这是一个原因,它是针对 ReactJS 应用程序的:

----更新-----

错误

ERROR in ./index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-env' from 'C:\Projects\# App Projects\Qcentic-Log'
- Did you mean "@babel/env"?
    at Function.module.exports [as sync] (C:\Projects\# App Projects\Qcentic-Log\node_modules\resolve\lib\sync.js:58:15)
    at resolveStandardizedName (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:101:31)
    at resolvePreset (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:58:10)
    at loadPreset (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:77:20)
    at createDescriptor (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\config-descriptors.js:154:9)
    at items.map (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\config-descriptors.js:109:50)

index.js

const express = require('express');
const app = express();
const CustomModule = require('./CustomModule');
app.use(express.json());
//My Endpoints...
app.listen(80, () => console.log('Listening on port 80'));

由于您将 babel 配置直接传递给加载程序,因此不需要 .babelrc 文件。此外,您使用的是 babel v7,所以下面是更新的配置(您的配置和 package.json 包含 babel v6 和 v7 的混合包,它们不能一起工作):

module.exports = {
    target: "node",
    entry: './index.js',
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js',
    },
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
                presets: [
                    [
                        "@babel/preset-env",
                        {
                            targets: {
                                node: "8.10"
                            }
                        }
                    ]
                ]
            }
        }]
    },
    resolveLoader: {
        modules: [
            __dirname + '/node_modules'
        ]
    }
}
  1. 请注意,我删除了 @babel/polyfill,服务器环境不需要它(我确定,因为我也将我的服务器代码与 webpack 捆绑在一起,并且从不需要它)。

  2. 确保将 node 的版本设置为您的目标版本。

  3. 另请注意,query 是一种将选项传递给 webpack 加载程序的非常古老的方法,因此我使用 options 将其更新为新语法。最好传递 babel 插件的全名,例如:@babel/preset-env 而不是 env。解析插件名称的旧方法会生成一个基于 envbabel-preset-env,但自从 babel v7 以来,他们将项目重组为 "scoped packages",因此 @babel/ 前缀,所以最好指定全名。