简体   繁体   English

运行 ng build 时执行 JavaScript 文件

[英]Executing a JavaScript file when running ng build

I would like to run a JavaScript file in my Angular application every time I run ng build .每次运行ng build时,我都想在我的 Angular 应用程序中运行一个 JavaScript 文件。 To be more precise, I want this file to be executed before the build process so that the changes that it makes are present in the build.更准确地说,我希望在构建过程之前执行此文件,以便它所做的更改存在于构建中。

Its a simple script that reads the app version and its dependencies and write them to an object.它是一个简单的脚本,可以读取应用程序版本及其依赖项并将它们写入对象。

The file is called pre-build.js and I have tried configuring the build command in package.json as follows, however I can see that the script was not executed:该文件名为pre-build.js ,我已尝试在package.json中配置build命令,如下所示,但是我可以看到该脚本未执行:

{
   ...
   ...,
   "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "node pre-build.js && ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
   },
   ...,
   ...,
}

The path of the script is ./pre-build.js .脚本的路径是./pre-build.js I assume that I have to change more configurations in order to achieve this but I am not able to find out where.我假设我必须更改更多配置才能实现这一点,但我无法找出在哪里。 Any leads will be appreciated.任何线索将不胜感激。

Edit:编辑:

This is the content of pre-build.js :这是pre-build.js的内容:

const path = require('path');
const fs = require('fs');
const appVersion = require('./package.json').version;
const appDeps = JSON.stringify(require('./package.json').dependencies);

const versionFilePath = path.join(__dirname + '/src/environments/version.ts');

const src = `export const version = '${appVersion}';\nexport const buildTime = '${new Date()}';\nexport const deps = ${appDeps};`;

// ensure version module pulls value from package.json
fs.writeFile(versionFilePath, src, (err) => {
  if (err) {
    console.log(err);
  }
});

When I run node pre-build.js in the terminal, the code works fine and updates the version.ts file.当我在终端中运行node pre-build.js时,代码可以正常工作并更新version.ts文件。 But i want to somehow automatically execute this command every time i run ng build .但是我想在每次运行ng build时以某种方式自动执行此命令。 Which so far i was not able to do so.到目前为止,我无法这样做。

Edit编辑

The correct answer to this problem is that you shouldn't run ng build but should run npm run build since you want to execute the script.这个问题的正确答案是你不应该运行ng build而应该运行npm run build因为你想执行脚本。 When you do ng build this would only trigger the build for angular and wouldn't update your version file indeed.当您执行ng build时,这只会触发 Angular 的构建,并且确实不会更新您的版本文件。

Below is an example of your exact same code when doing npm run build , so make sure to update how you build.下面是执行npm run build时完全相同的代码示例,因此请确保更新您的构建方式。

在此处输入图像描述

Give it a try and let me know if this is still an issue.试一试,让我知道这是否仍然是一个问题。


Old answer旧答案

You can create a ".sh" script to other execute everything you need.你可以创建一个“.sh”脚本来执行你需要的一切。 This might be helpful later on to add more pre or post build commands这可能有助于以后添加更多的构建前或构建后命令

Here is an example这是一个例子

package.json包.json

"scripts": {
  "build:angular": "ng build",
  "build": ./build.sh
}

build.sh构建.sh

#!/bin/bash
node ./pre-build.js

npm run build:angular

Make sure that pre-build is executable so is the build.sh (chmod https://askubuntu.com/questions/229589/how-to-make-a-file-ega-sh-script-executable-so-it-can-be-run-from-a-termi )确保预构建是可执行的,所以 build.sh (chmod https://askubuntu.com/questions/229589/how-to-make-a-file-ega-sh-script-executable-so-it-可以从终端运行

试试这样:

node ./pre-build.js && ng build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM