Skip to content

Commit

Permalink
feat(angular): improve incremental build speed (nrwl#3854)
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr authored Oct 30, 2020
1 parent 4a90fba commit 0892fa2
Show file tree
Hide file tree
Showing 20 changed files with 697 additions and 204 deletions.
35 changes: 35 additions & 0 deletions docs/angular/api-angular/builders/ng-packagr-lite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ng-packagr-lite

Build an Angular library for incremental building

Builder properties can be configured in angular.json when defining the builder, or when invoking it.

## Properties

### project

Type: `string`

The file path for the ng-packagr configuration file, relative to the current workspace.

### tsConfig

Type: `string`

The full path for the TypeScript configuration file, relative to the current workspace.

### updateBuildableProjectDepsInPackageJson

Default: `true`

Type: `boolean`

Update buildable project dependencies in package.json

### watch

Default: `false`

Type: `boolean`

Run build when files change.
2 changes: 1 addition & 1 deletion docs/angular/api-angular/builders/package.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package

Build an Angular library
Build and package an Angular library for publishing

Builder properties can be configured in angular.json when defining the builder, or when invoking it.

Expand Down
36 changes: 36 additions & 0 deletions docs/node/api-angular/builders/ng-packagr-lite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ng-packagr-lite

Build an Angular library for incremental building

Builder properties can be configured in workspace.json when defining the builder, or when invoking it.
Read more about how to use builders and the CLI here: https://nx.dev/node/guides/cli.

## Properties

### project

Type: `string`

The file path for the ng-packagr configuration file, relative to the current workspace.

### tsConfig

Type: `string`

The full path for the TypeScript configuration file, relative to the current workspace.

### updateBuildableProjectDepsInPackageJson

Default: `true`

Type: `boolean`

Update buildable project dependencies in package.json

### watch

Default: `false`

Type: `boolean`

Run build when files change.
2 changes: 1 addition & 1 deletion docs/node/api-angular/builders/package.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package

Build an Angular library
Build and package an Angular library for publishing

Builder properties can be configured in workspace.json when defining the builder, or when invoking it.
Read more about how to use builders and the CLI here: https://nx.dev/node/guides/cli.
Expand Down
36 changes: 36 additions & 0 deletions docs/react/api-angular/builders/ng-packagr-lite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ng-packagr-lite

Build an Angular library for incremental building

Builder properties can be configured in workspace.json when defining the builder, or when invoking it.
Read more about how to use builders and the CLI here: https://nx.dev/react/guides/cli.

## Properties

### project

Type: `string`

The file path for the ng-packagr configuration file, relative to the current workspace.

### tsConfig

Type: `string`

The full path for the TypeScript configuration file, relative to the current workspace.

### updateBuildableProjectDepsInPackageJson

Default: `true`

Type: `boolean`

Update buildable project dependencies in package.json

### watch

Default: `false`

Type: `boolean`

Run build when files change.
2 changes: 1 addition & 1 deletion docs/react/api-angular/builders/package.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package

Build an Angular library
Build and package an Angular library for publishing

Builder properties can be configured in workspace.json when defining the builder, or when invoking it.
Read more about how to use builders and the CLI here: https://nx.dev/react/guides/cli.
Expand Down
176 changes: 176 additions & 0 deletions e2e/angular/src/angular-library.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { toClassName } from '@nrwl/workspace';
import {
forEachCli,
newProject,
readJson,
runCLI,
uniq,
updateFile,
} from '@nrwl/e2e/utils';

forEachCli('angular', (cli) => {
['publishable', 'buildable'].forEach((testConfig) => {
describe('Build Angular library', () => {
/**
* Graph:
*
* childLib
* /
* parentLib =>
* \
* \
* childLib2
*
*/
let parentLib: string;
let childLib: string;
let childLib2: string;

beforeEach(() => {
parentLib = uniq('parentlib');
childLib = uniq('childlib');
childLib2 = uniq('childlib2');

newProject();

if (testConfig === 'buildable') {
runCLI(
`generate @nrwl/angular:library ${parentLib} --buildable=true --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib} --buildable=true --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib2} --buildable=true --no-interactive`
);
} else {
runCLI(
`generate @nrwl/angular:library ${parentLib} --publishable=true --importPath=@proj/${parentLib} --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib} --publishable=true --importPath=@proj/${childLib} --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib2} --publishable=true --importPath=@proj/${childLib2} --no-interactive`
);

// create secondary entrypoint
updateFile(
`libs/${childLib}/sub/package.json`,
`
{
"ngPackage": {}
}
`
);
updateFile(
`libs/${childLib}/sub/src/lib/sub.module.ts`,
`
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({ imports: [CommonModule] })
export class SubModule {}
`
);

updateFile(
`libs/${childLib}/sub/src/public_api.ts`,
`export * from './lib/sub.module';`
);

updateFile(
`libs/${childLib}/sub/src/index.ts`,
`export * from './public_api';`
);

updateFile(`tsconfig.base.json`, (s) => {
return s.replace(
`"@proj/${childLib}": ["libs/${childLib}/src/index.ts"],`,
`"@proj/${childLib}": ["libs/${childLib}/src/index.ts"],
"@proj/${childLib}/sub": ["libs/${childLib}/sub/src/index.ts"],
`
);
});
}

// create dependencies by importing
const createDep = (parent, children: string[]) => {
let moduleContent = `
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
${children
.map(
(entry) =>
`import { ${toClassName(
entry
)}Module } from '@proj/${entry}';`
)
.join('\n')}
`;

if (testConfig === 'publishable') {
moduleContent += `
import { SubModule } from '@proj/${childLib}/sub';
@NgModule({
imports: [CommonModule, ${children
.map((entry) => `${toClassName(entry)}Module`)
.join(',')}, SubModule]
})
export class ${toClassName(parent)}Module {}`;
}

updateFile(
`libs/${parent}/src/lib/${parent}.module.ts`,
moduleContent
);
};

createDep(parentLib, [childLib, childLib2]);
});

it('should throw an error if the dependent library has not been built before building the parent lib', () => {
expect.assertions(2);

try {
runCLI(`build ${parentLib}`);
} catch (e) {
expect(e.stderr.toString()).toContain(
`Some of the project ${parentLib}'s dependencies have not been built yet. Please build these libraries before:`
);
expect(e.stderr.toString()).toContain(`${childLib}`);
}
});

it('should build the library when it does not have any deps', () => {
const libOutput = runCLI(`build ${childLib}`);
expect(libOutput).toContain(`Built @proj/${childLib}`);
});

it('should properly add references to any dependency into the parent package.json', () => {
const childLibOutput = runCLI(`build ${childLib}`);
const childLib2Output = runCLI(`build ${childLib2}`);
const parentLibOutput = runCLI(`build ${parentLib}`);

expect(childLibOutput).toContain(`Built @proj/${childLib}`);
expect(childLib2Output).toContain(`Built @proj/${childLib2}`);
expect(parentLibOutput).toContain(`Built @proj/${parentLib}`);

const jsonFile = readJson(`dist/libs/${parentLib}/package.json`);
// expect(jsonFile.dependencies).toEqual({ tslib: '^2.0.0' });

expect(jsonFile.dependencies['tslib']).toEqual('^2.0.0');
expect(jsonFile.dependencies[`@proj/${childLib}`]).toBeDefined();
expect(jsonFile.dependencies[`@proj/${childLib2}`]).toBeDefined();
expect(jsonFile.peerDependencies['@angular/common']).toBeDefined();
expect(jsonFile.peerDependencies['@angular/core']).toBeDefined();
});
});
});
});

forEachCli('nx', () => {
describe('Build Angular library', () => {
it('should work', async () => {}, 1000000);
});
});
Loading

0 comments on commit 0892fa2

Please sign in to comment.