如何使用nx仅构建和打包相关依赖项?

Ada*_*old 8 dependency-management npm typescript nrwl nrwl-nx

我有一个项目使用nx其中有很多包。我的问题是我只有一个package.json文件,因此如果我只想构建一个包,我仍然必须使用npm install.

这是一个问题,因为在 CI/CD 步骤中,我必须构建整个项目,这花费了太长时间,而且它还生成了一个node_modules变得巨大的文件夹(3GB),这也使我的包大小变得非常大。

如何构建单个包,使node_modules文件夹仅包含我的包所需的依赖项,而不是包含所有包的所有依赖项?

如果这是不可能的,我如何编译一个main.js捆绑所有这些依赖项的可执行文件?

编辑:我尝试拆分package.json所有包的文件,但是每当我构建单个包时,我仍然会将所有依赖项加载到node_modules根目录中的文件夹中。是否可以为每个单独的包建立一个node_modules文件夹?

Ada*_*old 7

我找到了一个并不理想的解决方案,但至少有效。它的generatePackageJson作用是package.json在构建文件夹中创建一个文件,可用于再次运行构建以生成node_modules仅包含特定目标的依赖项的文件夹。因此,就我而言,我所做的就是添加"generatePackageJson": truebuild我的后端包中的目标:

{
  "sourceRoot": "apps/backend/src",
  "projectType": "application",
  "targets": {
    "build": {
      // ...
      "options": {
        "outputPath": "dist/apps/backend",
        "main": "apps/backend/src/main.ts",
        "tsConfig": "apps/backend/tsconfig.app.json",
        "generatePackageJson": true, // 
        "assets": [
          "apps/backend/src/assets"
        ]
      }
      // ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行,nx build backend我将得到一些dist/apps/backend可以用作独立项目的东西。不幸的是npm,检查父文件夹中的package.json文件并弄乱了所有内容,因此我最终编写了一个构建脚本来清理项目并实现轻松部署。(这适用于 Heroku,但很容易适应其他 PaaS / IaaS 解决方案。我将把它逐字粘贴到这里:

请注意,此脚本从根文件夹运行,因此我们的后端 dist 文件夹相对dist/apps/backend于此脚本运行的位置。(我将脚本保留在script文件夹中。您也不应该在本地运行它,因为它会删除项目中的内容。仅在 CI/CD 环境中运行它。

#!/bin/bash

print_usage() {
    echo " $1"
    echo "Help below "
    echo ""
    echo "Builds the specified project for Heroku."
    echo ""
    echo "Usage: ./heroku-build <nx-project-name>"
    echo ""
    echo "__Note that__ this script is intended to be used from CI/CD, you probably won't need it during development."
    exit 1
}

if [ -z "$1" ]; then
    print_usage "Project name is missing!"
fi

# Heroku build is a little bit different because they have a slug size (deployment size) limit of 500MB.
# If you build the project (not just the package) you'll end up with a `node_modules` folder that's ridiculously big (3GB)
# but it can't be pruned properly. If you think you can prune it without this hacky solution go ahead, but it is unlikely
# that you'll be able to figure it out. **If** you try it please increment the counter below:
# 
# total_hours_wasted_trying_to_prune_node_modules=13
# 
# So how this works is that Heroku will run `npm install --prod` that will delete devDependencies too, so
#  DON'T MOVE nx and @nrwl packages to devDependencies! 
# After the project is built you'll have the horrendous `node_modules` folder, but it's not a big deal as we'll delete it.

# Build the project with nx
nx build $1 --prod

# This step is necessary because npm will look for `package.json` files in the parent folder and it will use the `node_modules`
# folder from the parent folder. We don't want that, we want to have only the necessary packages (backend) in the `node_modules` folder.
mv package.json package.json.bak
mv package-lock.json package-lock.json.bak

# We get rid of all the unnecessary packages.
rm -Rf node_modules

# We install the necessary packages.
# In the `nx build` step nx generates a `package.json` that only contains the dependencies of the backend project
# so this will *only* () download 500MB from npm.
npm install dist/apps/backend

# More reading on this problem:
#
# - https://stackoverflow.com/questions/73373307/how-to-build-and-package-only-the-relevant-dependencies-using-nx?noredirect=1#comment129738870_73373307
# - https://github.com/nrwl/nx/issues/1518
# - https://github.com/nestjs/nest/issues/1706#issuecomment-579248915
Run Code Online (Sandbox Code Playgroud)