通过BitBucket使用Angular Library存储库作为依赖关系

发布于 2025-01-24 20:04:33 字数 742 浏览 1 评论 0原文

我在使用角库存储库时遇到了问题。希望在这里找到一个答案:)

我有一个角度存储库,该存储库容纳一个角库。 该库存储在Bitbucket存储库中 - 包括“ DIST”文件夹中的编译文件。

现在,我想通过一个应用程序消耗此库。

当前,当我将库存储库作为对我的应用程序的依赖性时(通过NPM安装git+ssh:// [email  procearted] /name/my-library.git)整个存储库都添加到了应用程序的node_modules文件夹中。

但是,使用此配置,该库无法通过应用程序找到。由于事实,编译的库文件位于存储库的“ DIST”文件夹中。

现在,如何告诉应用程序在库文件夹的“ DIST”文件夹中查找所需的库文件?

当前的解决方法是将以下内容放置在我的应用程序的tsconfig.json中

"compilerOptions": {
    "paths": {
      "my-library": [ "node_modules/my-library/dist" ]
    }
}

,但我几乎可以肯定,必须有更多的通用。库存储库中必须放置的东西。

先感谢您。

I got a problem regarding the use of an Angular library repository. Hopefully find an answer here :)

I have an Angular repository which holds one single Angular library.
This library is stored in a bitbucket repository - including the compiled files in the "dist" folder.

Now I want to consume this library by an application.

