Angular 6 CLI -> how to make ng build build project + libraries

前端 未结 9 489
感情败类
感情败类 2020-12-31 01:58

So the question is pretty basic but I can\'t find it.

I created a new app through ng new my-project, followed by a ng g library my-library.

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 02:14

    As much as I know, there is no build-in way to do this in the current version (Angular 8).
    It might be possible to use the new builders but I don't know much about them yet.
    So what I did instead was to create a script, which reads in the angular.json file and determines all application projects and all configurations.
    Then it executes ng build for every project and configuration.
    Also, it will collect all the failed builds and log them to the console at the end.
    This script looks like this:

    import { ProjectType, WorkspaceSchema } from "@schematics/angular/utility/workspace-models";
    import { execSync } from "child_process";
    import { readFileSync } from "fs";
    
    interface ExecParams {
      project: string;
      config: string;
    }
    
    interface ExecError extends ExecParams {
      message: string;
    }
    
    function buildAll() {
      const json: WorkspaceSchema = JSON.parse(readFileSync("./angular.json").toString());
      const errors: ExecError[] = Object.keys(json.projects)
        // Filter application-projects
        .filter(name => json.projects[name].projectType === ProjectType.Application)
        // Determine parameters needed for the build command
        .reduce((arr, project) => {
          const proj = json.projects[project];
          let build = proj.architect && proj.architect.build;
          if (build) {
            arr = arr.concat(...Object.keys(build.configurations || {})
              .map(config => ({ project, config }))
            );
          }
          return arr;
        }, [])
        // Execute `ng build` and collect errors.
        .reduce((err, exec) => {
          try {
            console.log(`Building ${exec.project} (${exec.config}):`);
            execSync(`ng build --prod --project ${exec.project} --configuration ${exec.config}`, {stdio: "inherit"});
          }
          catch (error) {
            err.push({
              project: exec.project,
              config: exec.config,
              message: error.message
            });
          }
          console.log("\n");
          return err;
        }, []);
    
      // Conditionally log errors
      if (errors.length === 0)
        console.log("Completed");
      else {
        console.log("\n");
        errors.forEach(error => {
          console.error(`Building ${error.project} (${error.config}) failed:\n\t${error.message}`);
        });
      }
    }
    
    buildAll();
    

    You can compile it using tsc and then run it with NodeJs.

提交回复
热议问题