【问题标题】:How to chain npm script call after each ng serve build?如何在每个 ng serve 构建后链接 npm 脚本调用?
【发布时间】:2019-10-10 02:21:28
【问题描述】:

我试图在运行时链接 ng serve 脚本调用,但是当我运行此命令时,第二个链接脚本 build-components.js 仅在第一次调用时运行。
我认为 concurrently 这里的包将允许两个脚本根据文档https://www.npmjs.com/package/concurrently 依次运行

我的目标是在每次运行 ng serve 时运行 build-components.js 脚​​本(即检测源更改)。

Package.json 脚本:

"build:watch": "concurrently \"ng serve --port 4003\" \"node build-components.js\""

测试

 concurrently "ng serve --port 4003" "node build-components.js"

[1] node build-components.js exited with code 0
[0] i 「wds」: Project is running at http://localhost:4003/webpack-dev-server/

问题:

如何在每次 ng serve 构建后运行另一个 npm 脚本?

我还查看了 npm post 挂钩,但在 ng serve 运行后似乎也没有运行脚本。

http://www.marcusoft.net/2015/08/pre-and-post-hooks-for-npm-scripting.html#hooks-pre-and-post

这是build-components.js 脚本供参考。它将一些额外的构建文件复制到一个公共文件夹以供托管:

const fs = require('fs-extra');
const concat = require('concat');

(async function build() {
  const js = [
    './dist/app/runtime-es2015.js',
    './dist/app/main-es2015.js',
    './dist/app/scripts.js',
  ];
  const css = ['./dist/app/styles.css'];

  await fs.ensureDir('components');
  await concat(js, 'components/component.js');
  await concat(css, 'components/component.css');
})();

【问题讨论】:

  • 你想做什么更大的图?
  • 另一个 Angular 应用正在运行时从这个应用请求构建文件。因此,当开发人员保存他们的工作时,我正在尝试自动ng build && node build-elements.js。这有意义吗?
  • 所以我真的希望能够执行以下操作ng build --watch && node build-components.js,但您似乎说上面的 ng build 在 watch 中运行时没有完成?
  • 嗯,您可以调整 angular.json 以创建具有不同输出文件夹的配置(或更改默认输出文件夹)。我没有看到任何用于连接文件的内置内容。您可以使用ngx-build-plus 来做到这一点,但我不确定。 This question 似乎暗示这是可能的。

标签: javascript angular typescript npm-scripts concurrently


【解决方案1】:

这是不可能的。 ng serve 在监视模式下运行时不会完成,因此链接项目以在它之后运行将无效。 serve 命令对此没有挂钩。

【讨论】:

  • 是的,我看到 ngx-build-plus 确实有一个参考应用程序来演示单个包构建。看起来是 ng-build 构建时间的一半
  • 通过使用 npm 包 watch 在每次源文件更改时使用 ngx build plus 运行构建来破解此问题。它比普通的 ng build 快几秒钟,因为我正在输出一个包。但是没有ng serve 这么快,我理解这是因为ng serve 不会将包输出到磁盘而是将包存储在内存中? @佩斯
  • @BrianJ ng serve 是增量构建。第一次构建的速度应该与ng build 大致相同,但后续更改不需要完全重新编译。是的,这是通过不仅保存捆绑包而且还保存已经解析和标记化的内存中的构建上下文来实现的。有什么方法可以让你仍然使用ngx build plus 并传入--watch 标志?
  • Nope 使用--watch 标志运行的行为与默认ng build 相同,它不会退出。所以第二个脚本没有运行。例如ng-build --watch && node build-components.js
【解决方案2】:

只需在脚本中使用“post”

{
    "scripts": [
        "build": "ng build --watch',
        "postbuild": "node build-components.js"
    ]
}

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 2017-06-07
    • 2018-05-29
    • 2020-05-31
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2017-07-07
    • 2022-01-18
    相关资源
    最近更新 更多