Currently, when I install the library repository as an dependency to my application (via npm install git+ssh://[email protected]/NAME/my-library.git) the whole repository is added to the node_modules folder of the application.

But with this configuration the library can not be found by the application. Because of the fact, that the compiled library files are located in the "dist" folder of the repository.

Now how can I tell the application to look in the "dist" folder of the library folder, to find the needed library files?

A current workaround is to place the following in the tsconfig.json of my application

"compilerOptions": {
    "paths": {
      "my-library": [ "node_modules/my-library/dist" ]
    }
}

But I'm almost sure, that there must be somethign more generic. Something which must be placed in the package.json/angular.json of the library repository to point consuming applications to the dist-folder?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2025-01-31 20:04:33

我很遗憾地告诉您,与在配置文件中添加一行相比,要做的小要做的要多。

除了您的存储库my-library外,您需要仅保存转码的DIST文件的Bitbucket中的第二个存储库。让我们命名my-library-Dist注意:如果您在my-library存储库中有多个项目,则每个项目都需要一个Dist存储库。

要将DIST文件推在DIST存储库中,您可以在本地构建它们并将其推入之后,或者一种更自动化的方法是使用bitbucket-pipeline.yml file。但这也是最困难的方法。

为了节省很多跟踪和错误,我将在此处复制自己的文件。将此文件放在您的my-library存储库的根文件夹中。

# /bitbucket-pipeline.yml

image: node:16

pipelines:
  branches:
    main:
      - parallel:
        - step:
            name: Security Scan
            script:
              # Run a security scan for sensitive data.
              # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
              - pipe: atlassian/git-secrets-scan:0.5.1
      - step:
          name: Build and bump version
          deployment: Production
          script:

            # NOTE: for this to work at least one active user has to add the
            # public repository SSH key ( -> repository settings -> ssh key)
            # to his own SSH keys ( -> avatar -> personal settings
            # -> ssh keys -> add key ). now the repository has the same
            # access rights as the user holing the public key.

            # Build and bump version

            - git remote set-url origin ${BITBUCKET_GIT_SSH_ORIGIN}
            # create directory for the distribution repo
            - mkdir ${BITBUCKET_CLONE_DIR}/dist-repo
            # clone the distribution repo
            - git clone [email protected]:my-organisation/my-library-dist.git ${BITBUCKET_CLONE_DIR}/dist-repo
            # install angular cli to build the app
            - npm install --location=global @angular/cli
            - npm install
            # in order to have the correct (next) minor version in the
            # components project, we need to change the version number in that
            # project and NOT in the root library. so lets jump into it
            - cd ${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library
            # change the version only of this project
            - npm version minor --no-git-tag-version
            - echo "components version is now at `node -p "require('./package.json').version"`"
            # go back into the library root
            - cd ${BITBUCKET_CLONE_DIR}
            # build the library
            - ng build --configuration=production
            # empty the dist folder to git-remove files that aren't
            # included in the build anymore.
            - rm ${BITBUCKET_CLONE_DIR}/dist-repo/* -R
            # copy the resuling build into the dist-repo folder where the
            # dist-repo was checkout in an earlier step.
            # this way we can easily update only the build result in the
            # dist-repo
            - cp ${BITBUCKET_CLONE_DIR}/dist/my-project-within-my-library/* ${BITBUCKET_CLONE_DIR}/dist-repo/ -r
            - cd ${BITBUCKET_CLONE_DIR}/dist-repo
            # we need to specify a user to git to allow uploads
            - git config --global user.email "[email protected]"
            - git config --global user.name "bitbucket-pipelines"
            - echo "dist version is now at `node -p "require('./package.json').version"`"
            # commit all build files to dist-repo with the new package version
            # number, including a new TAG with the same version number.
            - git add --all
            - git commit -a -m "Bump build to `node -p "require('./package.json').version"`"
            - git tag `node -p "require('./package.json').version"`
            - git push && git push --tags
            # go back in the root directory
            - cd ${BITBUCKET_CLONE_DIR}
            # add only the package.json and package-lock.json to git and
            # commit it to the library repo.
            # skip ci to prevent infinite loop
            - git add ${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package*.json
            - git commit -m "Bump build to `node -p "require('${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package.json').version"` [skip ci]"
            - git tag `node -p "require('${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package.json').version"`
            - git push && git push --tags

注意:

  • 您需要在给定的bitbucket-pipeline.yml文件中调整一些项目名称的路径。
  • 您需要在项目my-library/projects/my-projection-my-library/package.json文件中拥有一个版本号。 (在任何管道运行中进行更新)
  • 此管道获取提交的triggert,并合并到main分支(第7行)
  • 以使至少一个活动用户必须添加<<的公共存储库SSH键代码> my -library 存储库( - &gt; repository设置 - &gt; ssh键)to他自己的ssh键( - &gt; avatar - &gt;个人设置 - &gt; ssh键 - &gt; add key)。现在,存储库具有与用户相同的访问权限。
  • 持有存储库SSH密钥的用户需要对所有相关存储库具有读/写入访问权限。

构建完成后,您可以在my-library-Dist存储库中找到一个新的次要版本标签。使用以下NPM命令安装/更新:
npm i bitbucket:my-Company/my-library-Dist#xyz

这是截至2022年6月的Bitbucket支持的方式,并用Angular V14进行了测试

I am pretty sorry to tell you that there is a little more to do than adding a line to a configuration file.

Aside your repository my-library you need a second repository in bitbucket holding only the transcoded dist files. Lets name it my-library-dist. Note: If you have multipe projects in your my-library repository you like to distribute, you need one dist repository for each project.

To push the dist files in your dist repo, you can build them locally and push them afterwards, or a more automated way is to use a bitbucket-pipeline.yml file. But this is also the hardest way.

To save you a lot of trail and error, I will copy my own file here. Place this file in root folder of your my-library repository.

# /bitbucket-pipeline.yml

image: node:16

pipelines:
  branches:
    main:
      - parallel:
        - step:
            name: Security Scan
            script:
              # Run a security scan for sensitive data.
              # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
              - pipe: atlassian/git-secrets-scan:0.5.1
      - step:
          name: Build and bump version
          deployment: Production
          script:

            # NOTE: for this to work at least one active user has to add the
            # public repository SSH key ( -> repository settings -> ssh key)
            # to his own SSH keys ( -> avatar -> personal settings
            # -> ssh keys -> add key ). now the repository has the same
            # access rights as the user holing the public key.

            # Build and bump version

            - git remote set-url origin ${BITBUCKET_GIT_SSH_ORIGIN}
            # create directory for the distribution repo
            - mkdir ${BITBUCKET_CLONE_DIR}/dist-repo
            # clone the distribution repo
            - git clone [email protected]:my-organisation/my-library-dist.git ${BITBUCKET_CLONE_DIR}/dist-repo
            # install angular cli to build the app
            - npm install --location=global @angular/cli
            - npm install
            # in order to have the correct (next) minor version in the
            # components project, we need to change the version number in that
            # project and NOT in the root library. so lets jump into it
            - cd ${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library
            # change the version only of this project
            - npm version minor --no-git-tag-version
            - echo "components version is now at `node -p "require('./package.json').version"`"
            # go back into the library root
            - cd ${BITBUCKET_CLONE_DIR}
            # build the library
            - ng build --configuration=production
            # empty the dist folder to git-remove files that aren't
            # included in the build anymore.
            - rm ${BITBUCKET_CLONE_DIR}/dist-repo/* -R
            # copy the resuling build into the dist-repo folder where the
            # dist-repo was checkout in an earlier step.
            # this way we can easily update only the build result in the
            # dist-repo
            - cp ${BITBUCKET_CLONE_DIR}/dist/my-project-within-my-library/* ${BITBUCKET_CLONE_DIR}/dist-repo/ -r
            - cd ${BITBUCKET_CLONE_DIR}/dist-repo
            # we need to specify a user to git to allow uploads
            - git config --global user.email "[email protected]"
            - git config --global user.name "bitbucket-pipelines"
            - echo "dist version is now at `node -p "require('./package.json').version"`"
            # commit all build files to dist-repo with the new package version
            # number, including a new TAG with the same version number.
            - git add --all
            - git commit -a -m "Bump build to `node -p "require('./package.json').version"`"
            - git tag `node -p "require('./package.json').version"`
            - git push && git push --tags
            # go back in the root directory
            - cd ${BITBUCKET_CLONE_DIR}
            # add only the package.json and package-lock.json to git and
            # commit it to the library repo.
            # skip ci to prevent infinite loop
            - git add ${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package*.json
            - git commit -m "Bump build to `node -p "require('${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package.json').version"` [skip ci]"
            - git tag `node -p "require('${BITBUCKET_CLONE_DIR}/projects/my-project-within-my-library/package.json').version"`
            - git push && git push --tags

Note:

  • you need to adjust some paths for project names in the given bitbucket-pipeline.yml file.
  • you need to have a version number in your project my-library/projects/my-project-within-my-library/package.json file. (gets updated for any pipeline run)
  • this pipeline gets triggert for commits and merges to the main branch (line 7)
  • for this to work at least one active user has to add the public repository SSH key of my-library repository ( -> repository settings -> ssh key) to his own SSH keys ( -> avatar -> personal settings -> ssh keys -> add key ). now the repository has the same access rights as the user holing the public key.
  • the user holding the repository SSH key needs to have read/write access permissions to all relevant repositories.

Once the build is done, you can find a new minor version tag in your my-library-dist repository. Use the following NPM command to install/update:
npm i bitbucket:my-company/my-library-dist#X.Y.Z

This is the suggested way by the bitbucket support by june 2022, tested with angular v14

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文