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

前端 未结 9 488
感情败类
感情败类 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:27

    use with simple node js like this:

    create js file in ~App root (e.g: build.js).

    then placed the following codes in your file.

    var child_process = require('child_process');
    
    console.log("building all packages...\n");
    
    var files = GetProjectList();
    var count = files.length;
    var counter = 1;
    
    files.forEach(item => 
    {
        try 
        {
            console.log(`${counter++} of ${count} building ${item.fileName}...`);
            child_process.execSync(`ng b ${item.fileName}`);
            console.log(`${item.fileName} built successfully.\n`);
        } 
        catch (er) 
        {
            console.error(` Couldn't build ${item.fileName} .\n`);
        }
    });
    
    
    
    function GetProjectList()
    {
        // enter list of projects here.
        var lst = [];
        lst.push({ fileName : 'project1' });
        lst.push({ fileName : 'project2' });
        lst.push({ fileName : 'project3' });
        //...
        return lst;
    }
    

    Finally, run your file with node js command. like this:

    node build.js             // `build.js` is name of your created file on above
    

    pay attention to the unicode your file. // build.js

    it's working well.

    I hope is useful.

    0 讨论(0)
  • 2020-12-31 02:38

    I find and test this: https://github.com/angular/angular-cli/wiki/stories-create-library

    So instead ng build --prod you should use ng build my-lib --prod

    0 讨论(0)
  • 2020-12-31 02:38

    I created a script that, when placed in the same folder as angular.json, will pull in the file, loop over the projects, and build them in batches asynchronously.

    Here's a quick gist, you can toggle the output path and the number of asynchronous builds. I've excluded e2e for the moment, but you can remove the reference to the filteredProjects function, and it will run for e2e as projects as well. It would also be easy to add this to package.json as an npm run script. So far, it has been working well.

    https://gist.github.com/bmarti44/f6b8d3d7b331cd79305ca8f45eb8997b

    const fs = require('fs'),
      spawn = require('child_process').spawn,
      // Custom output path.
      outputPath = '/nba-angular',
      // Number of projects to build asynchronously.
      batch = 3;
    
    let ngCli;
    
    function buildProject(project) {
      return new Promise((resolve, reject) => {
        let child = spawn('ng', ['build', '--project', project, '--prod', '--extract-licenses', '--build-optimizer', `--output-path=${outputPath}/dist/` + project]);
    
        child.stdout.on('data', (data) => {
          console.log(data.toString());
        });
    
        child.stderr.on('data', (data) => {
          process.stdout.write('.');
        });
    
        child.on('close', (code) => {
          if (code === 0) {
            resolve(code);
          } else {
            reject(code);
          }
        });
      })
    }
    
    function filterProjects(projects) {
      return Object.keys(projects).filter(project => project.indexOf('e2e') === -1);
    }
    
    function batchProjects(projects) {
      let currentBatch = 0,
        i,
        batches = {};
    
      for (i = 0; i < projects.length; i += 1) {
        if ((i) % batch === 0) {
          currentBatch += 1;
        }
        if (typeof (batches['batch' + currentBatch]) === 'undefined') {
          batches['batch' + currentBatch] = [];
        }
    
        batches['batch' + currentBatch].push(projects[i]);
      }
      return batches;
    }
    
    fs.readFile('angular.json', 'utf8', async (err, data) => {
      let batches = {},
        batchesArray = [],
        i;
    
      if (err) {
        throw err;
      }
    
      ngCli = JSON.parse(data);
    
      batches = batchProjects(filterProjects(ngCli.projects));
      batchesArray = Object.keys(batches);
    
      for (i = 0; i < batchesArray.length; i += 1) {
        let promises = [];
    
        batches[batchesArray[i]].forEach((project) => {
          promises.push(buildProject(project));
        });
    
        console.log('Building projects ' + batches[batchesArray[i]].join(','));
    
        await Promise.all(promises).then(statusCode => {
          console.log('Projects ' + batches[batchesArray[i]].join(',') + ' built successfully!');
          if (i + 1 === batchesArray.length) {
            process.exit(0);
          }
        }, (reject) => {
          console.log(reject);
          process.exit(1);
        });
      }
    });
    
    0 讨论(0)
提交回复
热议问题