Pete Bacon Darwin 8d13f631d9 refactor(ngcc): support processing only the typings files of packages (#40976)
Some tools (such as Language Server and ng-packagr) only care about
the processed typings generated by ngcc. Forcing these tools to process
the JavaScript files as well has two disadvantages:

First, unnecessary work is being done, which is time consuming.

But more importantly, it is not always possible to know how the final bundling
tools will want the processed JavaScript to be configured. For example,
the CLI would prefer the `--create-ivy-entry-points` option but this would
break non-CLI build tooling.

This commit adds a new option (`--typings-only` on the command line, and
`typingsOnly` via programmatic API) that instructs ngcc to only render changes
to the typings files for the entry-points that it finds, and not to write any
JavaScript files.

In order to process the typings, a JavaScript format will need to be analysed, but
it will not be rendered to disk. When using this option, it is best to offer ngcc a
wide range of possible JavaScript formats to choose from, and it will use the
first format that it finds. Ideally you would configure it to try the `ES2015` FESM
format first, since this will be the most performant.

Fixes #40969

PR Close #40976
2021-02-24 14:23:14 -08:00

85 lines
3.2 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {DtsProcessing} from '../../src/execution/tasks/api';
import {computeTaskDependencies, sortTasksByPriority} from '../../src/execution/tasks/utils';
import {createTasksAndGraph} from './helpers';
describe('execution utils', () => {
describe('computeTaskDependencies()', () => {
it('should throw, if there are multiple tasks that generate typings for a single entry-point',
() => {
const {tasks, graph} = createTasksAndGraph(2, 2, {
0: [], // Entry-point #0 does not depend on anything.
1: [0], // Entry-point #1 depends on #0.
});
tasks[1].processDts = DtsProcessing.Yes; // Tweak task #1 to also generate typings.
expect(() => computeTaskDependencies(tasks, graph))
.toThrowError(
'Invariant violated: Multiple tasks are assigned generating typings for ' +
'\'/path/to/entry/point/0\':\n' +
' - {entryPoint: entry-point-0, formatProperty: prop-0, processDts: Yes}\n' +
' - {entryPoint: entry-point-0, formatProperty: prop-1, processDts: Yes}');
});
it('should add non-typings tasks to the dependents of typings tasks', () => {
const {tasks, graph} = createTasksAndGraph(2, 2, {
0: [], // entry-point-0 does not depend on anything.
1: [0], // entry-point-1 depends on entry-point-0.
});
const dependents = computeTaskDependencies(tasks, graph);
// entry-point-0
expect(dependents.get(tasks[0])).toEqual(new Set([
tasks[1], // non-typings task for entry-point-0
tasks[2], // typings task for entry-point-1, which depends upon entry-point-0
tasks[3], // non-typings task for entry-point-1, which depends upon entry-point-0
]));
expect(dependents.get(tasks[1])).toBeUndefined();
// entry-point-1
expect(dependents.get(tasks[2])).toEqual(new Set([
tasks[3], // non-typings task for entry-point-1
]));
expect(dependents.get(tasks[3])).toBeUndefined();
});
});
describe('sortTasksByPriority', () => {
it('should return the tasks in their original order if there are no dependencies', () => {
const {tasks} = createTasksAndGraph(3, 1);
const sortedTasks = sortTasksByPriority(tasks, new Map());
expect(sortedTasks).toEqual(tasks);
});
it('should return the tasks ordered by how many tasks depend upon them', () => {
// Before sort:
// 0 blocks [3]
// 1 blocks [2, 3]
// 2 blocks []
// 3 blocks [4]
// 4 blocks []
const {tasks, graph} = createTasksAndGraph(5, 1, {0: [], 1: [], 2: [1], 3: [0, 1], 4: [3]});
const sortedTasks = sortTasksByPriority(tasks, computeTaskDependencies(tasks, graph));
// After sort:
// 1 blocks [2, 3]
// 0 blocks [3]
// 3 blocks [4]
// 2 blocks []
// 4 blocks []
expect(sortedTasks).toEqual([
tasks[1],
tasks[0],
tasks[3],
tasks[2],
tasks[4],
]);
});
});
});