首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何仅使用nx构建和打包相关的依赖项?

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

Stack Overflow用户
提问于 2022-08-16 11:32:00
回答 1查看 1.2K关注 0票数 3

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

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

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-14 20:18:51

我找到了一个不理想的解决方案,但至少起作用了。generatePackageJson所做的是在build文件夹中创建一个package.json文件,该文件可用于再次运行build 来生成只包含特定目标的依赖项的node_modules文件夹。因此,在我的例子中,我所做的就是将"generatePackageJson": true添加到后端包中的build目标中:

代码语言:javascript
运行
复制
{
  "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"
        ]
      }
      // ...
    }
  }
}

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

注意,这个脚本是从根文件夹运行的,所以我们的后端dist文件夹相对于这个脚本的运行位置位于dist/apps/backend。(我将脚本保存在script文件夹中。您还不应该运行这个本地,因为它会删除项目中的内容。只在CI/CD环境中运行此操作。

代码语言:javascript
运行
复制
#!/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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73373307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档