Yarn在工作区中安装单个包的生产依赖项

时间:2017-10-25 16:34:36

标签: node.js yarnpkg

我正在尝试仅为我的工作区中的单个包安装生产依赖项。这可能吗?

我已经尝试过了:

yarn workspace my-package-in-workspace install -- --prod

但它正在安装我所有软件包的所有生产依赖项。

2 个答案:

答案 0 :(得分:0)

您可以尝试

纱线工作区@ my-monorepo / my-package-in-workspace install---prod

答案 1 :(得分:0)

据我所知

yarn 1不支持它。

如果您尝试在dockerfile中安装特定的软件包,则有一种解决方法:

  1. 复制yarn.lock文件和根package.json

  2. 仅复制所需的软件包package.json:软件包以及软件包所依赖的其他软件包(在monorepo中本地)。

  3. 在dockerfile中,手动删除您复制的所有package.json的所有devDependnecies。

  4. 在根package.json上运行yarn install

注意:

完整的dockefile示例:

FROM node:12

WORKDIR /usr/project

COPY yarn.lock package.json remove-all-dev-deps-from-all-package-jsons.js change-version.js ./

ARG package_path=packages/dancer-placing-manager

COPY ${package_path}/package.json ./${package_path}/package.json

RUN node remove-all-dev-deps-from-all-package-jsons.js && rm remove-all-dev-deps-from-all-package-jsons.js

RUN yarn install --frozen-lockfile --production

COPY ${package_path}/dist/src ./${package_path}/dist/src
COPY ${package_path}/src ./${package_path}/src

CMD node --unhandled-rejections=strict ./packages/dancer-placing-manager/dist/src/index.js

从所有程序包-jsons.js中删除所有开发人员-deps:

const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')

async function deleteDevDeps(packageJsonPath) {
  const packageJson = require(packageJsonPath)
  delete packageJson.devDependencies
  await new Promise((res, rej) =>
    fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8', error => (error ? rej(error) : res())),
  )
}

function getSubPackagesPaths(repoPath) {
  const result = execSync(`yarn workspaces --json info`).toString()
  const workspacesInfo = JSON.parse(JSON.parse(result).data)
  return Object.values(workspacesInfo)
    .map(workspaceInfo => workspaceInfo.location)
    .map(packagePath => path.join(repoPath, packagePath, 'package.json'))
}

async function main() {
  const repoPath = __dirname
  const packageJsonPath = path.join(repoPath, 'package.json')
  await deleteDevDeps(packageJsonPath)
  await Promise.all(getSubPackagesPaths(repoPath).map(packageJsonPath => deleteDevDeps(packageJsonPath)))
}

if (require.main === module) {
  main()
}