首页 > 解决方案 > 如何从 Angular 库中公开 bin javascript 文件

问题描述

我正在创建一个库,它向用户公开一些 bin 脚本,然后他可以在他的构建过程中使用这些脚本。但是,我对将此类脚本暴露给dist包感到不安。

我的 package.json 看起来像这样:

{
  "name": "my-app",
  "bin": {
    "foobar": "./bin/custom-build.js"
  }
}

结构如下所示:

项目结构

当我运行构建时,custom-build.js在目录中dist找不到。我的问题是如何公开这些文件?

标签: angularnpmangular-library

解决方案


您应该将它们添加到angular.json. 它们将合并为scripts.jsin dist

{
  "projects": {
    "your-project-name": {
      "architect": {
        "build": {
          "options": {
            "scripts": [
              "path/to/your/lib1.js",
              "path/to/your/lib2.js"
            ]
          }
        }
      }
    }
  }
}

编辑:

在 Angular CLI 中没有复制文件的选项。您可以使用外部工具存档,例如 bash 脚本、gulp https://gulpjs.com/

然后将其合并到您的package.js

{
  "scripts": {
    "build": "ng build && gulp bundle"
  }
}

推荐阅读