Skip to content

Commit 7e78fb4

Browse files
authored
feat: remove commonjs-allowed-dependencies on angular version under 10 (#37)
* feat: remove commonjs-allowed-dependencies on angular version under 10 * docs: update docs * feat: update build bundles * fix: apply pr review suggestions
1 parent c5e395b commit 7e78fb4

9 files changed

+354
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ The following packages are managed by this action:
134134
- `typescript`
135135
- `zone.js`
136136

137+
### Additional features
138+
139+
- Replace the `angular.json` library builder (ng-packagr) based on the angular version.
140+
- Use `@angular-devkit/build-ng-packagr:build` for version prior (<) Angular 10.1
141+
- Use `@angular-devkit/build-angular:ng-packagr` for version post (>=) Angular 10.1
142+
- Remove `allowedCommonJsDependencies` options on Angular versions under 10
143+
137144
## Contributing
138145

139146
We would love you to contribute to `@ngworker/angular-versions-action`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {AngularJson} from '../../src/types/angular-json';
2+
3+
export const lumberjackPreAngular10RemovedCommonJSDependencies: AngularJson = {
4+
projects: {
5+
'lumberjack-app': {
6+
projectType: 'application',
7+
architect: {
8+
build: {
9+
builder: '@angular-devkit/build-angular:browser',
10+
options: {
11+
vendorChunk: true,
12+
extractLicenses: false,
13+
buildOptimizer: false,
14+
sourceMap: true,
15+
optimization: false,
16+
namedChunks: true
17+
}
18+
},
19+
serve: {
20+
builder: '@angular-devkit/build-angular:dev-server'
21+
},
22+
'extract-i18n': {
23+
builder: '@angular-devkit/build-angular:extract-i18n'
24+
}
25+
}
26+
},
27+
'lumberjack-schematics-app': {
28+
projectType: 'application',
29+
architect: {
30+
build: {
31+
builder: '@angular-devkit/build-angular:browser',
32+
options: {
33+
vendorChunk: true,
34+
extractLicenses: false,
35+
buildOptimizer: false,
36+
sourceMap: true,
37+
optimization: false,
38+
namedChunks: true
39+
}
40+
},
41+
serve: {
42+
builder: '@angular-devkit/build-angular:dev-server'
43+
},
44+
'extract-i18n': {
45+
builder: '@angular-devkit/build-angular:extract-i18n'
46+
}
47+
}
48+
},
49+
'ngworker-lumberjack': {
50+
projectType: 'library',
51+
architect: {
52+
build: {
53+
builder: '@angular-devkit/build-ng-packagr:build',
54+
options: {
55+
tsConfig: 'libs/ngworker/lumberjack/tsconfig.lib.json',
56+
project: 'libs/ngworker/lumberjack/ng-package.json'
57+
}
58+
},
59+
test: {
60+
builder: '@angular-builders/jest:run'
61+
},
62+
lint: {
63+
builder: '@angular-devkit/build-angular:tslint'
64+
}
65+
}
66+
},
67+
'internal-test-util': {
68+
projectType: 'library',
69+
architect: {
70+
build: {
71+
builder: '@angular-devkit/build-angular:ng-packagr'
72+
},
73+
test: {
74+
builder: '@angular-builders/jest:run'
75+
},
76+
lint: {
77+
builder: '@angular-devkit/build-angular:tslint'
78+
}
79+
}
80+
},
81+
'internal-console-driver-test-util': {
82+
projectType: 'library',
83+
architect: {
84+
test: {
85+
builder: '@angular-builders/jest:run'
86+
},
87+
lint: {
88+
builder: '@angular-devkit/build-angular:tslint'
89+
}
90+
}
91+
}
92+
}
93+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import {AngularJson} from '../../src/types/angular-json';
2+
3+
export const lumberjackWithMultipleAllowedCommonJSDependencies: AngularJson = {
4+
projects: {
5+
'lumberjack-app': {
6+
projectType: 'application',
7+
architect: {
8+
build: {
9+
builder: '@angular-devkit/build-angular:browser',
10+
options: {
11+
allowedCommonJsDependencies: ['zone.js'],
12+
vendorChunk: true,
13+
extractLicenses: false,
14+
buildOptimizer: false,
15+
sourceMap: true,
16+
optimization: false,
17+
namedChunks: true
18+
}
19+
},
20+
serve: {
21+
builder: '@angular-devkit/build-angular:dev-server'
22+
},
23+
'extract-i18n': {
24+
builder: '@angular-devkit/build-angular:extract-i18n'
25+
}
26+
}
27+
},
28+
'lumberjack-schematics-app': {
29+
projectType: 'application',
30+
architect: {
31+
build: {
32+
builder: '@angular-devkit/build-angular:browser',
33+
options: {
34+
allowedCommonJsDependencies: ['zone.js'],
35+
vendorChunk: true,
36+
extractLicenses: false,
37+
buildOptimizer: false,
38+
sourceMap: true,
39+
optimization: false,
40+
namedChunks: true
41+
}
42+
},
43+
serve: {
44+
builder: '@angular-devkit/build-angular:dev-server'
45+
},
46+
'extract-i18n': {
47+
builder: '@angular-devkit/build-angular:extract-i18n'
48+
}
49+
}
50+
},
51+
'ngworker-lumberjack': {
52+
projectType: 'library',
53+
architect: {
54+
build: {
55+
builder: '@angular-devkit/build-ng-packagr:build',
56+
options: {
57+
tsConfig: 'libs/ngworker/lumberjack/tsconfig.lib.json',
58+
project: 'libs/ngworker/lumberjack/ng-package.json'
59+
}
60+
},
61+
test: {
62+
builder: '@angular-builders/jest:run'
63+
},
64+
lint: {
65+
builder: '@angular-devkit/build-angular:tslint'
66+
}
67+
}
68+
},
69+
'internal-test-util': {
70+
projectType: 'library',
71+
architect: {
72+
build: {
73+
builder: '@angular-devkit/build-angular:ng-packagr'
74+
},
75+
test: {
76+
builder: '@angular-builders/jest:run'
77+
},
78+
lint: {
79+
builder: '@angular-devkit/build-angular:tslint'
80+
}
81+
}
82+
},
83+
'internal-console-driver-test-util': {
84+
projectType: 'library',
85+
architect: {
86+
test: {
87+
builder: '@angular-builders/jest:run'
88+
},
89+
lint: {
90+
builder: '@angular-devkit/build-angular:tslint'
91+
}
92+
}
93+
}
94+
}
95+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import {removeCommonJsAllowList} from '../src/remove-commonjs-allowed-dependencies';
2+
import {lumberjackWithMultipleAllowedCommonJSDependencies} from './angular-json/lumberjack-with-multiple-allowed-commonjs-dependencies-options';
3+
import {lumberjackPreAngular10RemovedCommonJSDependencies} from './angular-json/lumberjack-pre-angular10-removed-allowed-commonjs-dependencies';
4+
5+
import {AngularVersion} from '../src/types/angular-version';
6+
7+
const preAngular10Versions: AngularVersion[] = [
8+
'6.0.x',
9+
'6.1.x',
10+
'7.0.x',
11+
'7.1.x',
12+
'7.2.x',
13+
'8.0.x',
14+
'8.1.x',
15+
'8.2.x',
16+
'9.0.x',
17+
'9.1.x'
18+
];
19+
20+
const postAngular10Versions: AngularVersion[] = [
21+
'10.0.x',
22+
'10.1.x',
23+
'10.2.x',
24+
'11.0.x',
25+
'11.1.x',
26+
'11.2.x',
27+
'12.0.x'
28+
];
29+
30+
describe(removeCommonJsAllowList.name, () => {
31+
describe('when using angular versions older than 10.0', () => {
32+
preAngular10Versions.forEach(version => {
33+
describe(`using the version ${version}`, () => {
34+
const actualLumberjackAngularJson = removeCommonJsAllowList(
35+
version,
36+
lumberjackWithMultipleAllowedCommonJSDependencies
37+
);
38+
39+
describe('the lumberjack workspace angular.json', () => {
40+
test('should exactly match the snapshot', () => {
41+
expect(actualLumberjackAngularJson).toEqual(
42+
lumberjackPreAngular10RemovedCommonJSDependencies
43+
);
44+
});
45+
46+
test('should delete the allowedCommonJsDependencies', () => {
47+
expect(
48+
actualLumberjackAngularJson.projects['lumberjack-app'].architect
49+
.build.options?.allowedCommonJsDependencies
50+
).toBeUndefined();
51+
expect(
52+
actualLumberjackAngularJson.projects['lumberjack-schematics-app']
53+
.architect.build.options?.allowedCommonJsDependencies
54+
).toBeUndefined();
55+
});
56+
});
57+
});
58+
});
59+
});
60+
describe('when using angular versions newer or equal than 10.0', () => {
61+
postAngular10Versions.forEach(version => {
62+
describe(`using the version ${version}`, () => {
63+
const actualLumberjackAngularJson = removeCommonJsAllowList(
64+
version,
65+
lumberjackWithMultipleAllowedCommonJSDependencies
66+
);
67+
68+
describe('the lumberjack workspace angular.json', () => {
69+
test('should exactly match the snapshot', () => {
70+
expect(actualLumberjackAngularJson).toEqual(
71+
lumberjackWithMultipleAllowedCommonJSDependencies
72+
);
73+
});
74+
75+
test('should keep the allowedCommonJsDependencies', () => {
76+
expect(
77+
actualLumberjackAngularJson.projects['lumberjack-app'].architect
78+
.build.options?.allowedCommonJsDependencies
79+
).toBeDefined();
80+
expect(
81+
actualLumberjackAngularJson.projects['lumberjack-schematics-app']
82+
.architect.build.options?.allowedCommonJsDependencies
83+
).toBeDefined();
84+
});
85+
});
86+
});
87+
});
88+
});
89+
});

dist/index.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {cloneDeep} from 'lodash-es';
2+
3+
import {AngularVersion} from './types/angular-version';
4+
import {AngularJson} from './types/angular-json';
5+
import {angularVersionComparer} from './angular-version-comparer';
6+
import {ProjectConfiguration} from './types/project-configuration';
7+
import {ArchitectConfiguration} from './types/architect-configuration';
8+
9+
const exclusiveAngularVersion = '10.0.x';
10+
11+
export function removeCommonJsAllowList(
12+
angularVersion: AngularVersion,
13+
angularJson: AngularJson
14+
): AngularJson {
15+
const modifiedAngularJson = cloneDeep(angularJson);
16+
17+
const projectsAndTargetsWithAllowedCommonJSDependencies = getNameOfProjectsAndTargetsWithAllowedCommonJSDependencies(
18+
angularJson
19+
);
20+
21+
projectsAndTargetsWithAllowedCommonJSDependencies.forEach(
22+
([projectName, targetName]) => {
23+
if (isRemovable(angularVersion)) {
24+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
25+
delete modifiedAngularJson.projects[projectName].architect[targetName]
26+
.options!.allowedCommonJsDependencies;
27+
}
28+
}
29+
);
30+
31+
return modifiedAngularJson;
32+
}
33+
34+
function getNameOfProjectsAndTargetsWithAllowedCommonJSDependencies(
35+
workspace: AngularJson
36+
): [string, string][] {
37+
return Object.entries(workspace.projects)
38+
.filter(([, projectConfig]) => projectConfig.projectType === 'application')
39+
.map(([projectName, application]) => {
40+
return [
41+
projectName,
42+
findProjectTargetWithAllowedCommonJSDependencies(application)
43+
] as [string, [string, ArchitectConfiguration]];
44+
})
45+
.filter(([, target]) => target !== undefined)
46+
.map(([projectName, target]) => [projectName, target[0]]);
47+
}
48+
49+
function findProjectTargetWithAllowedCommonJSDependencies(
50+
application: ProjectConfiguration
51+
): [string, ArchitectConfiguration] | undefined {
52+
return Object.entries(application.architect).find(
53+
([, targetConfig]) => !!targetConfig.options?.allowedCommonJsDependencies
54+
);
55+
}
56+
57+
function isRemovable(angularVersion: AngularVersion): boolean {
58+
return angularVersionComparer(angularVersion, exclusiveAngularVersion) < 0;
59+
}

src/replace-libraries-ngpackagr-builder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ export function replaceLibrariesNgPackagrBuilder(
2222
): AngularJson {
2323
const modifiedAngularJson = cloneDeep(angularJson);
2424

25-
const projectsAndTargetsWithNgPackagrBuilder =
26-
getNameOfProjectsAndTargetsUsingNgpackagr(angularJson);
25+
const projectsAndTargetsWithNgPackagrBuilder = getNameOfProjectsAndTargetsUsingNgpackagr(
26+
angularJson
27+
);
2728

2829
const correctBuilderForVersion = getCorrectNgPackgrBuilder(angularVersion);
2930

3031
projectsAndTargetsWithNgPackagrBuilder.forEach(
3132
([projectName, targetName]) => {
32-
modifiedAngularJson.projects[projectName].architect[targetName].builder =
33-
correctBuilderForVersion;
33+
modifiedAngularJson.projects[projectName].architect[
34+
targetName
35+
].builder = correctBuilderForVersion;
3436
}
3537
);
3638

src/types/architect-configuration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface ArchitectConfiguration {
22
builder: string;
3+
options?: {
4+
[prop: string]: unknown;
5+
};
36
[prop: string]: unknown;
47
}

0 commit comments

Comments
 (0)